Created
October 28, 2021 13:21
-
-
Save mattkasun/7d3b42575661b025245402fdb0816498 to your computer and use it in GitHub Desktop.
Version info for app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here's a simple example, hopefully it would clarify and help how to do it easily: | |
Create a directory for your application: | |
$ mkdir app && cd app | |
Create a sub directory config: | |
$ mkdir config | |
Add the following file. it should be under app/config/vars.go: | |
package config | |
var Version string | |
var BuildTime string | |
//todo: can add as many as build vars | |
In the root app/, add main package main.go: | |
package main | |
import ( | |
"fmt" | |
"app/config" | |
) | |
func main() { | |
fmt.Println("build.Version:\t", Version) | |
fmt.Println("build.Time:\t", build.BuildTime) | |
} | |
Now it is time to build: | |
go build -ldflags "-X 'app/config.Version=0.0.1' -X 'app/config.BuildTime=$(date)'" | |
Once it's built, you can run now the app: | |
$ ./app | |
Version: 0.0.1 | |
build.Time: Sat Jul 4 19:49:19 UTC 2020 | |
Finally, you may need sometimes to explore what does specific app you didn't code yourself provide in ldflags, you can do this by using nm that comes with go tool to list all ldflags. | |
Just build the app then use go tool nm to list all linker flags. | |
$ go build -o app | |
$ go tool nm ./app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment