Go 的编译参数和参数说明

11 min read

Go 编译器的参数如下:

Usage: go [arguments] [command]

The commands are:

    build       compile packages and dependencies
    clean       remove object files and cached files
    doc         show documentation for package or symbol
    env         print Go environment information
    bug         start a bug report
    fix         update packages to use new APIs
    fmt         gofmt (reformat) package sources
    generate    generate Go files by processing source
    get         download and install packages and dependencies
    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
    package     package files
    testflag    testing flags
    testfunc    testing functions

Use "go help [topic]" for more information about that topic.

其中,常用的编译参数和参数说明如下:

  • -o:指定输出文件名;
  • -v:打印编译过程中的详细信息;
  • -i:安装包和依赖;
  • -a:完全重新编译包及其依赖;
  • -n:打印编译命令,但不执行编译命令;
  • -x:打印编译命令,并执行编译命令;
  • -p:指定并行编译的数量;
  • -race:开启竞态检测;
  • -tags:指定编译时使用的标记;
  • -ldflags:指定链接时使用的参数;
  • -gcflags:指定编译器参数;
  • -asmflags:指定汇编器参数;
  • -mod:指定模块模式。

例如:编译一个叫 main 的文件,在 Windows 平台上,输出名为 main.exe,使用了 -ldflags 选项来链接一些库文件,命令如下:

go build -o main.exe -ldflags "-lxxx -lyyy" main.go