go build

命令

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

features

  1. 忽略*_test.go
  2. -i 自动安装依赖。不加也会自动安装啊

build flags

build, clean, get, install, list, run, and test 共享build flags

-a

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

强制重新编译所有package

-n

print the commands but do not run them.

打印即将执行的命令,但是不执行他们。eg:go build -n all.go

PS D:\work\gowrokspace> go build -n .\all.go

#
# command-line-arguments
#

mkdir -p $WORK\b001\
cat >$WORK\b001\importcfg << 'EOF' # internal        
# import config
packagefile fmt=C:\Go\pkg\windows_amd64\fmt.a        
packagefile runtime=C:\Go\pkg\windows_amd64\runtime.a
EOF
cd D:\work\gowrokspace
"C:\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "$WORK\\b001\\_pkg_.a" -trimpath "$WORK\\b001" -p main -complete -buildid wxMFmciacSP3v8U_ybY7/wxMFmciacSP3v8U_ybY7 -goversion go1.12.1 -D _/D_/work/gowrokspace -importcfg "$WORK\\b001\\importcfg" -pack -c=4 "D:\\work\\gowrokspace\\all.go"
"C:\\Go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\_pkg_.a" # internal
cat >$WORK\b001\importcfg.link << 'EOF' # internal
packagefile command-line-arguments=$WORK\b001\_pkg_.a
packagefile fmt=C:\Go\pkg\windows_amd64\fmt.a
packagefile runtime=C:\Go\pkg\windows_amd64\runtime.a
packagefile errors=C:\Go\pkg\windows_amd64\errors.a
packagefile internal/fmtsort=C:\Go\pkg\windows_amd64\internal\fmtsort.a
packagefile io=C:\Go\pkg\windows_amd64\io.a
packagefile math=C:\Go\pkg\windows_amd64\math.a
packagefile os=C:\Go\pkg\windows_amd64\os.a
packagefile reflect=C:\Go\pkg\windows_amd64\reflect.a
packagefile strconv=C:\Go\pkg\windows_amd64\strconv.a
packagefile sync=C:\Go\pkg\windows_amd64\sync.a
packagefile unicode/utf8=C:\Go\pkg\windows_amd64\unicode\utf8.a
packagefile internal/bytealg=C:\Go\pkg\windows_amd64\internal\bytealg.a
packagefile internal/cpu=C:\Go\pkg\windows_amd64\internal\cpu.a
packagefile runtime/internal/atomic=C:\Go\pkg\windows_amd64\runtime\internal\atomic.a
packagefile runtime/internal/math=C:\Go\pkg\windows_amd64\runtime\internal\math.a
packagefile runtime/internal/sys=C:\Go\pkg\windows_amd64\runtime\internal\sys.a
packagefile sort=C:\Go\pkg\windows_amd64\sort.a
packagefile sync/atomic=C:\Go\pkg\windows_amd64\sync\atomic.a
packagefile math/bits=C:\Go\pkg\windows_amd64\math\bits.a
packagefile internal/poll=C:\Go\pkg\windows_amd64\internal\poll.a
packagefile internal/syscall/windows=C:\Go\pkg\windows_amd64\internal\syscall\windows.a
packagefile internal/testlog=C:\Go\pkg\windows_amd64\internal\testlog.a
packagefile syscall=C:\Go\pkg\windows_amd64\syscall.a
packagefile time=C:\Go\pkg\windows_amd64\time.a
packagefile unicode/utf16=C:\Go\pkg\windows_amd64\unicode\utf16.a
packagefile unicode=C:\Go\pkg\windows_amd64\unicode.a
packagefile internal/race=C:\Go\pkg\windows_amd64\internal\race.a
packagefile internal/syscall/windows/sysdll=C:\Go\pkg\windows_amd64\internal\syscall\windows\sysdll.a
packagefile internal/syscall/windows/registry=C:\Go\pkg\windows_amd64\internal\syscall\windows\registry.a
EOF
mkdir -p $WORK\b001\exe\
cd .
"C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "$WORK\\b001\\exe\\a.out.exe" -importcfg "$WORK\\b001\\importcfg.link" -buildmode=exe -buildid=bEfabckFmZB4v9w_Lx-N/wxMFmciacSP3v8U_ybY7/wxMFmciacSP3v8U_ybY7/bEfabckFmZB4v9w_Lx-N -extld=gcc "$WORK\\b001\\_pkg_.a"     
"C:\\Go\\pkg\\tool\\windows_amd64\\buildid.exe" -w "$WORK\\b001\\exe\\a.out.exe" # internal
mv $WORK\b001\exe\a.out.exe all.exe

-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.

编译或者test程序可以并行的程序数。如果test各个func有race,必须指定这个为1

-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, linux/arm64
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.

打印出跟-n一样的内容,但是会执行。跟shell script的-x效果一样

-asmflags ‘[pattern=]arg list’

arguments to pass on each go tool asm invocation.

编译过程中调用asm工具时候的flags

-buildmode mode

build mode to use. See 'go help buildmode' for more.

简单来说就是控制生成的文件,静态文件.a还是动态文件.so,默认就行了。搞动态文件不是玩死自己了么

-compiler name

name of compiler to use, as in runtime.Compiler (gccgo or gc).

-gccgoflags ‘[pattern=]arg list’

arguments to pass on each gccgo compiler/linker invocation.

-gcflags ‘[pattern=]arg list’

arguments to pass on each go tool compile invocation.

-installsuffix suffix

a suffix to use in the name of the package installation directory,
in order to keep output separate from default builds.
If using the -race flag, the install suffix is automatically set to race
or, if set explicitly, has _race appended to it. Likewise for the -msan
flag. Using a -buildmode option that requires non-default compile flags
has a similar effect.

生成文件的名字

-ldflags ‘[pattern=]arg list’

arguments to pass on each go tool link invocation.

链接阶段的flags,如果要修改符号的值就需要使用这个

-linkshared

link against shared libraries previously created with
-buildmode=shared.

动态链接的,不管

-mod mode

module download mode to use: readonly or vendor.
See 'go help modules' for more.

应该是module和vendor的选择吧

-pkgdir dir

install and load all packages from dir instead of the usual locations.
For example, when building with a non-standard configuration,
use -pkgdir to keep generated packages in a separate location.

临时指定一个pkg dir

-tags tag,list

a comma-separated list of build tags to consider satisfied during the
build. For more information about build tags, see the description of
build constraints in the documentation for the go/build package.
(Earlier versions of Go used a space-separated list, and that form
is deprecated but still recognized.)

build tag,在go代码里面可以定义一些代码在特定条件满足的时候才会被编译。有点类似于c语言的ifdef宏以及编译时候的-D

-trimpath

remove all file system paths from the resulting executable.
Instead of absolute file system paths, the recorded file names
will begin with either "go" (for the standard library),
or a module path@version (when using modules),
or a plain import path (when using GOPATH).

是在生成的二进制符号里面,runtime获取文件名的时候,不使用文件系统的路径而是用import的路径

-toolexec ‘cmd args’

a program to use to invoke toolchain programs like vet and asm.
For example, instead of running asm, the go command will run
'cmd args /path/to/asm <arguments for asm>'.

指定在运行过程中的工具该使用啥样的命令

go tool compile(-gcflags)

go : usage: compile [options] file.go...

  -%	debug non-static initializers
  -+	compiling runtime
  -B	disable bounds checking
  -C	disable printing of columns in error messages
  -D path
    	set relative path for local imports
  -E	debug symbol export
  -I directory
    	add directory to import search path
  -K	debug missing line numbers

  -L	show full file names in error messages
  -N	disable optimizations
  -S	print assembly listing

  -V	print version and exit
  -W	debug parse tree after type checking

  -allabis
    	generate ABI wrappers for all symbols (for bootstrap)
  -asmhdr file
    	write assembly header to file
  -bench file
    	append benchmark times to file
  -blockprofile file
    	write block profile to file
  -buildid id
    	record id as the build id in the export metadata

  -c int
    	concurrency during compilation, 1 means no concurrency (default 1)
  -complete
    	compiling complete package (no C or assembly)

  -cpuprofile file
    	write cpu profile to file
  -d list
    	print debug information about items in list; try -d help

  -dwarf
    	generate DWARF symbols (default true)
  -dwarflocationlists
    	add location lists to DWARF in optimized mode (default true)

  -dynlink
    	support references to Go symbols defined in other shared libraries
  -e	no limit on number of errors reported

  -gendwarfinl int
    	generate DWARF inline info records (default 2)

  -goversion string
    	required version of the runtime
  -h	halt on error

  -importcfg file
    	read import configuration from file
  -importmap definition
    	add definition of the form source=actual to import map

  -installsuffix suffix
    	set pkg directory suffix
  -j	debug runtime-initialized variables

  -l	disable inlining
  -lang string
    	release to compile for

  -linkobj file
    	write linker-specific object to file
  -live
    	debug liveness analysis

  -m	print optimization decisions
  -memprofile file
    	write memory profile to file

  -memprofilerate rate
    	set runtime.MemProfileRate to rate
  -mutexprofile file
    	write mutex profile to file

  -nolocalimports
    	reject local (relative) imports
  -o file
    	write output to file

  -p path
    	set expected package import path
  -pack
    	write to file.a instead of file.o

  -r	debug generated wrappers
  -race
    	enable race detector

  -s	warn about composite literals that can be simplified
  -shared
    	generate code that can be linked into a shared library

  -std
    	compiling standard library
  -symabis file
    	read symbol ABIs from file

  -traceprofile file
    	write an execution trace to file

  -trimpath prefix
    	remove prefix from recorded source file paths
  -v	increase debug verbosity

  -w	debug type checking

  -wb
    	enable write barrier (default true)

go tool link(-ldflags)

go : usage: link [options] main.o
  -B note
    	add an ELF NT_GNU_BUILD_ID note when using ELF
  -D address
    	set data segment address (default -1)
  -E entry
    	set entry symbol name
  -H type
    	set header type
  -I linker
    	use linker as ELF dynamic linker
  -L directory
    	add specified directory to library path
  -R quantum
    	set address rounding quantum (default -1)
  -T address
    	set text segment address (default -1)

  -V	print version and exit

  -X definition
    	add string value definition of the form importpath.name=value
  -a	disassemble output

  -buildid id
    	record id as Go toolchain build id
  -buildmode mode
    	set build mode

  -c	dump call graph

  -compressdwarf
    	compress DWARF if possible (default true)
  -cpuprofile file
    	write cpu profile to file

  -d	disable dynamic executable
  -debugtramp int
    	debug trampolines

  -dumpdep
    	dump symbol dependency graph

  -extar string
    	archive program for buildmode=c-archive
  -extld linker
    	use linker when linking in external mode

  -extldflags flags
    	pass flags to external linker
  -f	ignore version mismatch

  -g	disable go package data checks
  -h	halt on error

  -importcfg file
    	read import configuration from file
  -installsuffix suffix
    	set package directory suffix

  -k symbol
    	set field tracking symbol
  -libgcc string
    	compiler support lib for internal linking; use "none" to disable

  -linkmode mode
    	set link mode
  -linkshared
    	link against installed Go shared libraries

  -memprofile file
    	write memory profile to file
  -memprofilerate rate
    	set runtime.MemProfileRate to rate

  -msan
    	enable MSan interface
  -n	dump symbol table

  -o file
    	write output to file

  -pluginpath string
    	full path name for plugin

  -r path
    	set the ELF dynamic linker search path to dir1:dir2:...

  -race
    	enable race detector
  -s	disable symbol table

  -tmpdir directory
    	use directory for temporary files
  -u	reject unsafe packages

  -v	print link trace
  -w	disable DWARF generation

我们可以提供一个配置文件version.conf,程序运行时从version.conf取得这些信息进行显示。但是在部署程序时,除了二进制文件还需要额外的配置文件,不是很方便。
或者将这些信息写入代码中,这样不需要额外的version.conf,但要在每次编译时修改代码文件,也够麻烦的了。
有一种更好的办法是在编译时使用参数-ldflags -X importpath.name=value,官方解释如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Go 语言中,生成带文件版本号的文件并不像在其他编程语言中那样常见,也不像 Windows 操作系统那样具有内置的机制。不过,您可以在您的 Go 项目中使用一些第三方工具来实现这一功能,下面是其中两个比较常用的工具: 1. goversioninfo:这是一个 Go 语言编写的工具,可以为您的 Windows 程序生成带文件版本号的文件。您可以使用以下命令安装 goversioninfo: ``` go get github.com/josephspurrier/goversioninfo/cmd/goversioninfo ``` 安装完成后,您可以在您的 Go 项目中使用该工具来生成带文件版本号的文件。例如,如果您要将您的项目的版本号更新为 v1.2.3,并生成一个带文件版本号的可执行文件 myapp.exe,您可以使用以下命令: ``` goversioninfo -o versioninfo.go go build -ldflags="-X main.version=v1.2.3" -o myapp.exe main.go versioninfo.go ``` 其中,goversioninfo 命令用于生成 versioninfo.go 文件,该文件包含了文件版本号等信息。go build 命令使用 -ldflags 参数来指定版本号信息。 2. go-bindata:这是一个 Go 语言编写的工具,可以将您的资源文件打包成一个单独的可执行文件,并为其生成带文件版本号的文件。您可以使用以下命令安装 go-bindata: ``` go get github.com/go-bindata/go-bindata/... ``` 安装完成后,您可以在您的 Go 项目中使用该工具来生成带文件版本号的文件。例如,如果您要将您的项目的版本号更新为 v1.2.3,并生成一个带文件版本号的可执行文件 myapp.exe,您可以使用以下命令: ``` go-bindata -pkg main -o bindata.go assets/ go build -ldflags="-X main.version=v1.2.3" -o myapp.exe main.go bindata.go ``` 其中,go-bindata 命令用于将 assets/ 目录下的资源文件打包成 bindata.go 文件,该文件包含了资源文件的二进制数据。go build 命令使用 -ldflags 参数来指定版本号信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值