Skip to Content
Hands-On Software Engineering with Golang
book

Hands-On Software Engineering with Golang

by Achilleas Anagnostopoulos
January 2020
Intermediate to advanced
640 pages
16h 56m
English
Packt Publishing
Content preview from Hands-On Software Engineering with Golang

Comparing semantic versions

If we are given two semantic versions, a.b.c and x.y.z, how can we tell which one is more recent? To compare two semantic versions, we need to compare each one of their individual components from left to right. Here is a short piece of code demonstrating how we can compare two semantic versions:

// SemVer contains the major, minor, patch components of a semantic version// string.
type SemVer [3]int

// GreaterThan returns true if the receiver version is greater than other.
func (sv SemVer) GreaterThan(other SemVer) bool {
    for i, v := range sv {
        if v != other[i] {
            return v > other[i]
        }
    }

    return false
}

Comparing or sorting semantic versions is quite an easy task for humans to perform, but nevertheless, as is evident ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Software Architecture with Golang

Hands-On Software Architecture with Golang

Jyotiswarup Raiturkar

Publisher Resources

ISBN: 9781838554491Supplemental Content