Build

3 Ways to Add a --version Flag to a Go CLI

Inject build metadata with -ldflags, read it back with debug.ReadBuildInfo, or grab a tiny helper package — three approaches to a --version flag, with the trade-offs of each

Every CLI eventually grows a --version flag. Users need it to file useful bug reports, scripts need it to gate behavior, and you need it when an issue lands and you want to know what binary the reporter is actually running. Go gives you a few ways to wire this up — none of them long, but each with different ergonomics. Here are the three I reach for.

Golang golang cli build