企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 格式 其实是给go tool compile 的参数, 查看文档:`go tool compile —help` ``` -gcflags '[pattern=]arg list': -%: 调试非静态初始化器(debug non-static initializers) -+:编译 go runtime -B:禁用边界检查 -C:禁用错误消息中的列打印 -D path:设置local import的相对路径 -E 调试符号表导出 -I directory:添加 import的搜索路径 -K:调试缺少的行号 -L:在错误消息中显示文件的全路径 -N:禁止编译优化 -S:打印汇编码 -V:打印版本号并退出 -W:类型检查之后调试解析树 -asmhdr file:把汇编头(assemply header)写进文件file -bench file:把benchmark时间添加到文件file -blockprofile file:把 阻塞(block) profile信息写进文件file -buildid id: 设置构建ID,写进导出的元信息中 (record id as the build id in the export metadata) -c int:指定编译时期的并发数量,1表示无并发(默认是1) -complete:编译完整的软件包(无C和汇编代码) -cpuprofile file: 写 cpu profile到file -d list: 打印列表中iterm的调试信息(debug information about items in list) -dwarf: 生成 DWARF符号(默认开启) -dwarfbasentries:使用DWARF中的基地址选择条目(use base address selection entries in DWARF) -dwarflocationlists:在优化模式下将位置列表添加到DWARF(默认开启) -dynlink: 引用在其他go 共享库里定义的符号 -e:显示详细错误信息,不限制错误信息行数 -gendwarfinl int:生成DWARF内联信息记录(默认是 2) -goversion string: 所需的运行时版本 -h:在遇到错误时停止 -importcfg file:从file中读取import配置信息 -importmap definition:添加source=actual形式的定义到 import map -installsuffix suffix: 设置 pkg目录前缀 -j:调试运行时初始化的(runtime-initialized)变量。 -l:禁止内联 -gcflags=-l: 禁止内联 不指定该选项: 开启一般的内联优化 -gcflags=’-l -l’: 开启level 2的内联优化,内联优化更加激进,有可能能优化目标文件执行速度,也有可能生成更大的二进制文件。 -gcflags=’-l -l -l’: 开启level3的内联优化,更加更加激进,目标文件可能执行的更快,一定会生成更大的二进制文件,而且还可能引入bug。 -gcflags=-l=4(4个l), Go 1.11中将开启处于试验阶段的mid-stack inlining优化。 -lang string: release to compile for -linkobj file:将链接器特定的对象写入文件file -live: 调试活性(liveness)分析 -m:打印优化决策 -memprofile file:将memory profile写入文件file -memprofilerate rate:设置runtime.MemProfileRate为rate -mutexprofile file: 将mutex profile写入文件file -newescape: 开启 new escape分析(默认开启),具体含义参考上面编译优化部分“逃逸分析”。 -nolocalimports: 禁止本地(相对)路径import -o file: 将生成的目标文件写到file -p path: 设置包导入路径 -pack:构建目标文件成 .a文件而非.o文件 -r: 调试生成的包装器 -race: 开启竞态检测 -s: 警告可以简化的复合文字 -shared: 生成共享库 -smallframes:减小栈分配对象的大小上限 -std: 编译标准库 -symabis file:从文件file读取ABIs符号信息 -traceprofile file: 将执行trace信息写入文件file -trimpath prefix: 从记录的源代码文件中删除 prefix文件路径前缀 -v: 增加显示调试信息的详细程度 -w: 调试类型检查 -wb: 启用写屏障(默认开启) ``` ## 参数 ## gcflags=-m 进行逃逸分析 ``` // main_pointer.go package main import "fmt" type Demo struct { name string } func createDemo(name string) *Demo { d := new(Demo) // 局部变量 d 逃逸到堆 d.name = name return d } func main() { demo := createDemo("demo") fmt.Println(demo) } ``` 结果 ``` $ go build -gcflags=-m main_pointer.go ./main_pointer.go:10:6: can inline createDemo ./main_pointer.go:17:20: inlining call to createDemo ./main_pointer.go:18:13: inlining call to fmt.Println ./main_pointer.go:10:17: leaking param: name ./main_pointer.go:11:10: new(Demo) escapes to heap ./main_pointer.go:17:20: new(Demo) escapes to heap ./main_pointer.go:18:13: demo escapes to heap ./main_pointer.go:18:13: main []interface {} literal does not escape ./main_pointer.go:18:13: io.Writer(os.Stdout) escapes to heap <autogenerated>:1: (*File).close .this does not escape ```