Golang development environment construction
Go language development kit
Abroad: https://golang.org/dl/
Domestic (recommended): https://golang.google.cn/dl/
editor
- Golang:https://www.jetbrains.com/go/
- Visual Studio Code: https://code.visualstudio.com/
To set up a Go language development environment, you need to download the Go language development kit first.
View operating system and version
Windows:
Mac:
Linux:
Terminal input uname -a
, examples are as follows
Golang language development kit
The MSI installation method is relatively simple and recommended
This method is recommended on Windows systems. The current operating systems are basically 64-bit, so choose the 64-bit go1.15.windows-amd64.msi to download, if the operating system is 32-bit, choose go1.15.windows-386.msi to download .
After downloading, double-click the MSI installation file and follow the prompts to install it step by step. By default, the Go language development kit will be installed to the c:\Go directory. You can also choose the directory you want to install during the installation process.
Assuming it is installed in the c:\Go directory, the installer will automatically add c:\Go\bin to your PATH environment variable. If not, you can go through the system -> control panel -> advanced -> environment variable option. add manully. The example is as follows
- enter
- Configuration
Install under macOS
If your operating system is macOS, you can use the PKG installation package. Download go1.15.darwin-amd64.pkg
later, double-click follow the prompts to install. After the installation is successful, the path /usr/local/go/bin
should have been added to the PATH environment variable.
If not, you can add it manually by adding the following content to the /etc/profile or $HOME/.profile file and save it. The command is as follows
export PATH=$PATH:/usr/local/go/bin
BrewHome installation
# brewHome安装
brew install golang
Linux
- Protection management tool installation
# yum(Centos)
sudo yum -y update # 更新yum
sudo yum install -y golang
# apt(ubantu)
sudo apt -y update
sudo apt install -y golang
- Compressed package installation (please check the operating system version before installation)
# amd 版本
wget https://golang.google.cn/dl/go1.15.4.linux-amd64.tar.gz
# arm版本
https://golang.google.cn/dl/go1.15.4.linux-arm64.tar.gz
# 解压(amd)
sudo tar -C /usr/local/ -xzvf go1.15.4.linux-amd64.tar.gz
# 解压
sudo tar -C /usr/local/ -xzvf go1.15.4.linux-arm64.tar.gz
# 环境变量配置
# sudo vim /etc/profile
export GOROOT=/usr/local/go
export GOPATH=/home/bruce/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
# 环境变量生效
source /etc/profile
GOROOT
Set the installation location of the golang development package, we unzip it to the/usr/local/
directory, the folder under this directorygo/
must be the root directory of the go environment, that isgo
, don't have anothergo/
directory after opening the directory.
GOBIN
Directory is the implementation ofgo install
an executable file of the directory
GOPATH
It is our working directory. Generally, we set it to the user directory. This should be configured according to the actual situation of your computer. Introducing the working directory structure of go. There are 3 subdirectories under the working directory we set
Test installation
# 任意目录下输入go version
go version go1.15.2 darwin/amd64
# 查看环境配置
go env
# go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/stringle-004/Library/Caches/go-build"
GOENV="/Users/stringle-004/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/stringle-004/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/stringle-004/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go" # golang开发包的安装位置
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/stringle-004/go/src/github.com/payne/awesomeProject/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sb/__n99hm13ms08lkk2lv5pxj40000gn/T/go-build659271527=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOPROXY
After Go1.14 version, it is recommended to use the go mod
mode to manage the dependent environment, and we no longer force us to write the code in GOPATH
the src directory below. You can write go code anywhere on your computer. (Some tutorials on the Internet are applicable before version 1.11.)
The default GoPROXY configuration is:, GOPROXY=https://proxy.golang.org,direct
because it is not accessible in China https://proxy.golang.org
, we need to change to a PROXY, https://goproxy.io
or is recommended here https://goproxy.cn
.
You can execute the following command to modify GOPROXY:
go env -w GOPROXY=https://goproxy.cn,direct
Cross-platform compilation
Another powerful feature of the Go language development kit is that it can be compiled across platforms. What is cross-platform compilation?
It is developed on macOS, you can compile executable programs on platforms such as Linux and Window, so that the programs you develop can run on these platforms. In other words, you can choose your favorite operating system for development, and cross-platform compilation into an executable program that needs to be released on the platform.
Go language by two environment variables to control cross-platform compilation, they are GOOS
and GOARCH
.
-
GOOS: Represents the target operating system to be compiled. Common ones are Linux, Windows, Darwin, etc.
-
GOARCH: Represents the target processor architecture to be compiled, common ones are 386, AMD64, ARM64, etc.
In this way, different executable programs can be compiled by combining different GOOS and GOARCH. For example, my current operating system is macOS AMD64. I want to compile an executable program for Linux AMD64. I only need to execute the go build command, as shown in the following code:
For more combinations of GOOS and GOARCH, please refer to the $GOOS and $GOARCH section of the official document.
GOOS=linux GOARCH=amd64 go build /Users/stringle-004/go/main.go
For more combinations of GOOS and GOARCH, please refer to the $GOOS and $GOARCH section of the official document .
Go editor
The first one is the Visual Studio Code + Go extension plug-in, which allows you to develop very efficiently. Download the corresponding operating system version of Visual Studio Code through the official website .
- Installation: Slightly (enter the official website, download and install)
- Visual Studio Code + Go settings
Install Chinese Simplified Plugin
Click the last item 管理扩展
in the menu bar on the left 搜索框
, enter it chinese
, select the first item in the result list, and click install
Install.
After the installation is complete, there will be a prompt in the lower right corner 重启VS Code
, and your VS Code will display Chinese after restarting!
VSCode
Main interface introduction:
Install go extension
Now we have to install Go
extension plug-ins for our VS Code editor to support Go language development.
Install Chinese Simplified Plugin
Click the last item 管理扩展
in the menu bar on the left 搜索框
, enter it chinese
, select the first item in the result list, and click install
Install.
After the installation is complete, there will be a prompt in the lower right corner 重启VS Code
, and your VS Code will display Chinese after restarting!
The second is Goland launched by JetBrains, an established IDE company. All plugins have been integrated, easier to use, and powerful, suitable for novices and veterans. You can download and use it through the official website https://www.jetbrains.com/go/ .
The first Golang program
package main # 可执行文件必须为package main开头
import "fmt" # 导入“fmt”包,用于打印(fmt.Print(打印)、fmt.Println(换行打印)、fmt.Printf(格式化打印)等)
func main() { # 主函数main
fmt.Print("你好,我叫payne") # 输入
}
# 你好,我叫payne
Want to know the author, more articles, stay tuned: Accumulate Coder