Linux Go Environment installation
Method One
This time the source package installs the directory is/root under.
1, the official website download source package.
官网链接:https://golang.org/dl/。ps:本人的是linux服务器,所以选择红框标注的
wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
2. Decompression and Installation
TAR-ZXVF go1.9.2.linux-amd64.tar.gz
Mkdir-p WORKSPACE/SRC
Vim ~/.BASHRC
export GOROOT=$HOME/goexport GOPATH=$HOME/workspaceexport PATH=$GOROOT/bin:$GOPATH/bin:$PATH
SOURCE ~./BASHRC
3. Verify the installation is successful
Method Two
Installing the Go Tool
Download the latest Linux version in http://golang.org/dl/and extract it to the/usr/local directory, unzip it in this directory
$ TAR-XVF xxx.tar.gz
The/usr/local/go/bin is then added to the PATH environment variable to perform
$ export path= $PATH:/usr/local/go/bin
In fact, go will assume that it is installed in the/usr/local/go directory, but you can also install go to a different location, and you must set the GOROOT environment variable to indicate where it is installed.
For example, to install go to your HOME directory, you should add the following command to the $home/.profile file
$ export goroot= $HOME/go
$ export path= $PATH: $GOROOT/bin
Execute go version to see the installation of Go is successful
Your first program
Gopath Environment variables
Gopath environment variable Specifies the location of your workspace
First create a working directory, and set the appropriate Gopath, the working directory can be placed anywhere, but not the same as the Go installation directory, where we use $home/work
$ mkdir $HOME/work
$ export gopath= $HOME/work
Note: The go code must be placed in the workspace, which is our work directory, which contains three subdirectories
The bin directory contains executable commands
Pkg Directory contains Package objects
The SRC directory contains the go source files, which are organized into packages (each directory corresponds to a package)
Next, add the bin subdirectory of the workspace to the path:
[HTML] View plain copy
$ export PATH=$PATH:$GOPATH/bin
Package path
A package in the standard library has a given short path such as "FMT", and for your own package, you must also select a basic path to ensure that it does not conflict with packages that will be added to the standard library or other standard libraries in the future.
Using packs as the base path, create a directory in your workspace where we put the source:
$ mkdir $GOPATH/src/packs
Your first program
To compile a simple program, first select the package path, where we use Packs/hello and create the appropriate package directory within your workspace
$ mkdir $GOPATH/src/packs/hello
Next, create a file named Hello.go in this directory with the following contents
Package Main
Import "FMT"
Func Main () {
Fmt. Printf ("Hello, world.\n")
}
You can now build and install the program using the Go tool
$ go Install Packs/hello
Note that you can run this command from anywhere on the system. The Go tool will find the source code within the Packs/hello package based on the workspace specified by Gopath.
If you run go install from the package directory, you can also omit the package path:
$ cd $GOPATH/src/packs/hello
$ go Install
This command builds the Hello command, resulting in an executable binary file. and stored in the bin directory of the workspace, here is the $gopath/bin directory
Because you have added $gopath/bin to path, you only need to enter the binary file name to execute.
$ hello
Hello, world.
Your first library
Let's write a library and let the Hello program use it.
Similarly, the first step is to select the package path (use Packs/stringutil here) and create the package directory:
$ mkdir $GOPATH/src/packs/stringutil
Next, create a file named Reverse.go in the directory that reads as follows:
The stringutil contains a tool function for working with strings.
Package Stringutil
Reverse will actually invert the argument string in runes.
Func Reverse (s string) string {
r: = []rune (s)
For I, J: = 0, Len (r)-1; I < Len (r)/2; I, j = i+1, j-1 {
R[i], r[j] = R[j], r[i]
}
return string (R)
}
Then use the Go Build command to test the package's compilation:
$ go Build packs/stringutil
, this will not produce the output file, if you want to output it, you must use the Go Install command, it will put the object of the package into the PKG directory of the work space.
Next, modify the original Hello.go file
Package Main
Import (
"FMT"
"github.com/user/stringutil"
)
Func Main () {
Fmt. Printf (Stringutil. Reverse ("!og, Olleh"))
}
And then through
$ go Install Packs/hello
To install the Hello program and then run the new version of the program, you should see a new, Reverse message:
$ hello
Hello, go!.
Method Three
The
declares the root user to log on.
$ vim/etc/profile
After opening/etc/profile//last line Insert
Export goroot=/usr/local/go//Here My go decompression is in/user/local/go
Export Gopath=~/golib:~/goproject
Export gobin=~/gobin
Export path= $PATH: $GOROOT/bin: $GOBIN
//Then press ESC Skip to command mode, and then save exit
: Wq//Press confirm keyboard
//input below to get the command to take effect
$ source/etc/profile//ok
//To determine if the setting is successful, regardless of the CD under any folder enter
$ go Version