Golang预处理器,如C风格的编译开关

GO语言是否有预处理器? 当我查找互联网时,几乎没有将* .pgo转换为* .go的方法。 而且,我想知道它是否可以在Go中使用</ p>

  #ifdef COMPILE_OPTION 
{编译此代码...}
#elif
{编译其他代码... }
</ code> </ pre>


#undef in c </ code> </ p>
</ div>

展开原文

原文

Does GO language have a preprocessor? When I looked up internet, there was few approaches which *.pgo convert to *.go. And, I wonder if it is doable in Go

#ifdef COMPILE_OPTION
  {compile this code ... }
#elif 
  {compile another code ...}

or, #undef in c

2个回答



有可能使用 Java注释预处理器 + maven golang插件并得到一些 类似的行为,在golang代码看起来像</ p>

  //#if COMPILE_OPTION 
fmt.Println(“ Ok”)
//#else
fmt的情况下。 Println(“否”)
//#endif
</ code> </ pre>

此处已放置一些示例 https://github.com/raydac/mvn-golang/tree/master/mvn-golang -examples / mvn-golang-examples-预处理 </ p>
</ div>

展开原文

原文

potentially it is possible to use Java Comment Preprocessor + maven golang plugin and get some similar behavior, in the case golang code will look like

//#if COMPILE_OPTION
 fmt.Println("Ok")
//#else
 fmt.Println("No")
//#endif

some example has been placed here https://github.com/raydac/mvn-golang/tree/master/mvn-golang-examples/mvn-golang-examples-preprocessing



最接近的方法是使用构建约束。 示例:</ p>

main.go </ p>

 包main 

func main(){
println(“ main()”)

conditionalFunction()
}
</ code> </ pre>

a.go </ p>

  // +构建COMPILE_OPTION 
\ npackage main

func conditionalFunction(){
println(“ conditionalFunction”)
}
</ code> </ pre>

b.go </ p>

< pre> // + build!COMPILE_OPTION

package main

func conditionalFunction(){
}
</ code> </ pre>

输出:</ p>

 %进入build -o示例;  ./example
main()

% go build -o example -tags COMPILE_OPTION; ./example
main()
conditionalFunction


</ div>

展开原文

原文

The closest way to achieve this is by using build constraints. Example:

main.go

package main

func main() {
    println("main()")
    conditionalFunction()
}

a.go

// +build COMPILE_OPTION

package main

func conditionalFunction() {
    println("conditionalFunction")
}

b.go

// +build !COMPILE_OPTION

package main

func conditionalFunction() {
}

Output:

% go build -o example ; ./example
main()

% go build -o example -tags COMPILE_OPTION ; ./example
main()
conditionalFunction

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问