How to add version number and commit hash to Go application during build?
Hi everyone! I'm writing a small web application in Go and I want it to display its version when started. Right now I'm manually changing a constant in the code:
```go
const Version = "1.0.3"
```
But this is terribly inconvenient. With every commit I have to dig into the code, change the string, commit separately. Also I'd like to see not only the version number but also the commit hash and build date — to know exactly which build is running on the server.
I tried reading files, but that doesn't work if the binary is moved to another machine. I've heard about ldflags but can't figure out how to use them properly.
Tell me, what's the standard way to handle versioning in Go projects? How do you pass the version, commit hash, and build date into the binary at compile time so it works without hacks? And so that when running `./myapp ver`
No answers yet.
Log in to leave an answer