Day2 - GO environment installation

100day Institute golang series:
https://github.com/qisenshi/Go-100-Days
Updating continuously

Download Go

Download address:

  • Go official website download address: https://golang.org/dl/

  • Go official mirror station (recommended): https://golang.google.cn/dl/

It is recommended to download the executable version on Windows platform and Mac platform, and the compressed file version on Linux platform.

Install Go

Linux

Linux installation is relatively simple. wget needs the version of go, and then set the environment variables. If you don't want to knock the go code on the Linux platform, you don't need to install go on the Linux platform. The go code written on our development machine can be copied to the Linux server after cross platform compilation, which is also the advantage of cross platform easy deployment of Go program.

1. For example, Download go1 14.1. linux-amd64. tar. GZ file:

wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz

2. Unzip the downloaded file to / usr/local directory:

tar -zxvf go1.14.1.linux-amd64.tar.gz -C /usr/local  

If you are prompted that you do not have permission, add sudo and run it as root. After execution, you can see the go directory under / usr/local /.

3. Configure environment variables: (the specific meaning of environment variables will be introduced later)
There are two files under Linux that can configure environment variables, among which / etc/profile is effective for all users$ HOME/.profile is effective for the current user. Select a file to open according to your own situation, add the following two lines of code, save and exit.

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
After modifying / etc/profile, you need to restart and take effect H O M E / . p r o f i l e after send use s o u r c e life order plus load HOME/. Use the source command to load the profile HOME/. After the profile, use the source command to load home / The profile file takes effect. Check: go version
go version go1.14.1 linux/amd64

Mac

Download the executable version and click next to install. Go will be installed in / usr/local/go directory by default.

Similarly, check with go version after installation.

windows

For example, download the 64 bit Win10 system go1 14.1 install the package locally.

Then click next all the way. When you need to set the go installation directory, you can select a directory you want to install. This path is GOROOT.
For example:

Go environment variable

After installing go, you also need to set GOPATH. Here, let me explain the meaning of several nouns:

  • GOROOT:
    The value of GOROOT is the Go installation directory and the location of its own standard library. Generally, users do not need to set GOROOT. By default, the Go language installation tool will set it as the installation directory path. (Mac is usually installed in / usr/local/go).

  • GOBIN:
    The executable files generated after go install are placed in the bin directory. Multiple paths are not allowed. Can be empty. If it is empty, the principle of "Convention is better than configuration" is followed, and the executable files are placed in the bin folder of their GOPATH directory

  • GOPATH:
    Go is a very important environment variable commonly used in the working environment (this design is similar to java). Specific purpose: the go command is often used, such as go run, go install, go get, etc. It is allowed to set multiple paths. Like the multipath setting of each system environment, windows uses ", linux (mac) is separated by ":".

GOPATH is the directory where the code base is located, that is, the workspace. The workspace is divided into three basic directories:

  1. src: store the source code (such as. Go. C. H. S) according to the default convention of golang, the current working path of go run, go install and other commands (that is, execute the above commands under this path).

  2. pkg: stores intermediate files generated during compilation, mainly * a documents. It is used to store the archive file of the code package installed through the go install command. The premise is that the code package must contain the Go library source file. Archive files are files whose names end with ". a".

  3. Bin: it mainly stores the build executable file. After the project passes the go build, go install or go get instructions, the generated binary executable file will be placed in the $GOPATH/bin directory.

     goWorkSpace     // goWorkSpace is the GOPATH directory
       -- bin
          -- myApp1  // Compile generation
          -- myApp2  // Compile generation
          -- myApp3  // Compile generation
       -- pkg
       -- src
          -- common 1
          -- common 2
          -- common utils ...
          -- myApp1     // project1
             -- models
             -- controllers
             -- others
             -- main.go 
          -- myApp2     // project2
             -- models
             -- controllers
             -- others
             -- main.go 
          -- myApp3     // project3
             -- models
             -- controllers
             -- others
             -- main.go 
    

You can use go env to view the current go environment variables. Look at env on my windows computer.

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\shiqi\AppData\Local\go-build
set GOENV=C:\Users\shiqi\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\shiqi\IdeaProjects\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\shiqi\IdeaProjects\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn
set GOROOT=D:\Go
set GOSUMDB=sum.golang.google.cn
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config

Compiler recommendation

  • GOLand (first)
  • VSCode

Keywords: Go

Added by jclarkkent2003 on Sat, 22 Jan 2022 12:28:53 +0200