Analysis of the relationship among root, gopath and go modules


1、 Introduction to root

After installing the golang language, the so-called installation path is actually yoursGOROOTPath, that is to sayGOROOTThe location of the built-in Library in the golang language. Usually, after you install it, the environment variables of your computer will be setGOROOTPaths, when you develop golang programs, when youimportThere is no need to install the built-in library. When the program is running, the default will go firstGOROOTPath to find the corresponding library to run.

1. View glang environment variables

go env


envSome of them have been modified by me. Usually, if you are installing golang for the first time and do not set any environment variables,GOROOTSetting the path is the path where you installed the golang language, andGOPATHUsually the default is in the user directorygoFolder.

2. Use go run to execute a file

package main

import (

func main() {
	fmt.Println("hello world")

Then execute the following command:

go run main.go

It will output successfullyhello worldgo runIn fact, it will help you to compile the code and generate an executable file. In fact, the compiler and the executable file are stored in a temporary folder. After running this program, it will be deleted automatically. The instruction can be run like literal translation without any other environment settings.

3. Reference to third party kits

package main

import (

func main() {
	router := gin.Default()

importI got is a web framework package developed by others. It does not exist in the official library, but is a third-party package on GitHub.

  • When you run golang code, when you need to access a package, you go firstGOROOTUnder the pathsrcFolder lookup is the same as we are in codeimportGo down the path to find out if there is anyginThis folder contains all the libraries related to the package.
  • If inGOROOTIf it is not found in the path, it will go toGOPATHThe SRC folder in the path is the same as our codeimportGo down the path to find out if there is anyginThis folder.

So as long asGOROOTWithGOPATHIf the package is not found in the path, the code cannot be executed.

2、 Gopath introduction

According to the above introduction of goroot, we can know that the official library is located in goroot, and gopath is dedicated to storing third-party packages for our code needs. When developing golang, you usually reset the location of gopath.

1. Gopath directory


According to the Convention of golang language (mandatory), gopath refers to the upper layer of the SRC path. We need to actively add a SRC folder under the gopath path. The so-called SRC represents the source code, that is, the source code of the project that we develop the golang code.

2. Disadvantages of gopath

Third party packages need to be placed in theGOPATH/srcCan only be used under the path of.

Go get is most commonly used when we want to use the kit that others have exposed on GitHub, which can help us clone from the InternetGOPATH/srcInside. It’s convenient, but you’ll find thatGOPATH/srcThe code will be very complex. In addition to your own project folder, it also contains other project folders of equation 3 library.

Then, what if you develop projects with different versions of third-party packages? The previous solution is to set different gopaths. Although the community also has corresponding development strategiespackage manager, such asVendorDepTo solve the problem, but after all, it is not officially led.

3、 The birth of go modules

In order to solve the problem of not being used by gopath, the official launched the go modules function on 1.11. The go modules solution is similar to the way Java sees Maven. It stores the third equation Library in the local space and gives it to the program to refer to.

1. Setting go111module environment variables

There are three different values:

  • auto

Whether the go command is enabled depends on the default directory. Two situations need to be met:
The project directory is not availableGOPATH/src/lower
The current or previous directory exists go.mod archives

  • on

The go command uses modules instead of searching in the gopath directory.

  • off

The go command will not support the module function. Look for the suite as gopath did before.

I suggest that gopath should not be used to develop go projects, but go modules should be used instead. Therefore, it is recommended to set it to on.
With go modules, where are the downloaded third-party packages? It’s actually in placeGOPATH/pkg/modIt’s in the folder.

2. Initialize Mod

go mod init <module name>

<module name>It can be filled in or not. If not, the default is to use the name of the project folder.

The following keywords can be written in this file:

  • module

Define module path

  • go

Define go language version

  • require

Specify the dependent suite. The default is the latest version. You can specify the version number

  • exclude

Exclude the kit and its version

  • replace

Use a different KIT version and replace the original kit version annotation
//Single line annotation
/*Multiline annotation*/
Indirect represents the dependency package that is indirectly imported

Let’s say I’m going to introduce thegin-gonic/ginThe definition of the kit is as follows:

module awesomeProject

go 1.13

require v1.6.3

Then execute the following command:

go mod download

The required kits will be installed in theGOPATH/pkg/modIt’s in the folder. And you’ll find one go.sum This file is basically used to record the relationship between suite versions to ensure that it is correct and does not need much attention.

Official note: except go.mod In addition, the go command maintains a go.sum File containing the expected encrypted hash of the contents of a specific module version
Go command use go.sum Files ensure that future downloads of these modules retrieve the same bits as the first download to ensure that the modules on which the project depends do not change unexpectedly, whether for malicious, accidental, or other reasons. go.mod and go.sum Version control should be checked in.
go.sum It doesn’t need manual maintenance, so you don’t need to pay much attention to it.

If the gin version is changed to v1.4.0, then execute it againgo mod downloadAnd you’ll find outGOPATH/pkg/modinsidegin-gonicThere will be two folders[email protected][email protected]To distinguish versions.

Of course, you can also run go build or go install without go mod download, and the package will be automatically installed in the corresponding place.

There is also a way to download the package directly instead of downloading it in the go.mod Definition in

go get[email protected]

As long as it’s opengo modulesFunction,go getIt’s not going to be like beforeGOPATH/srcThe package file will be placed in theGOPATH/pkg/modInside, andgo.modI will write the introduction, so I don’t need to use itgo mod downloadI’ve got the order.

So far, this article about the relationship among root, gopath and go modules is introduced here. For more information about the content of root, gopath and go modules, please search the previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!

Recommended Today

Explain singleton methods and singleton classes in Ruby

singleton method Ruby allows you to add methods to a single object. This method that works only for a single object is called a singleton method Sample code ? 1 2 3 4 5 6 7 8 9 10 11 12 str = “just a regular string”   def str.title?   self.upcase == self end   […]