golang 条件编译

很多时候对于不同的平台需要编写不同的代码,可选的方式

build tag

  • 参考格式
// +build
  • 对于平台处理的例子
// +build linux,386 darwin,!cgo

文件后缀

  • 参考格式
_$GOOS.go
_$GOOS_$GOARCH.go
  • 参考例子
mypkg_freebsd_arm.go  
mypkg_plan9.go  

如何选择

对于对于构建需要特定平台关联(排他性),可以使用build tag,或者需要平台hotfix (都支持),可以使用文件后缀,下边的链接有说明

参考资料

https://dave.cheney.net/2013/10/12/how-to-use-conditional-compilation-with-the-go-build-tool
https://github.com/rongfengliang/go-build-tag-file-suffix

原文地址:https://www.cnblogs.com/rongfengliang/p/12251836.html