An overview of the
In the process of writing code, there are always references to other libraries and dependencies on functional modules. Go began providing support for Module modularity in version 1.1. All users are encouraged to migrate to modules from other dependency management systems after version 1.4
The module
concept
A “module” is a collection of Go packages that are versioned together as a whole.
Modules document the correct dependencies and dependency needs, and are handy to build.
Summarize the relationship between project engineering warehouse, modules and packages:
- Project engineering repositories can contain one or more Go modules.
- Each module can contain one or more Go packages.
- Each package consists of one or more Go source files in a single directory.
The module must have a version number in the format v(Major).(Minor).(patch), for example, V0.1.0, v1.2.3, or V1.5.0-RC.1.
The go.mod file is located at the root of the project, and the project doesn’t have to be in the GOPATH directory.
Initialize a module
To enable module support in a project, you need to manually enable it.
Command format: go mod init Module name
go mod init mymod
Copy the code
When executed in the command line window, a go.mod file will be produced in the current directory with the following contents:
The module mymod go 1.14Copy the code
Module application update
In Goland, after the above modules are configured. The go.mod file will be automatically updated if a third party module is imported into the code, such as when we add it to the code
Func main(){FMT.Print(" XXXXX ") FMT.Println(quote.Hello()) // If I add a new} hereCopy the code
So, when you run or synchronize a project, you’ll notice that go.mod has changed as follows:
Module mymod go 1.14 require rsc. IO /quote v1.5.2Copy the code
Note the failure to update modules: Agents need to be added due to network differences. For Goland IDE, open Preferences, Go > Go Modules(vgo), write the proxy address of goproxy.cn to proxy, and check Enable Go Modules.
image.png
To learn more, read github.com/golang/go/w…
END