Note that the following commands need to be run as root, or sudo. Note: If you want to customize the installation location, you need to configure the GOROOT environment variable, so here we use the default directory to install, so we can not configure the GOROOT environment variable.

Go to go1.5.1.linux-amd64.tar.gz. Go to go1.5.1.linux-amd64.tar.gz

/usr/local/go tar-c /usr/ local-xzf go1.5.1.linux-amd64.tar.gz /usr/local/go tar-c /usr/ local-xzf go1.5.1.linux-amd64.tar.gz /usr/local/go tar-c /usr/ local-xzf go1.5.1.linux-amd64.tar.gz

■ 2 Add the PATH environment variable

Export PATH=$PATH:/usr/local/go/bin

■ 3. Create the Go workspace (workspace) where the GOPATH environment variable points to

Go code must be in the game workspace. The workspace is a directory that contains three subdirectories: each subdirectory in SRC —- is a package. Inside the package is the Go source file PKG —- compiled after the generation, the package target file bin is generated executable file. Here, we will create a folder called gopath(it can be anything but gopath) in the /home directory, and then create three subfolders (subfolder names must be SRC, PKG, bin). The diagram below:

■ 4. Set the GOPATH environment variable

Export GOPATH=/home/gopath After saving, execute the following command to make the environment variable take effect immediately: source /etc/profile

At this point, the Go language environment is installed. Let’s start with HelloWorld

■ 5, New project (application package)

In general, it is recommended that the package name and directory name should be the same, so in the SRC folder, organize the folder path by the package name you want to create. Here, we create the hello folder under /home/gopath/ SRC. For example: If your package name is myGo /first/hello, then you need a directory structure like this: / home/gopath/SRC/mygo/first/hello if considering the introduction of the git repository management, then can create such a package name:

■ 6. Create a new Go code

Package main import “FMT” func main() {/home/gopath/ SRC /hello: Package main import “FMT” func main() {

fmt.Printf("Hello, world.\n")


■ 7. Compile and generate GO program

On any file path, run: Go Install Hello

You can also go to the path of the project (application package) and run: Go Install

Note that when compiling a Go program, Go actually looks for the package in two places: under the SRC folder under GOROOT, and under the SRC folder under goPath.

In the program package, automatically find the main function of the main package as the entry point of the program, and then compile.

■ 8, run the GO program

Under /home/gopath/bin/, you’ll see a hello executable file appear, run with the following command:./hello