I will explain the construction under Linux, Windows and Mac systems one by one, including the configuration of the editor.
What to prepare?
Go installation package: Win Go installation package,Linux Go package (binary),Mac Go package
Git environment, Go downloads dependent packages with the help of GIT tools, which will not be explained here.
Test for installation
If the version command is output normally, it indicates that it has been installed.
go version ### output go version go1.16.4 windows/amd64
Windows
- After obtaining the installation package, you can directly go to the next fool installation and remember your installation location.
- To configure environment variables, some unnecessary ones are not set, such as GOBIN.
GO111MODULE: on, open the gomod management pack
GOPROXY: https://goproxy.cn , set up the agent to speed up the download of dependent packages
GOPATH: C:\workspace\go (self defined), working directory
PATH: add C:\Program Files\Go\bin and C:\workspace\go\bin
- Run the Go environment command
go env ### Output, part of the content is omitted, and only the key points are selected set GO111MODULE=on ... set GOPATH=C:\workspace\go ... set GOPROXY=https://goproxy.cn ...
The Windows environment will be configured. It's simple. Go down and directly look down at the editor's configuration.
Mac
- After downloading the installation package in the Mac environment, install it directly
- Environment configuration (explained in Windows)
### gomod, managing third-party packages echo 'export GO111MODULE=on' >> ~/.bash_profile ### Agent, accelerate dependent package download echo 'export GOPROXY=https://goproxy.cn' >> ~/.bash_profile ### Working directory, customizing echo 'export GOPATH=/Users/miaogaolin/workspace/go' >> ~/.bash_profile ### Join PATH echo 'echo PATH=$PATH:$GOPATH/bin' >> ~/.bash_profile ### Environment variable validation source ~/.bash_profile
After installing the Mac environment, turn down the editor installation.
Linux
Choosing the installation package I provided above is a common way under Linux. Is there any other simpler way? Answer: Yes.
Ubuntu
sudo apt-get golang-go
Centos
sudo yum install go -y
recommend
- Download installation package
wget https://studygolang.com/dl/golang/go1.16.4.linux-amd64.tar.gz
- decompression
tar -xvf go1.16.4.linux-amd64.tar.gz
The full directory path I extracted is / mnt/c/Users/owner/go, which can be moved to the location I want to place.
- Configure environment variables
### GOROOT, installation path configuration echo 'export GOROOT=/mnt/c/Users/owner/go' >> ~/.bash_profile ### gomod, managing third-party packages echo 'export GO111MODULE=on' >> ~/.bash_profile ### Agent, accelerate dependent package download echo 'export GOPROXY=https://goproxy.cn' >> ~/.bash_profile ### Working directory, customizing echo 'export GOPATH=/Users/miaogaolin/workspace/go' >> ~/.bash_profile ### Join PATH echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.bash_profile ### Environment variable validation source ~/.bash_profile
editor
Here I recommend Golan. Download the link: https://www.jetbrains.com/go/download.
The installation process will not be described, but directly start from the configuration. If you want to crack the little partner, you can ask me quietly.
- Open the project directory, file > Settings > project structure. If it is not set, click + Add Content Root to set it.
- Check GOROOT, file > Settings > Go > GOROOT, if not, as shown in the figure. Click and select Local to load the Go directory you installed.
- i check GOPATH, file > Settings > go > GOPATH. If there is no problem when configuring environment variables, the path of GOPATH will be displayed automatically.
- Configure two tools
- goimports, which automatically imports the third-party package when writing code. It needs to be downloaded. After running the following command, a binary file will be generated in $GOPATH/bin.
go get [golang.org/x/tools/cmd/goimports](http://golang.org/x/tools/cmd/goimports)
- gofmt, automatic formatting, self-contained, no need to download
Next, configure the two tools in Golan, open file > Settings > Tools > file watchers, and click the plus sign as shown in the figure to import.
- After configuration, let's have a "Hello World" code experience. Let's see how the directory is built.
Bin: automatically generated. When you run the go get command above, a bin directory is automatically generated to place the runnable binary files.
pkg: automatically generated. The generated third-party package will be automatically placed in this directory.
src: manually create a new directory. gobasic is the directory where I write code. You can also create other directories under src.
The code of hellworld.go file is as follows:
// Package name of the entry file package main // Reference package import "fmt" // Entry function func main() { // console output fmt.Println("Hello World!") }
The console runs the following commands:
### Initialize gomod go mod init ### function go run hellworld.go ### output Hello World!
summary
This chapter explains the construction of Go environment and the configuration of editor. During the actual construction, everyone may encounter different problems. If you don't know how to deal with them, please comment below!