This section focuses on
1) Go install path for goroot How to set Gopath add Gopath/bin directory in Path
2) Gopath directory (here for/home/admin/go) has three folders bin pkg src but generally src is my own creation, the rest is not
If you do not want to create these folders, such as the creation of Pkg involved in the idea of Go run time may go install will be refused to write the pkg created by myself
3) Idea Project save path (here is/home/admin/go/src/learngo) my own note: It is best to save the next time as/home/admin/go/src/ycx/learngo
4) Unable to get Golang package directly so use work gopm (get time with Go get command but always remember to install git)
5) If an error occurs when running a program in idea (please check to see if you have gone to the package directory for Go install, if there is no error about LINUX_AMD64)
6) Gpath: Go build to compile go install generate pkg file and executable go run direct compile runs
Gopath
[Admin@localhost ~]$ cd/opt/go[admin@localhost go]$ pwd/opt/go[admin@localhost go]$ echo $GOPATH/ home/admin//etc/profile
Gopath
Focus on: Gopath and setting path about Gopath
Go get
Execute the command under the configured Gopath directory: Go get-v github.com/gpmgo/gopm
Note 1: If you cannot do this, check to see if you have gitinstalled.
Note: If the error message appears as follows (please remove the github.com folder from the SRC directory using the command: RM-RF folder name
Install GOPM complete Check the directory for some files: (At this point gopm installation is complete, you can see the GOPM help to run)
[Root@localhost go]# GOPM Help
[Root@localhost go]# gopm help get
Using GOPM:
Use the command for the first time, there's no problem.
Open idea to see Gopath has appeared:
Run the Go Build command to build the required Goimports to install it under the Bin directory
Note: Here it will do two things (first: the import of idea two empty line second piece: A lot of things in the golang.org x directory)
Using the example
We can use it for a bit: using Intsets. sparse{} Below is the total code for the tree
Note: Error running program Treeentry.go: (This is due to the fact that you have created a pkg, if not you will not report this error)
Workaround:
Tree
Directory structure:
Package Mainimport ("FMT" "Learngo/tree" "golang.org/x/tools/container/intsets") Type Mytreenodestruct{node*Tree. Node}func (Mynode*Mytreenode) Postorder () {ifMynode = = Nil | | Mynode.node = =Nil {return} Left:=Mytreenode{mynode.node.left} right:=Mytreenode{mynode.node.right} left.postorder () Right.postorder () myNode.node.Print ()}func testsparse () { S:=intsets. sparse{} s.insert (1) S.insert ( +) S.insert (1000000) fmt. Println (S.has ( +) FMT. Println (S.has (10000000) )}func main () {varroot tree. Node Root= Tree. Node{value:3} root. Left= &Tree. node{} root. Right= &tree. node{5, nil, nil} root. Right.left=New(tree. Node) root. Left.right= Tree. CreateNode (2) root. Right.Left.SetValue (4) fmt. Print ("In-order Traversal:") root. Traverse () fmt. Print ("My own Post-order traversal:") Myroot:= mytreenode{&root} myroot.postorder () fmt. Println () Nodecount:=0Root. Traversefunc (Func (Node*Tree. Node) {Nodecount++}) fmt. Println ("Node Count:", Nodecount) C:=Root. Traversewithchannel () Maxnodevalue:=0 forNode: =Range C {ifNode. Value >Maxnodevalue {maxnodevalue=node. Value}} fmt. Println ("Max node Value:", Maxnodevalue) Testsparse ()} Entry.go
Package Treeimport"FMT"type Nodestruct{ValueintLeft , right*Node}func (Node node) Print () {fmt. Print (node. Value," ")}func (node*node) SetValue (valueint) { ifnode = =Nil {fmt. Println ("Setting Value to nil"+"node. ignored.") return} node. Value=value}func CreateNode (valueint) *Node {return&Node{value:value}} Node.go
Package Treeimport"FMT"Func (Node*Node) Traverse () {node. Traversefunc (func (n*Node) {n.print ()}) Fmt. Println ()}func (node*node) Traversefunc (f func (*Node)) { ifnode = =Nil {return} node. Left.traversefunc (f) F (node) node. Right.traversefunc (f)}func (node*node) Traversewithchannel () Chan *Node { out: = Make (Chan *node) go func () {node. Traversefunc (Func (Node*Node) { out<-node}) Close ( out) }() return out} Traversal.go
The output is:
In-order traversal:0 2 3 4 5 My own post-order traversal:2 0 4 5 3 node Count:5Max node value:5truefalse
process finished with exit code 0
Queue
Do the prep work first.
The code structure is as follows:
Package Mainimport ( "fmt" "learngo/queue" func Main () { Q:= queue. queue{1} Q.push (2) Q.push (3) FMT. Println (Q.pop ()) FMT. Println (Q.pop ()) FMT. Println (Q.isempty ()) FMT. Println (Q.pop ()) FMT. Println (Q.isempty ())}
Main.go
Package Queue//A FIFO queue.Type Queue []int//pushes the element into the queue.//e.g. Q.push (123)Func (q *queue) Push (vint) { *q = Append (*Q, v)}//Pops element from head.Func (q *queue) Pop ()int{head:= (*Q) [0] *q = (*q) [1:] returnHead}//Returns If the queue is an empty or not.Func (q *queue) IsEmpty ()BOOL { returnLen (*q) = =0} Queue.go
The output is:
1 2 false 3 true 0
Package Queueimport"FMT"func Examplequeue_pop () {q:= queue{1} q.push (2) Q.push (3) fmt. Println (Q.pop ()) fmt. Println (Q.pop ()) fmt. Println (Q.isempty ()) fmt. Println (Q.pop ()) fmt. Println (Q.isempty ())//Output://1//2//false//3//true} Queue_test.go
Run is: