This is a simple Go package for handling semantic versioning (SemVer).
- Representation of semantic versions with major, minor, and patch version numbers, as well as optional pre-release and build metadata.
- Comparison of versions with
Lessmethod, according to the rules described in the Semver Spec. - Equality check with
Equalmethod. - Automatic VCS commit information extraction with
Commit()function for build metadata. - Package version introspection with
Current()function.
Here's an example of how to use the Version struct:
v1 := semver.Version{ Major: 1, Minor: 0, Patch: 0, PreRelease: "alpha", Build: "001", }
fmt.Println(v1.String()) // Output: "1.0.0-alpha+001"
v2 := semver.Version{ Major: 1, Minor: 0, Patch: 1, }
if v1.Less(v2) { fmt.Println("v1 is less than v2") }
if !v1.Equal(v2) { fmt.Println("v1 is not equal to v2") }The Commit() function automatically extracts VCS commit information to populate build metadata:
// Automatically populate build metadata with commit info
version := semver.Version{
Major: 1,
Minor: 2,
Patch: 3,
PreRelease: "beta",
Build: semver.Commit(), // Uses Git commit hash from build info
}
fmt.Println(version.String())
// Examples of output:
// "1.2.3-beta+abc1234" (clean build from commit abc1234)
// "1.2.3-beta+abc1234-dirty" (build with uncommitted changes)
// "1.2.3-beta+*-dirty" (dirty build, no commit hash available)This is particularly useful for:
- CI/CD pipelines - Track which commit produced each build
- Deployment tracking - Link deployed artifacts to source code
- Debugging - Identify the exact code version causing issues
You can get the version of the semver package itself:
fmt.Printf("Using semver package version: %s\n", semver.Current().String())
// Output: "Using semver package version: 0.3.0+abc1234"The Compare() method enables easy sorting of version slices:
versions := []semver.Version{
{Major: 1, Minor: 0, Patch: 0, PreRelease: "beta"},
{Major: 1, Minor: 0, Patch: 0},
{Major: 1, Minor: 0, Patch: 0, PreRelease: "alpha"},
{Major: 0, Minor: 9, Patch: 0},
}
sort.Slice(versions, func(i, j int) bool {
return versions[i].Compare(versions[j]) < 0
})
// Result: [0.9.0, 1.0.0-alpha, 1.0.0-beta, 1.0.0]You can also use the ByVersion type with the standard sort.Interface:
versions := []semver.Version{
{Major: 1, Minor: 0, Patch: 0, PreRelease: "beta"},
{Major: 1, Minor: 0, Patch: 0},
{Major: 1, Minor: 0, Patch: 0, PreRelease: "alpha"},
}
sort.Sort(semver.ByVersion(versions))
// Result: [1.0.0-alpha, 1.0.0-beta, 1.0.0]Different string representations for various use cases:
version := semver.Version{
Major: 1, Minor: 2, Patch: 3,
PreRelease: "beta.1",
Build: "20250129.abc123",
}
fmt.Println(version.String()) // "1.2.3-beta.1+20250129.abc123" (full version)
fmt.Println(version.Short()) // "1.2.3-beta.1" (without build metadata)
fmt.Println(version.Core()) // "1.2.3" (core version only)You can run the unit tests included in the project with the following command:
go testIf you would like to contribute, please fork the repository and use a feature branch. All contributions are welcome!
This project is open source, under the MIT License.