跳转至

Golang命令

1 go help

  • 功能:查看go帮助信息
  • 语法:`go help
使用go help可以列出所有可使用的topic
$ go help
Go is a tool for managing Go source code.

Usage:

        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
        work        workspace 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:

        buildconstraint build constraints
        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
        packages        package lists and patterns
        private         configuration for downloading non-public code
        testflag        testing flags
        testfunc        testing functions
        vcs             controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.
  • 示例:查看go和c调用帮助信息
$ go help c     
There are two different ways to call between Go and C/C++ code.

The first is the cgo tool, which is part of the Go distribution. For
information on how to use it see the cgo documentation (go doc cmd/cgo).

The second is the SWIG program, which is a general tool for
interfacing between languages. For information on SWIG see
http://swig.org/. When running go build, any file with a .swig
extension will be passed to SWIG. Any file with a .swigcxx extension
will be passed to SWIG with the -c++ option.

When either cgo or SWIG is used, go build will pass any .c, .m, .s, .S
or .sx files to the C compiler, and any .cc, .cpp, .cxx files to the C++
compiler. The CC or CXX environment variables may be set to determine
the C or C++ compiler, respectively, to use

2 go run

3 go build

https://blog.abnerzhao.com/posts/go-build/ - 功能:构建go代码,生成可执行文件 - 语法:go build [-o output] [build flags] [packages] - -o output:指定生成的可执行文件名 - -a:强制重新构建。(默认在已经有生成的可执行文件后,源文件没有更新,就不会重新构建) - -compiler name:指定go编译器,可选gcgccgo,默认使用go自带的gc编译器,gccgo是GCC编译器

4 go clean

  • 功能:清理编译的object文件
  • 语法:go clean [clean flags] [build flags] [packages]

5 go get

  • 功能:下载指定的go module包
  • 语法:go get [-d] [-t] [-u] [build flags] [packages]
    • go get example.com/pkg:下载或更新最新版
    • go get example.com/pkg@v1.2.3:下载或更新到指定tag版
    • go get golang.org/x/text@master:下载或更新master分支版
    • go get example.com/mod@none:移除module,并下载依赖包

6 go install

  • 功能:编译并打包应用

7 go env

  • 功能:查看环境变量

8 go mod

9 go doc

  • 功能:查看go 代码文档

10 go fmt

  • 功能:格式化go代码

11 go test

  • 功能:go单元测试