I created a simple program where I set 2 flags and build it. How to read these flags and set this 2 values from compiled binary?
I tried to read the file many ways but I did not find the flag value.
flag.go
var (
name *string
version *int
)
func init(){
name = flag.String("name","test", "a string")
version = flag.Int("version", 1.0, "an int")
}
func main(){
flag.Parse()
fmt.Println("name : ", *name)
fmt.Println("version : ", *version)
}
main.go
func main(){
path := "/Users/____/Desktop/flag"
dat,_ := ioutil.ReadFile(path)
strings.Contains(string(dat),"name")
fmt.Println("%s opened
", dat)
}
I want to find flag value from compiled binary a store it to main program variable. Something like that
var flagMain = test var versionMain = 1.0
flag.Parse()
is a runtime operation.
Reading back an embedded value means said value was embedded at compile time.
See AgentZombie go-embed-version
as an example:
go build -o /tmp/versserv -ldflags "-X github.com/AgentZombie/go-embed-version/cmd.Version=foo" server/server.go
Then you can call your binary, which can return the embedded value.
Same idea in GoogleContainerTools/container-diff
commit 367eb42 from PR 304.
This is automated (again as an example) in ahmetb/govvv