go build

控制台编译,少不了使用:go build,用法:

usage: go build [-o output] [-i] [build flags] [packages]

       根据输入的包之间的依赖性,编译包,但并不会将编译的结果进行安装。如果编译的参数是一系列的*.go文件,build命令视这些文件在一个包中。当编译一个main包时,build命令会将编译的可执行结果放入第一个go文件名(无后缀.go)中(如:’go build ed.go rx.go’ 编译后的可执行文件为’ed’或者’ed.exe‘,后者为windows系统的文件名 ),当编译的参数是一个目录而不是一系列go文件时,build命令生产的可执行文件名为目录名 (‘go build unix/sam’ writes ‘sam’ or ‘sam.exe’)。

当编译多个包或一个非main包时,build命令编译包并丢弃编译结果,只是检查是否能够被编译。

当编译包时,build会忽略*_test.go文件;

-o参数,只被允许在编译一个单独包时使用,并且强制将编译的可执行文件或object写入户名的文件名中。

-i参数,安装依赖于目标的包

build flag在build、clean、get、install、list、run、test命令中通用。

    -a

        force rebuilding of packages that are already up-to-date.

    -n

        print the commands but do not run them.

    -p n

        the number of programs, such as build commands or

        test binaries, that can be run in parallel.

The build flags are shared by the build, clean, get, install, list, run,

and test commands:

    -a

        force rebuilding of packages that are already up-to-date.

    -n

        print the commands but do not run them.

    -p n

        the number of programs, such as build commands or

        test binaries, that can be run in parallel.

        The default is the number of CPUs available.

    -race

        enable data race detection.

        Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.

    -msan

        enable interoperation with memory sanitizer.

        Supported only on linux/amd64,

        and only with Clang/LLVM as the host C compiler.

    -v

        print the names of packages as they are compiled.

    -work

        print the name of the temporary work directory and

        do not delete it when exiting.

    -x

        print the commands.

针对-gcflags的值可使用go tool compile查看

The ‘go build’ and ‘go install’ commands take a -buildmode argument which

indicates which kind of object file is to be built. Currently supported values

are:

-buildmode=archive

Build the listed non-main packages into .a files. Packages named

main are ignored.

-buildmode=c-archive

Build the listed main package, plus all packages it imports,

into a C archive file. The only callable symbols will be those

functions exported using a cgo //export comment. Requires

exactly one main package to be listed.

-buildmode=c-shared

Build the listed main packages, plus all packages that they

import, into C shared libraries. The only callable symbols will

be those functions exported using a cgo //export comment.

Non-main packages are ignored.

-buildmode=default

Listed main packages are built into executables and listed

non-main packages are built into .a files (the default

behavior).

-buildmode=shared

Combine all the listed non-main packages into a single shared

library that will be used when building with the -linkshared

option. Packages named main are ignored.

-buildmode=exe

Build the listed main packages and everything they import into

executables. Packages not named main are ignored.

-buildmode=pie

Build the listed main packages and everything they import into

position independent executables (PIE). Packages not named

main are ignored.

-buildmode=plugin

Build the listed main packages, plus all packages that they

import, into a Go plugin. Packages not named main are ignored.

    原文作者:pipfeng
    原文地址: https://www.jianshu.com/p/32e6746cc29e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞