Go build -ldflags
package main
import (
"fmt"
)
var flagString string
func main() {
fmt.Println("This build with ldflag:", flagString)
}
$ go build -ldflags '-X main.flagString "test"'
$ ./main
This build with ldflag: test
debug_config.go
//+build debug
package main
var TestString string = "test debug"
var TestString2 string = " and it will run every module with debug tag."
func GetConfigString() string {
return "it is debug....."
}
//+build debug 前後需要一個空行(除非你在第一行)
release_config.go
//+build !debug
package main
var TestString string = "test release"
var TestString2 string = " and it will run every module as no debug tag."
func GetConfigString() string {
return "it is release....."
}
main
package main
import (
"fmt"
)
func main() {
fmt.Println("This build is ", TestString, TestString2, GetConfigString())
}
$ go build -tags debug .
$ ./main
This build is test debug and it will run every module with debug tag. it is debug.....
$ go build .
$ ./main
This build is test release and it will run every module as no debug tag. it is release.....