Tuesday, June 12, 2012

Golang conditional compilation

In C language, we use #if, #else, #endif for conditional compilation.

For Go, the conditional compilation can be achieved by include different files in certain build configuration.

At begin of source file, put // +build XXX then followed by a blank line.

main1.go
// +build main1

package main

import (
    "fmt"
)

func main() {
    fmt.Println("This is main 1")
}
main2.go
// +build main2

package main

import (
    "fmt"
)

func main() {
    fmt.Println("This is main 2")
}
when running "go build", I still got compile error
$ go build -tags 'main1'

No comments: