Alex Rivera | Logout

Golang conditional compilation

Asked 2012-05-18T04:02:01.300
58

I've got a trouble with conditional compilation in Go 1.

Here is my test code. Is there anything I misunderstand about the "// +build" constraint and the "-tags" flag?

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'
# test
./main2.go:8: main redeclared in this block
        previous declaration at ./main1.go:8
Edit
Report

1 Answer

4

From the Build Constraints docs:

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

answered 2013-05-23T05:26:28.330

Your Answer