golang quick start [2.3]-go language development environment configuration linux
linux install Go language development package
The default reader will use the basic operation of linux
The first step to configure the go development environment is to go official website download page Download development package
linux needs to download tar.gz compressed file
Here we download the 64 bit development package. If the reader's computer is a 32-bit system or has special needs, we need to download the 32-bit development package
Scroll down the page shown in the figure above to find the download address of the 32-bit development package, as shown in the figure below
Note that if there is a graphical interface of linux operating system like ubuntu, we can download it directly
When there is no graphical interface, we need to operate on the command line
Step 1: download the development package
>> wget https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz --2020-02-06 14:18:58-- https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz Resolving dl.google.com (dl.google.com)... 126.96.36.199, 188.8.131.52, 184.108.40.206, ... Connecting to dl.google.com (dl.google.com)|220.127.116.11|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 120071076 (115M) [application/octet-stream] Saving to: 'go1.13.7.linux-amd64.tar.gz' 2020-02-06 14:19:19 (7.72 MB/s) - 'go1.13.7.linux-amd64.tar.gz' saved [120071076/120071076]
Use the tar command to extract the newly downloaded Go language development package to the / usr/local directory
>> tar -C /usr/local -xzf go1.13.7.linux-amd64.tar.gz
After the decompression is successful, a go directory will be added under the / usr/local directory, so far our go language development package has been installed.
Use the cd /usr/local/go command to enter the directory, and then execute the bin/go version command to view the current version of the Go language.
>> bin/go version go version go1.13.7 linux/amd64
Use the ls command to list the files and folders in the current directory
ls AUTHORS CONTRIBUTING.md CONTRIBUTORS LICENSE PATENTS README.md SECURITY.md VERSION api bin doc favicon.ico lib misc pkg robots.txt src test
The structure of this Directory follows the GOPATH rule, which will be mentioned later in the chapter. The meaning of each folder in the directory is shown in the following table.
The directory name indicates the api change difference of each version of api. Some library files referenced by the English version of Go document lib, such as the compilation of Android platform, the submission hook of git and other intermediate files compiled by pkglinux platform, such as the source code test of src standard library, are compiled by bingo source package compiler (Go), document tool (Go DOC), format tool (gofmt) doc, English version of Go document lib test case
Set GOPATH environment variable
Before we start to write the go project code, we need to configure the environment variables.
These environment variables need to be added to the profile file (~ /. Bash'profile or / etc/profile).
If it is used by a single user, you can add the environment variable to the bash ﹣ profile file in the home directory. If it is used by multiple users, you need to add it to the / etc/profile file. (it is recommended to set environment variables in the / etc/profile file.)
Use an editor such as the vim /etc/profile command to open the profile file and add the following environment variables to the end of the file.
Use the: wq command to save and exit after adding.
Then, use the source /etc/profile command to make the configuration file effective, and now you can use the Go language command in any directory.
export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin
After the environment variable is set, you can test it by entering the go env command on the command line.
>> go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go"