Go module learning notes


A common command of go module

Module maintenance:go mod command arguments
Create module: go mod init example.com/hello
Remove useless dependency: go mod tidy

Test packages:go test [build/test flags] [packages]
Test current directory*_ test.go : go test run the_ test.go file

List packages or modules:
go list [-f format] [-json] [-m] [list flags] [build flags] [packages]

List all the dependencies (including indirect dependencies) of the module: go list – m all

Add and install dependencies:
go get [-d] [-t] [-u] [-v] [-insecure] [build flags] [packages]

Upgrade minor version get packages

Upgrade depends on the major version of module:
Add semantic major version number to import, such as import quotev3“ rsc.io/quote/v3 “

2. Release go module

Semantic version:
Semantic version by vMAJOR.MINOR.PATCH When the public API of the newly released version is not compatible with the published version, the major version number of major will be added; when the newly added function is compatible with the published version, minor will be added; when the API and dependency are not changed, for example, bug repair and patch will be added.
V0 is the initial unstable version and V1 is the first stable version.
Mark the local branch. The default label is the latest commit: git tag v0.1.0
Push the local branch label to the far end: git push origin v0.1.0

Third, release V2 and above

The V2 and above versions of the module must have different module import paths with major version.
Create a new V2 subdirectory under the module, cuff all *. Go files under the module into the V2 directory, and go.mod Copy to the V2 directory and modify the module name.
Modify module name:
go mod edit -module github.com/killianxu/gopher/v2 v2/go.mod

Change all the modules that our project depends on to V2 version:
find . -type f -name ‘*.go’ -exec sed -i -e ‘s,github.com/my/project,github.com/my/project/v2,g’ {} \;
Find to find the specified file,. Represents the directory and subdirectory, – type represents the file type (f ordinary file), – name represents the file name, – exec execute instruction, – exec and {} combine to represent all the matching files [5].
Sed is a stream editor for text processing. Sed reads the file into the buffer, outputs it to the screen after processing, and then processes the next line.
Command usage: sed [options]’command ‘file (s)
-E can execute multiple commands in the same line, – I can change the original file directly. In command, s means replace, and G means replace all lines.
git tag v2.0.0
Push local branch label to remote end: git push origin v2.0.0


[1] Using Go Modules.https://blog.golang.org/using-go-modules
[2] Command go.https://golang.org/cmd/go/
[3] Publishing Go Modules.https://blog.golang.org/publishing-go-modules
[4] Go Modules: v2 and Beyond.https://blog.golang.org/v2-go-modules
[5] Find command https://man.linuxde.net/find
[6] The SED command https://man.linuxde.net/sed


Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]