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
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
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