This is a creation in Article, where the information may have evolved or changed.
Http://www.cnblogs.com/zhepama/archive/2013/04/04/3000027.html
Installing Golang
Goroot theoretically as long as the definition of this environment variable can play, go compile time will be first in the installation directory to find the package
GOBIN if not defined, the default is $goroot/bin if you want to not switch directories to execute the GO command. You need to add $gobin to the path directory. You know that.
Gopath is generally your development directory, you can have multiple. Multiple directories when Windows is a semicolon, the Linux system is a colon, and when there are multiple Gopath, the contents of go get are placed in the first directory by default
The $GOPATH directory convention has three subdirectories:
- SRC store source code (e.g.,. Go. C. h, etc.)
- The files generated by the PKG after compilation (for example:. a)
- Executable file generated after bin compilation (for convenience, this directory can be added to the $PATH variable)
Compile
Building a Go program takes two steps: compiling and linking.
The compile and link process is handled by the tool go, which not only builds local programs and packages, but also gets, builds, and installs third-party programs and packages.
Go Build
Go build compiles only the main program (the go file with the main () function). And if you do not specify the-o parameter, an executable file is generated in the current directory.
Go build-o Myfirstgo For example hellogo.go compiling exponentially target file name Myfirstgo
Go build without the file name, we will get an executable file with the same name as the directory name, such as App/main.go to get the app
If there are multiple items, that is, by more than one main. You can specify an item, which can be followed by a go build file name, for example go build a.go ;
Go get
Go get tools to get remote packages, now go get support for most open source communities (ex: GitHub, Googlecode, BitBucket, Launchpad)
For example, the go get github.com/astaxie/beedb go get-u parameter automatically updates the package and automatically gets the other third-party packages that the package relies on when go get
Go get is essentially understood as the first step is to go through the Source Code tool, clone, below SRC, and then run
To go can compile local programs and packages, there are three requirements:
- The Go Bin directory ($GOROOT/bin or%goroot%\bin) must be under the PATH environment variable
- A directory must exist that contains a src directory, and the source code for local programs and packages resides in the SRC directory
- The directory containing SRC must be set in the GOPATH environment variable
The go compiler's package search order is like this, to search for the Hello package for example:
- First, the go compiler will find the source of Src/pkg/hello related packages in the Go install directory (Goroot, here is/usr/local/go);
- If gopath=path1:paht2, the go compiler will look for the presence of Path1/src/hello, Path2/src/hello, and PATH1 and PATH2 configured in Gopath are called workplace;
Go Install
go Install command in addition to doing what go build, the executable file is also placed in a standard location ($ Gopath/bin or%gopath%\bin).
go install can also compile other non-main program packages placed in the pkg directory
Don't worry, the previous said go install also has its own convention:
- Go install compiles an executable file with the name of the directory in which it is located. So you can change the main directory to the name of the directory you want.
- Go install installs the executable file in the bin directory with the SRC peer, and the bin directory is created automatically by go install. If the primary file is not in the Src/dir subdirectory, it will not be generated to the bin directory.
- Go install compiles the various package that the executable depends on and places it in the PKG directory at the same level as the SRC
.| --bin| '--main.exe|--pkg| '--windows_amd64| '--hello.a '--src |--Hello | '--hello.go '--main '--main.go
The 1.GO entry is the main function under package main: is independent of the package name and file name in which it resides. That is, the package name, and the file name can be customized.
2. Although the package name can be customized, try to keep it as main. Because Golang does not allow a file with two different package names in the same directory, the following conflicts occur if the packages for App/main.go,app/test.go,test are app.
Can ' t load package:package app:found packages main (Main.go) and app (Test.go) In/volumes/opt/workspace-go/src/app