Inject values into your golang binary at build time
I recently discovered that you can update the value of a string variable inside your golang code via compiler flags. This is very useful for scenarios where you want to set a value based on an external process, but you don't want to let your users change it. We run into that issue recently when we wanted to add a version command to a binary we were building.
Let's say we something like the program below:
package main
import "fmt"
var version = "0.0.0"
func main() {
fmt.Printf("version %s \n", version)
}
`
If you compile and run this, the output will be:
go build main.go
./main
version 0.0.0
But by using the -ldflags "-X importpath.name=val" build parameters, you can set the value at build time:
go build -ldflags "-X main.version=1.0.0" main.go
./main
version 1.0.0
You pass the -X flag several times if you need it (e.g to set version and build time).
Hope it helps!
About the author
I’m the founder and CEO of Okteto, where we’re busy re-imagining the developer experience for cloud native applications.
Before Okteto, I built enterprise chat products at Atlassian, application lifecycle management tools at Elasticbox (now part of Centurylink) and cloud services at Microsoft Azure.
Comments
Join the discussionNo comments yet. Be the first to comment.




