1. Download and install Golang development environment
Download address
Golang official website: golang.google.cn/dl/
The version numbers below may not be up to date, but overall the installation tutorial is similar. Go language updates quickly. It is recommended to use the latest version to experience the latest features:
Version selection
You are advised to download the executable file for Windows and Mac platforms, and download the compressed file for Linux.
Run the installation after downloading:
Note: The installation directory can be easily found by yourself.
Run CMD on the PC and enter Go version to check whether the installation is successful.
Under Linux installation
If you do not need to type go code on Linux platform, there is no need to install Go on Linux platform. The GO code written on our development machine only needs cross-platform compilation (see cross-platform compilation at the end of the article), and then you can copy it to run on Linux server. This is also the advantage of easy cross-platform deployment of GO program.
We have selected and downloaded the go1.15.2.linux-amd64.tar.gz file from the version selection page:
$wget HTTP: / / https://dl.google.com/go/go1.15.2.linux-amd64.tar.gzCopy the code
Unzip the downloaded file to /usr/local:
$tar -zxvf go1.15.2.linux-amd64.tar.gz -c /usr/local # decompression
Copy the code
If no permission is displayed, add sudo as root and run again. After executing, you can see the go directory under /usr/local/.
Configure environment variables: In Linux, you can configure environment variables in two files. /etc/profile takes effect for all users. $HOME/. Profile is valid 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
Copy the code
Profile takes effect after you run the source command to load $HOME/. Profile after you modify $HOME/. Check:
$go Version $Go Version go1.15.2 Linux/AMD64Copy the code
2. Configuration GOPATH
Both GOROOT and GOPATH are environment variables, where GOROOT is the path where we install the Go development package. Starting with Go 1.8, the Go development package sets a default directory for GOPATH after installation, as shown in the table below.
Default values for GOPATH on different operating system platforms
platform | GOPATH default values | For example, |
---|---|---|
Windows | %USERPROFILE%/go | C:\Users\ user name \go |
Unix | $HOME/go | /home/username /go |
Here I choose to create GOPATH under drive D on my computer: D: \ GoLang \ GoLang – the workspace
GOROOT is located where its own GO development kit is installedE: \ Go
Add native environment variables:
3. Create a new Go project using Goland
Create a project:
Create the bin SRC PKG folder
Bin: used to store the compiled.exe file. SRC: Used to store the development files of our project. Create the project and project files in SRC
Create projects and files
Modify runtime configuration information
Run Kind: Selects Directory
Directory: Specifies the project Directory
Out Directory: Specifies the output path of the compiled file
WOrking Directory: Specifies the path of the workspace
The results
package main // Declare the main package, indicating that it is currently an executable program
import "fmt" // Import the built-in FMT package
func main(a){ // The main function is the entry point for program execution
fmt.Println("Hello Golang!") // 在终端打印 Hello Golang!
}
Copy the code
Matters needing attention:
# main is the entry to the program. Pakage must be main
Copy the code
4. GOPROXY
After go 1.14, it is recommended to use the Go mod mode to manage dependencies. It is no longer mandatory to write code in the SRC directory under GOPATH. You can write go code anywhere on your computer. (There are some online tutorials available prior to version 1.11.)
The default GoPROXY configuration is: GOPROXY=https://proxy.golang.org, direct, due to can’t access https://proxy.golang.org in China, so we need to change a PROXY, https://goproxy.io or https://goproxy.cn are recommended.
You can run the following command to modify GOPROXY:
go env -w GOPROXY=goproxy.cn,direct