1. GOROOT introduction
When the Golang language is installed, the so-called installation path is actually your GOROOT
path, that is GOROOT
, the location where the built-in Golang language library is stored, and usually after you install it, the environment variables of your computer will be set GOROOT
Path. When you develop a Golang program, when you have a import
built-in library, no additional installation is required. When the program is run, it will first go to the GOROOT
path to find the corresponding library to run by default .
1. View Glang environment variables
go env
env
Some of them are modified by me. Usually if you install Golang for the first time and do not make any environmental variable settings, the GOROOT
setting path is the path where you originally installed Golang, and GOPATH
usually the default will be in the user directory go
Folder.
2. Use go run to execute a file
package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}
Then execute the following command:
go run main.go
It will be successfully output hello world
, go run
which will actually help you compile the code and generate an executable file. The compiled file and the executable file are actually stored in a temporary folder and will be automatically deleted when the program is run. This command can be run in a way similar to literal translation, without any other environment settings.
3. Use third-party kits
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.Run()
}
import
A github.com/gin-gonic/gin
package, this is a Web Framework package developed by others, does not exist in the official library, but a third-party package on GitHub.
- When the Golang code is executed, when the package needs to be accessed, it will first go
GOROOT
to thesrc
folder under the path to find the same asimport
the path in the code to find if there isgin
this folder, and this folder contains all the information about The library of the package. - If it
GOROOT
is not found in the path, it willGOPATH
look for the src folder under the path, which is equivalent toimport
the path we have in the code, to find if there isgin
this folder.
So long as the GOROOT
heel GOPATH
of the path have not found the suite, then you can not execute the code.
Two, GOPATH introduction
According to the introduction of GOROOT above, we can know that the official library is located in GOROOT, and GOPATH is dedicated to storing third-party packages for our code needs. If you develop Golang, you usually reset the GOPATH location.
1. GOPATH directory
bin
pkg
src
According to the convention of the Golang language (mandatory), GOPATH refers to the upper level of the src path. We need to actively add the src folder under the GOPATH path. The so-called src means source code, which is where we develop Golang code The source code of the related project.
2. Disadvantages of GOPATH
As long as the third-party packages are not official libraries, they need to be placed in GOPATH/src
the path before they can be used.
Go get is most commonly used when we want to use a package that others have published on GitHub, which can help us clone GOPATH/src
it from the Internet . Although this is very convenient, you will find GOPATH/src
that the code will be very complicated. In addition to the project folder developed by you, it also contains the project folder of other third-party libraries.
Next, what if you develop a project that uses a third-party suite of different versions? The previous solution was to set multiple sets of different GOPATH. Although the community has developed corresponding solutions package manager
, such as Vendor
, Dep
to solve this problem, it is not officially led.
Third, the birth of Go Modules
In order to solve the problem of not being GOPATH, the official launch of the function of Go Modules since 1.11. The solution to Go Modules is very similar to what Java saw in Maven. It stores third-party libraries in a local space and references them to the program.
1. Set GO111MODULE environment variables
There can be three different values:
- The
default value of auto , the go command will determine whether to enable the modules function according to the current directory. Needs to meet two situations:
the project directory is not inGOPATH/src/
the
current or previous directory files present go.mod - The on
go command will use modules instead of searching in the GOPATH directory. - The off
go command will not support the module function. The search package is the same as the previous GOPATH method.
I suggest that if you want to develop a Go project, you should no longer use the function of GOPATH, but use the approach of Go Modules, so it is recommended to set it to on.
With Go Modules, where are the third-party packages downloaded? It's actually in the GOPATH/pkg/mod
folder.
2. Initialize the mod
go mod init <module name>
<module name>
You can fill in or leave it blank, if you leave it blank, the default is to use the name of the project folder.
The following keywords can be written in this file:
- module
defines the module path - go
definition go language version - require
specifies the dependent package, the default is the latest version, you can specify the version number - exclude
exclude the package and its version - replace
use a different package version and replace the original package version - Annotation
// Single-line annotation
/* Multi-line annotation*/
indirect represents dependent packages that are indirectly imported
Suppose now I want to import gin-gonic/gin
the suite on GitHub , as defined below:
module awesomeProject
go 1.13
require github.com/gin-gonic/gin v1.6.3
Then execute the following command:
go mod download
The required packages will be installed in the GOPATH/pkg/mod
folder. And you will find a go.sum file, this file is basically used to record the relationship between the package version, to ensure that it is correct, it is not necessary to bother.
Official note: In addition to go.mod, the go command also maintains a file called go.sum, which contains the expected cryptographic hash of the content of a specific module version. The
go command uses the go.sum file to ensure future download and retrieval of these modules. Download the same bits at a time to ensure that the modules that the project depends on will not be accidentally changed, whether for malicious, accidental, or other reasons. Both go.mod and go.sum should be checked into version control.
go.sum does not require manual maintenance, so you don't need to pay too much attention.
If you change the gin version v1.4.0 and then re-run go mod download
, you will find in GOPATH/pkg/mod
which gin-gonic
there are two folders [email protected]
, [email protected]
thereby to distinguish between versions.
Of course, it is also not necessary to execute go mod download, and directly run go build or go install will automatically install the package in the corresponding place.
There is another way to download the package directly without defining it in go.mod:
go get github.com/gin-gonic/gin@v1.5.0
As long as there are open go modules
feature, go get
it will not be the same as in the previous GOPATH/src
case files placed kit, but will be placed GOPATH/pkg/mod
there, and go.mod
will write the introduction, so do not use it go mod download
commanded.