Home  >  Article  >  Backend Development  >  How to modularize golang

How to modularize golang

(*-*)浩
(*-*)浩Original
2019-12-31 09:18:253559browse

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 path

1.5 and In the above version, the dependent organization name was added

vgo was proposed as a prototype for Go module support

In 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 run

Module structure

The module is a directory tree containing Go source files, and a file named go.mod is added to the root directory

go.mod contains module imports Name, declares required dependencies, excluded dependencies and replaced dependencies

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn