Go Commands


$ go
Go is a tool for managing Go source code.


        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildmode   build modes
        c           calling between Go and C
        cache       build and test caching
        environment environment variables
        filetype    file types
        go.mod      the go.mod file
        gopath      GOPATH environment variable
        gopath-get  legacy GOPATH go get
        goproxy     module proxy protocol
        importpath  import path syntax
        modules     modules, module versions, and more
        module-get  module-aware go get
        module-auth module authentication using go.sum
        module-private module configuration for non-public modules
        packages    package lists and patterns
        testflag    testing flags
        testfunc    testing functions

Use "go help <topic>" for more information about that topic.



# 初始化生成 go.mod 和 go.sum 文件
$ go mod init github.com/xxx/xxx

# 添加需要用到但 go.mod 中查不到的模块,删除未使用的模块
$ go mod tidy

# 强制将包替换成指定版本
$ go mod edit -replace xxx/xxx/xxx=xxx/xxx/[email protected]
# 会在 go.mod 生成如下语句
$ replace xxx/xxx/xxx => xxx/xxx/xxx v0.1.1


# 会更新指定的包版本和所有依赖该包的版本
$ go get -u github.com/pkg/errors

# 更新 go.mod 中依赖的所有的包
$ go get -u

# 更新指定包到特定的版本
$ go get [email protected][@version]
# version支持的格式:
# 1. vx.y.z
$ go get -u xxx/pkg/[email protected]
# 2. commit的checksum
$ go get xxx/pkg/[email protected]
# 3. master, latest
$ go get  xxx/pkg/[email protected]

Go 的编译命令执行过程