GO 语言有预处理器吗?当我查找互联网时,几乎没有将 *.pgo 转换为 *.go 的方法。而且,我想知道它在 Go 中是否可行
#ifdef COMPILE_OPTION
{compile this code ... }
#elif
{compile another code ...}
或者,
#undef in c
最佳答案
最接近的方法是使用 build constraints .示例:
main.go
package main
func main() {
println("main()")
conditionalFunction()
}
去吧
// +build COMPILE_OPTION
package main
func conditionalFunction() {
println("conditionalFunction")
}
b.go
// +build !COMPILE_OPTION
package main
func conditionalFunction() {
}
输出:
% go build -o example ; ./example
main()
% go build -o example -tags COMPILE_OPTION ; ./example
main()
conditionalFunction
关于Golang Preprocessor like C-style compile switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703867/