How to embed a string in the compiled binary at build time? [duplicate]

Solution 1:

Declare a variable in your code (in the example below, in main, it will be set in the next step)

var commit string

Add a flag to the build

go build -ldflags "-X main.commit=$GIT_COMMIT"

You can then access the variable as usual

log.Info().Msgf("commit → %v", commit)