Golang | Introduction to the relationship between GOROOT, GOPATH, and Go-Modules-

1. GOROOT introduction

When the Golang language is installed, the so-called installation path is actually your GOROOTpath, 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 GOROOTPath. When you develop a Golang program, when you have a importbuilt-in library, no additional installation is required. When the program is run, it will first go to the GOROOTpath to find the corresponding library to run by default .

1. View Glang environment variables

go env

Environment variable
envSome of them are modified by me. Usually if you install Golang for the first time and do not make any environmental variable settings, the GOROOTsetting path is the path where you originally installed Golang, and GOPATHusually the default will be in the user directory goFolder.

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 runwhich 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()
}

importA github.com/gin-gonic/ginpackage, 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 GOROOTto the srcfolder under the path to find the same as importthe path in the code to find if there is ginthis folder, and this folder contains all the information about The library of the package.
  • If it GOROOTis not found in the path, it will GOPATHlook for the src folder under the path, which is equivalent to importthe path we have in the code, to find if there is ginthis folder.

So long as the GOROOTheel GOPATHof 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/srcthe 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/srcit from the Internet . Although this is very convenient, you will find GOPATH/srcthat 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, Depto 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 in GOPATH/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/modfolder.

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/ginthe 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/modfolder. 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/modwhich gin-gonicthere 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 modulesfeature, go getit will not be the same as in the previous GOPATH/srccase files placed kit, but will be placed GOPATH/pkg/modthere, and go.modwill write the introduction, so do not use it go mod downloadcommanded.

Guess you like

Origin blog.csdn.net/y1534414425/article/details/108767310