Extra Info Fields
The version
package supports most popular version fields natively.
Native fields
Key | Description |
---|---|
.Version |
Binary version value set via -ldflags , otherwise taken from go install url/tool@version . |
.GitCommit |
Git commit value set via -ldfags , otherwise taken from debug.ReadBuildInfo() - the vcs.revision tag. |
.BuildDate |
Build date value set via -ldflags , otherwise empty. |
.CommitDate |
Git commit date value set via -ldfags , otherwise taken from debug.ReadBuildInfo() from the vcs.time tag. |
.DirtyBuild |
Dirty build value, set via -ldfags , otherwise taken from debug.ReadBuildInfo() from the vcs.modified tag. |
.GoVersion |
Go version taken from runtime.Version() . |
.Compiler |
Go compiler taken from runtime.Compiler . |
.Platform |
Build platform, passed in the following format: runtime.GOOS/runtime.GOARCH . |
Check build options to learn how to override them if you need.
However, each project may want to display more information such as documentation or changelog URLs, sometimes even domain related fields. You can provide them using your Go struct.
Usage
Tip
Want to try? See the custom fields example!
Steps:
-
Assign your custom struct to
Info.ExtraFields
.Go struct with nested fields are not properly supported in Pretty mode.
-
Use
json
,yaml
andpretty
field tags to define the field name for a given output format. - In the Pretty mode, fields are printed in the same order as defined in struct.
Example:
package main
import (
"log"
"os"
"github.com/spf13/pflag"
"go.szostok.io/version"
"go.szostok.io/version/printer"
)
type Custom struct {
// In the pretty mode, fields are printed in the same order as defined in struct.
BuiltBy string `json:"builtBy" yaml:"builtBy" pretty:"Built By"`
RepoURL string `json:"repoURL" yaml:"repoURL" pretty:"Repository URL"`
DocsURL string `json:"docsURL" yaml:"docsURL" pretty:"Documentation URL"`
}
func main() {
custom := Custom{
RepoURL: "https://github.com/mszostok/version",
DocsURL: "https://szostok.io/projects/version",
BuiltBy: "GoReleaser",
}
info := version.Get()
info.ExtraFields = custom
p := printer.New()
p.RegisterPFlags(pflag.CommandLine) // register `--output/-o` flag
pflag.Parse()
if err := p.PrintInfo(os.Stdout, info); err != nil {
log.Fatal(err)
}
}