Skip to content
Golang Cobra & Goreleaser Versioning library
Go Shell Makefile
Branch: master
Clone or download
christopherhein adding
Signed-off-by: Christopher Hein <>
Latest commit 55c09f8 Aug 15, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
hack adding changelog in markdown Aug 14, 2019
.gitignore adding go.* and new ignored files Aug 12, 2019
.travis.yml adding tests for the cobra function Aug 13, 2019 adding Aug 15, 2019
Makefile version bump Aug 15, 2019
VERSION version bump Aug 15, 2019
go.mod adding vendored packages Aug 14, 2019
go.sum adding vendored packages Aug 14, 2019
readme.adoc adding yaml outputter Aug 14, 2019
version.go adding yaml outputter Aug 14, 2019


Go Version

go version

This package gives allows you to use and together to output your version in a simple way. Supporting multiple flags for mutating the output.


To use this package you will need to create cobra command for version such as this.

package main

import (
	goversion ""

var (
	shortened  = false
	version    = "dev"
	commit     = "none"
	date       = "unknown"
	output     = "json"
	versionCmd = &cobra.Command{
		Use:   "version",
		Short: "Version will output the current build information",
		Long:  ``,
		Run: func(_ *cobra.Command, _ []string) {
			resp := goVersion.FuncWithOutput(shortened, version, commit, date, output)

func init() {
	versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
	versionCmd.Flags().StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")

When you do this then you can pass in these flags at build time. _If you’d like more control of the output you can change the Run function to something more like this.

Run: func(_ *cobra.Command, _ []string) {
	var response string
	versionOutput := New(version, commit, date)

	if shortened {
		response = versionOutput.ToShortened()
	} else {
		response = versionOutput.ToJSON()
	fmt.Printf("%+v", response)
go build -ldflags "-X main.commit=<SOMEHASH> -X<SOMEDATE>"

This then gives your CLI the ability to use this for the JSON output:

$ ./my-cli version

Or to make this human readable you could use:

$ ./my-cli -s

Version: dev
Commit: <SOMEHASH>


To run the test suite all you need to do is run.

go test -v ./...


You can’t perform that action at this time.