Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang conditional compilation

Tags:

go

build

go-build

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 
like image 281
Tianran Shen Avatar asked May 18 '12 04:05

Tianran Shen


1 Answers

You must follow // +build XXX with a blank line.

In my brief search, I couldn't find where/if this is documented. But the source clearly calls it out

like image 57
axw Avatar answered Sep 17 '22 19:09

axw