Home > Article > Backend Development > How to modularize golang
go module is implemented in go1.11, you can use the officially recommended modular programming solution
##go module Definition (Recommended learning: go)
A module (module) is a collection of related go packages and is the basis for source code interchange (interchange) and version control Unit
Brief History
In versions of go before 1.5, the go get command will download the package to the GOPATH path1.5 and In the above version, the dependent organization name was addedvgo was proposed as a prototype for Go module supportIn Go 1.11 (beta) version, vgo was merged into the main code and improved to go mod ( Experimental)Terms
「Module root」(Module root): A directory containing a file named go.mod「Module "Module path": The prefix of the import path corresponding to the module root directory "Main module" (Main module): The module that packages the directory where the go command is runModule structure
The module is a directory tree containing Go source files, and a file named go.mod is added to the root directorygo.mod contains module imports Name, declares required dependencies, excluded dependencies and replaced dependenciesThe following code is a simplified go.mod content
module my/thing require ( one/thing v1.3.2 other/thing v2.5.0 // indirect ... ) exclude ( bad/thing v0.7.3 ) replace ( src/thing 1.0.2 => dst/thing v1.1.0 )It should be noted that the dependencies declared in this file will not be automatically imported using import in the source code of the module, but we still need to manually add import statements to import
The above is the detailed content of How to modularize golang. For more information, please follow other related articles on the PHP Chinese website!