GoPath and GoModule in Golang

In Golang, there are two concepts that are very easy to get wrong. The first is GoPath and the second is GoModule. Many beginners are not clear about the relationship between the two, and it is difficult to clearly understand the overall structure of the project. It is naturally difficult to write clearly structured code.

What is GoPath?

What is Gopath? In my previous blog, Golang Environment Installation & IDEA Development Golang , the concept of GoPath was mentioned.
GoPath is the working space of Golang. All Go files need to be placed in the src directory under GoPath to be able to compile and run, so I propose not to directly configure the global GoPath directory, otherwise it will be very difficult to manage all Golang projects.

But in another blog, Golang, CRUD connecting MySQL database , I also mentioned that when we use a third-party library in the project, we can use the go getcommand to directly pull the package of the third-party library from the Internet, and pull The removed package will be directly downloaded to the src package in our GoPath directory.

This leads to a problem. Our own Golang code is mixed with third-party Golang files. This is obviously very troublesome for us to manage the Golang project packages, and each if the project needs the same dependencies, then we A large number of repeated third-party dependency packages will be downloaded in different GoPath src, which will also take up a lot of disk space.

We set different GoPaths for different projects, the advantages are very obvious:

It is easy to manage projects. Each project is a different GoPath. For us to manage multiple Golang projects, we can handle the project structure very clearly. If we put all the projects under the same GoPath src package, the project structure becomes very confusing and difficult to manage.

But when we need to rely on third-party packages, the disadvantages of setting different GoPath for different projects are also very obvious:

  1. Mixing third-party dependent packages with our own Golang packages will cause some trouble to our project file management.
  2. Different GoPaths need to download dependencies, so there will be a lot of duplicate dependencies on the disk, which will occupy a lot of our disk space.

Therefore, whether to set up a GoPath directory to solve the problem of reliance on duplication, or to set up a different GoPath directory to solve the problem of chaotic Golang project structure is a controversial issue.

In order to solve all these problems, Golang finally introduced the concept of GoModule.

What is GoModule?

GoModule is a concept initially introduced by Golang in version 1.11. It was started in version 1.12, so if you need to use GoModule, you need to ensure that your Golang version is 1.12 or above.
In addition, although Golang 1.11 and 1.12 have introduced the concept of GoModule, GoModule is not enabled by default. If you need to enable it, you need to configure an environment variable:, the GO111MODULE=ondefault is off.

In Golang 1.13 and above, the default configuration of GoModule is auto, that is, GoModule will determine whether to open GoModule according to whether there is a go.mod file in your directory. Therefore, in the Golang 1.13+ version, we do not need to configure the GO111MODULE attribute.
So if you use GoModule, then just use Golang 1.13+ version!

So what is GoModule?

  • In fact, to put it bluntly, GoModule is a Golang workspace used to replace GoPath.

As we said before, all Golang files need to be placed in the GoPath directory for proper compilation and operation. With GoModule, then we can put the files in the GoModule directory and in the GoModule directory The Golang files can also be compiled and run correctly.

So after we have GoModule, can GoPath be abandoned?

no!

As we said before, the problem caused by GoPath is caused by the package of the third-party library, so after we have GoModule, GoPath and GoModule are respectively responsible for different responsibilities and jointly serve our Golang project.

We use GoPath to store third-party dependency packages that we pull from the Internet.
GoModule is used to store our own Golang project files . When our own projects need to rely on third-party packages, we can refer to the third-party dependencies under the GoPath directory src package through a go.mod file in the GoModule directory. .

This dependency not only solves the problem of being limited to programming under the src package in the GoPath directory, but also solves the problem of third-party dependency packages that are difficult to manage and repeatedly rely on taking up disk space.

All in all, after the introduction of GoModule, we will not directly program in the GoPath directory, but use GoPath as a repository of third-party dependent packages, and our real workspace is in the GoModule directory.

GoModule settings

Now that you have figured out the difference between GoPath and GoModule, how do you configure GoModule? How can a directory be considered a GoModule directory.

Very simple, we use the go mod init 模块名称command to initialize the directory directly , you can set this directory as the GoModule directory.
We F:\GoModulecreate a directory folder named: go_module.
Then enter the directory through the cmd command prompt and execute the go mod init 模块名称initialization command.
Insert picture description hereAfter the initialization command is executed, a go.mod file will be generated in the go_module directory. This file is used to import third-party dependencies in the GoPath directory.

Go.mod file after initialization

module go_module

go 1.14

When we need to introduce the third-party dependency package under the GoPath directory, we only need to add the dependency name under the go.mod directory, and GoModule will automatically help us download the third-party dependency package to the GoPath directory.

For example, the following go.mod file:

module go_module_demo

go 1.14

require (
	github.com/astaxie/beego v1.12.1
	github.com/go-sql-driver/mysql v1.5.0
)

We introduced two dependencies in this go.mod file, namely: beego框架 v1.12.1版本and mysql驱动 v1.5.0版本.

  • If we use the IDE for development, we can directly create a GoModule project, so that the IDE will automatically help us generate the required files, and when using the dependent packages, the IDE will automatically help us add dependencies and download dependencies , This will save us a lot of time, and you do n’t have to remember the specific package name and version number that you depend on.

GoModule cannot download dependency packages abroad

This is a problem that many developers have encountered. For foreign dependent packages, they cannot be downloaded directly through the network, which obviously makes developers very uncomfortable, so Golang also introduced another attribute: GOPROXY, we only need to be in the environment Configure GOPROXY = in the variable https://goproxy.ioto solve the problem that GoModule cannot download foreign dependent packages.
Of course, it can also be configured through the IDE, which saves too many environment variables in the computer system and is difficult to manage.

Configure GOPROXY properties in IDEA:

Insert picture description here

Published 85 original articles · praised 92 · visits 9217

Guess you like

Origin blog.csdn.net/qq_45193304/article/details/105491864