将一个优化标志传递给Go编译器?

huangapple go评论66阅读模式
英文:

Passing an optimization flag to a Go compiler?

问题

要编译Go程序,您可以输入go build myprogram.go,您可以同时传递优化标志,还是代码总是以相同的方式编译?我指的是速度优化、代码大小优化或其他优化。

我知道如果您使用gccgo,只需传递-O2-O0,但我问的是关于官方Go编译器go的问题。

英文:

To compile a Go program you type go build myprogram.go, can you pass an optimization flags along or the code is always compiled in the same way? I am talking about speed optimizations, code size optimizations or other optimizations.

I know if you use gccgo you just pass -O2 or -O0 but my question is about an official Go compiler go.

答案1

得分: 27

实际上没有明确的标志,这个Go维基页面列出了Go编译器所做的优化,并且在golang-nuts组中就这个主题进行了讨论。

你可以在Go gc编译器中关闭优化和内联以进行调试。

-gcflags '-N -l'
  • -N:禁用优化
  • -l:禁用内联
英文:

Actually no explicit flags, this Go wiki page lists optimizations done by the Go compiler and there was a discussion around this topic in golang-nuts groups.

You can turn off optimization and inlining in Go gc compilers for debugging.

-gcflags '-N -l'
  • -N : Disable optimizations
  • -l : Disable inlining

答案2

得分: 11

如果你想优化二进制文件的大小,可以通过在Go链接器中传递-s-w来省略符号表、调试信息和DWARF符号表:

$ go build -o mybinary -ldflags="-s -w" src.go

源博文包含一些基准测试

英文:

If you're looking to optimize the binary size, you can omit the symbol table, debug information and the DWARF symbol table by passing -s and -w to the Go linker:

$ go build -o mybinary -ldflags="-s -w" src.go

(source blog post which includes some benchmarks)

huangapple
  • 本文由 发表于 2017年7月10日 11:06:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/45003259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定