Article Directory
Vscode builds go language development environment
interpreter
Download official website: Downloads - The Go Programming Language (google.cn)
Fool-style installation, just remember the installation path
Test whether the installation is successful in the terminal:
bin: store compiled binary files
**pkg: store compiled package files. The package file extension is usually xxx.a, which ends with .a**.
src: store the source code of the Go language package
Plug-ins that need to be installed in vscode
Configure user variables and environment variables (important)
environment variable
Put the bin directory of the go interpreter installation location in the path (insert one in the Path in the system variable, Golang installation directory/bin)
user variables
add user variables
The GOPATH variable sets the user working directory. If you need to use a third-party library when writing code , and then use go get xxx, xxx will be downloaded to this directory.
Delete GOPATH in \User Variables\ that comes with the system. Because the original path C:\Users\xxx\go is not where we store the go code.
变量 GOPATH 值 D:\interpreter\go\wrokspace
(Fill in your Golang working directory here to save the corresponding downloaded package
After adding, use it on the command line go get -u xxx
to install third-party libraries
go get -u gorm.io\gorm
After using the above command, you can find a gorm.io folder in the D:\interpreter\go\bin\pkg\mod directory, and there is a gorm folder below
The GOROOT variable sets where the Go compiler is installed
变量 GOROOT 值 D:\interpreter\go
(Fill in your Golang installation directory here)
Configure environment variables for go
Use the command on the command line go env
to see all the environment variables about go. (Check Golang's environment)
Since go is a product of google, many plug-ins are located abroad, but they cannot be accessed in China, so you need to set up proxy and other configurations
So we set the following parameters
go env -w GOPROXY=https://goproxy.io,direct
go env -w GOPRIVATE=*.corp.example.com
go env -w GO111MODULE=on
Problems that may be encountered in those vscode
For example, an error is reported: (go: cannot find GOROOT directory: c:\go)
The way to open the vscode terminal shortcut is:ctrl+shift+tab上那个键
If you have installed golang before and used it in vscode
Maybe when you install vscode once, you will find GOROOT according to the original path
As shown in the figure, it is not successful to check the go language environment for the first time, but set GOROOT=D:\interpreter\go
it is successful to manually set GOROOT with the command
In the same way, GOPATH can be set: (as shown below)
GO111MODULE
-
GO111MODULE Introduction
GO111MODULE has three values: off, on and auto (default):
- GO111MODULE=off
The go command line will not support the module function, and the way to find dependent packages will follow the old version to search through the vendor directory or GOPATH mode. - GO111MODULE=on
The go command line will use modules and will not search in the GOPATH directory. - GO111MODULE=auto (default value)
The go command line will decide whether to enable the module function according to the current directory. The following two situations will enable the module function:
a. The current directory is outside GOPATH/src and the directory contains the go.mod file
b. The current file is under the directory containing the go.mod file.
- GO111MODULE=off
-
Set GO111MODULE in the Windows system to
directly add a variable in the environment variable, the variable name is GO111MODULE, and the variable value is one of on, off, and auto. -
Use go env -w to set GO111MODULE
When the go version is greater than or equal to 1.13, you can directly use the go env -w command to set variables very conveniently. The go env -w command modifies variables at the user level, and the modified information is saved in the $HOME/.config/go/env file by default.
ps: For version 1.16.5, using go env -w may not take effect.
#查看GO111MODULE的当前值 go env #设置GO111MODULE的值 #开启模块支持,无论什么情况,go命令都会使用module go env -w GO111MODULE=on #关闭模块支持,无论什么情况,go命令都不会使用module go env -w GO111MODULE=off #默认值,go命令根据当前目录决定是否启用module功能 #即当前目录在GOPATH/src之外且该目录包含go.mod文件时开启module功能;否则继续使用GOPATH go env -w GO111MODULE=auto
GOPROXY
The most important thing here is to check the GOPROXY settings. The GO language is a product of Google, so if you don’t set a proxy, it will be difficult to install related plug-ins behind the proxy (even if you don’t have a ladder, you must set a proxy)!
Some proxies are available online:
go env -w GOPROXY=“https://goproxy.cn,direct”
go env -w GOPROXY=“https://goproxy.io”
go env -w GOPROXY=“https://mirrors.tencent.com/go/”
The function of the direct parameter is: when the https://goproxy.cn proxy can be accessed normally, the proxy will be given priority to download the dependency package. If the agent does not work or does not have this package, do not use the agent and go directly to the github source address of the package to download
GOPATH
The Go language is a compiled language, and there are many dependent packages that need to be developed and compiled in the packaging process. So we need to configure a workspace similar to workspace, that is, I define a directory and write the source code in this directory.
GOPATH environment variable: used to indicate the storage path (working directory) of the go project you wrote.
Create three new directories src, bin, and pkg where you want the project. The effect is as follows
src: store source code
bin: store compiled binary files
pkg: store compiled package files
Finally, create a new folder in src, open it with vscode, and directly open the terminal go mod init+项目名
to execute it to write code (note that it is in the case of GO111MODULE=on)
After initialization it looks like this:
Compile and execute
Compile first and then execute
compile
implement
direct execution
gin framework construction
-
Develop a good habit of initializing the project before writing the project
go mod init 项目名 go mod tidy
-
What the go mod tidy command does:
-
Add modules that need to be used but cannot be found in go.mod,
-
remove unused modules
-
Note that it should be in the same path as go mod&go sum when executing
-
-
Install the Gin framework
go get -u github.com/gin-gonic/gin
The .sum file appears in your folder, indicating that the installation is successful. Next, we write a simple program to verify whether it is successful
package main import "github.com/gin-gonic/gin" func main() { // 创建一个默认的路由引擎 r := gin.Default() // 配置路由 r.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{ // c.JSON:返回 JSON 格式的数据 "message": "Hello world!", }) }) r.Run() // 启动 HTTP 服务 } # go run main.go后打开127.0.0.1:8080成功显示Hello world
-
go mod vendor
The function is to automatically write the new dependency package into the vendor directory of the current project.