In Golang, there are two concepts that are very confusing. The first is GoPath, and the second is GoModule. Many beginners don’t understand the relationship between the two concepts, which makes it difficult to understand the overall structure of the project and write clean code.
What is a GoPath?
What is a Gopath? In my last blog post on Golang environment Installation &IDEA Development Golang, I mentioned the concept of GoPath. GoPath is Golang’s workspace, and all Go files need to be compiled and run in the SRC directory under GoPath, so I recommend not configuring the global GoPath directory directly, otherwise it will be very difficult to manage all Golang projects.
However, as I mentioned in Golang connecting to MySQL DATABASE CRUD, when we use third-party libraries in our project, we can use the Go get command to pull the packages directly from the network, and then download the packages directly to our GoPath directory SRC.
This led to the problem that our own Golang code got mixed up with third-party Golang files, which was obviously cumbersome for us to manage the Golang project package, and if every project required the same dependencies, Then we would download a lot of duplicate third-party dependencies in different GoPath SRC, which would also take up a lot of disk space.
We set different GoPath for different projects, and the advantages are obvious:
Easy to manage projects, each project is a different GoPath, which allows us to manage multiple Golang projects with a very clear project structure. If we put all of our projects under the same GoPath SRC package, the project structure would become very confusing and unmanageable.
But the disadvantages of having different GoPath Settings for different projects are obvious when relying on third-party packages:
- Third party dependent packages mixed with our own Golang package caused some problems for our project file management.
- All the different GoPath dependencies need to be downloaded, so the number of duplicate dependencies on disk can be very large and take up a lot of disk space.
So it’s a moot point whether to have a single GoPath directory to solve the problem of duplicate dependencies, or a different GoPath directory to solve the problem of disorganized Golang projects.
To solve all these problems, Golang eventually introduced the concept of GoModule.
What is GoModule?
GoModule is a concept that Golang introduced in 1.11 and was introduced in 1.12, so if you want to use GoModule, make sure Golang is 1.12 or above. Golang1.11 and 1.12 have introduced the GoModule concept, but GoModule is not enabled by default. To enable GoModule, you need to configure an environment variable: GO111MODULE=on, which is off by default.
In Golang1.13 and above, GoModule defaults to auto, meaning GoModule determines whether to enable GoModule based on whether you have the go.mod file in your directory. So in Golang1.13+ we don’t need to configure the GO111MODULE property. So if you use GoModule, go straight to Golang1.13+!
So what exactly is GoModule?
- To put it bluntly, GoModule is a workspace that replaces GoPath’s Golang.
As mentioned earlier, all Golang files need to be compiled and run in the GoPath directory. With GoModule, we can place Golang files in the GoModule directory. Golang files in the GoModule directory also compile and run correctly.
So when we have GoModule, can GoPath be discarded?
No!
As mentioned earlier, the problems GoPath caused were caused by third-party library packages, so once we had GoModule, GoPath and GoModule took on different responsibilities for our Golang project.
GoPath is where we store third-party dependencies that we pull from the web. The 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 GoPath SRC package via a go.mod file in the GoModule directory.
In this way, it not only solves the problem of programming in GoPath directory SRC package, but also solves the problem of difficult management of third-party dependency packages and occupying disk space by repeated dependency.
All in all, after introducing GoModule, instead of programming directly in the GoPath directory, we use GoPath as a repository for third-party dependencies, and our real workspace is in the GoModule directory.
The setting of GoModule
Now that we know the difference between GoPath and GoModule, how can GoModule be configured? A directory is a GoModule directory.
It’s very simple. We just use itGo mod init Module name
Command to initialize the directory and set it to the GoModule directory. We are inF:\GoModule
Create a folder under the directory named:go_module
. Then use the CMD command prompt to enter the directory and runGo mod init Module name
Initialization command.After the initialization command is executed, a go.mod file is generated in the go_module directory, which is used to import third-party dependencies in the GoPath directory.
The go.mod file after initialization
module go_module
go 1.14
Copy the code
When importing third-party dependencies from the GoPath directory, the GoModule will automatically download them to the GoPath directory by adding the dependency names to the go.mod directory.
For example, the following go.mod file:
module go_module_demo
go 1.14
require (
github.com/astaxie/beego v112.1.
github.com/go-sql-driver/mysql v1. 5. 0
)
Copy the code
We have introduced two dependencies in the go.mod file: Beego framework v1.12.1 and mysql driver v1.5.0.
- If we use the IDE for development, we can create a GoModule project. In this way, the IDE will automatically generate the required files for us. In addition, the IDE will automatically add and download dependencies when using the dependent packages, which saves a lot of time. And you don’t have to remember the exact package name and version number of the dependency.
The GoModule cannot download foreign dependency packages
This is a problem that many developers have encountered, and it’s obvious that foreign dependencies can’t be downloaded directly from the web, so Golang has also introduced another feature: GOPROXY: Configure GOPROXY=https://goproxy.io in the environment variable to solve the problem that the GoModule cannot download foreign dependent packages. Of course, it can also be configured through the IDE, which saves the configuration of environment variables in the computer system too much to manage.
Configure GOPROXY properties in IDEA: