2014-12-03 33 views
5

我有一個圍棋程序test.go如何使用去構建設置在編譯時布爾變量-ldflags

package main 

import "fmt" 

var DEBUG_MODE bool = true  

func main() { 
    fmt.Println(DEBUG_MODE) 
} 

我想在compile時間DEBUG_MODE變量設置爲false

我試着:

go build -ldflags "-X main.DEBUG_MODE 0" test.go && ./test 
true                                                        
[email protected]:18:49:32:/tmp$ go build -ldflags "-X main.DEBUG_MODE false" test.go && ./test 
true                                                        
[email protected]:18:49:41:/tmp$ go build -ldflags "-X main.DEBUG_MODE 0x000000000000" test.go && ./test                                 
true                    

它不工作,但它工作時DEBUG_MODEstring

+0

-X工作,但只用於字符串。考慮使用構建標記。 – Volker 2014-12-03 12:01:49

回答

5

您只能使用-X鏈接器標誌設置字符串變量。 From the docs

-X importpath.name=value 
    Set the value of the string variable in importpath named name to value. 
    Note that before Go 1.5 this option took two separate arguments. 
    Now it takes one argument split on the first = sign. 

你可以使用一個字符串,而不是:

var DebugMode = "true" 

然後

go build -ldflags "-X main.DebugMode=false" test.go && ./test 
相關問題