Skip to content

Commit 19fded7

Browse files
committed
internal/history: rename Version to GoVer
Make it more clear that this type represents a Go-specific version, rather than a version that follows the Semantic Versioning 2.0.0 specification. The type already has Go-specific methods, for example IsMajor reports true for "1.14" and IsMinor reports true for "1.14.1". This is consistent with the terminology used by Go releases, but very surprising if considered from the perspective of semver. Document some differences of the Go-specific version convention compared to semver, and describe the X, Y, Z fields in more detail. This change makes it viable to add a String method to GoVer type documented to print a Go-specific version string, which will be useful in more places in CL 229483. For golang/go#32450. Change-Id: If7482fdb4a739ff5b89b7133402d94412057f590 Reviewed-on: https://go-review.googlesource.com/c/website/+/229481 Reviewed-by: Carlos Amedee <[email protected]>
1 parent 02cb727 commit 19fded7

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

cmd/golangorg/release.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func (h releaseHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
7878
// sortReleases returns a sorted list of Go releases, suitable to be
7979
// displayed on the Release History page. Releases are arranged into
8080
// major releases, each with minor revisions.
81-
func sortReleases(rs map[history.Version]history.Release) []Major {
81+
func sortReleases(rs map[history.GoVer]history.Release) []Major {
8282
var major []Major
83-
byMajorVersion := make(map[history.Version]Major)
83+
byMajorVersion := make(map[history.GoVer]Major)
8484
for v, r := range rs {
8585
switch {
8686
case v.IsMajor():
@@ -108,8 +108,8 @@ func sortReleases(rs map[history.Version]history.Release) []Major {
108108

109109
// majorOf takes a Go version like 1.5, 1.5.1, 1.5.2, etc.,
110110
// and returns the corresponding major version like 1.5.
111-
func majorOf(v history.Version) history.Version {
112-
return history.Version{X: v.X, Y: v.Y, Z: 0}
111+
func majorOf(v history.GoVer) history.GoVer {
112+
return history.GoVer{X: v.X, Y: v.Y, Z: 0}
113113
}
114114

115115
type releaseTemplateData struct {
@@ -125,20 +125,13 @@ type Major struct {
125125

126126
// Release represents a Go release entry as displayed on the release history page.
127127
type Release struct {
128-
ver history.Version
128+
ver history.GoVer
129129
rel history.Release
130130
}
131131

132132
// V returns the Go release version string, like "1.14", "1.14.1", "1.14.2", etc.
133133
func (r Release) V() string {
134-
switch {
135-
case r.ver.Z != 0:
136-
return fmt.Sprintf("%d.%d.%d", r.ver.X, r.ver.Y, r.ver.Z)
137-
case r.ver.Y != 0:
138-
return fmt.Sprintf("%d.%d", r.ver.X, r.ver.Y)
139-
default:
140-
return fmt.Sprintf("%d", r.ver.X)
141-
}
134+
return r.ver.String()
142135
}
143136

144137
// Date returns the date of the release, formatted for display on the release history page.

internal/history/release.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package history
77

88
import (
9+
"fmt"
910
"html/template"
1011
"time"
1112
)
@@ -29,7 +30,7 @@ type Release struct {
2930
//
3031
// It contains entries for releases of Go 1.9 and newer.
3132
// Older releases are listed in doc/devel/release.html.
32-
var Releases = map[Version]Release{
33+
var Releases = map[GoVer]Release{
3334
{1, 14, 2}: {
3435
Date: Date{2020, 4, 8},
3536

@@ -457,20 +458,41 @@ of non-Git repositories under certain conditions.`,
457458
},
458459
}
459460

460-
// Version represents the version of a Go release.
461-
type Version struct {
462-
X int // 1 or higher.
463-
Y int // 0 or higher.
464-
Z int // 0 or higher.
461+
// GoVer represents a Go release version.
462+
//
463+
// In contrast to Semantic Versioning 2.0.0,
464+
// trailing zero components are omitted,
465+
// a version like Go 1.14 is considered a major Go release,
466+
// a version like Go 1.14.1 is considered a minor Go release.
467+
//
468+
// See proposal golang.org/issue/32450 for background, details,
469+
// and a discussion of the costs involved in making a change.
470+
type GoVer struct {
471+
X int // X is the 1st component of a Go X.Y.Z version. It must be 1 or higher.
472+
Y int // Y is the 2nd component of a Go X.Y.Z version. It must be 0 or higher.
473+
Z int // Z is the 3rd component of a Go X.Y.Z version. It must be 0 or higher.
474+
}
475+
476+
// String returns the Go release version string,
477+
// like "1.14", "1.14.1", "1.14.2", and so on.
478+
func (v GoVer) String() string {
479+
switch {
480+
case v.Z != 0:
481+
return fmt.Sprintf("%d.%d.%d", v.X, v.Y, v.Z)
482+
case v.Y != 0:
483+
return fmt.Sprintf("%d.%d", v.X, v.Y)
484+
default:
485+
return fmt.Sprintf("%d", v.X)
486+
}
465487
}
466488

467489
// IsMajor reports whether version v is considered to be a major Go release.
468490
// For example, Go 1.14 and 1.13 are major Go releases.
469-
func (v Version) IsMajor() bool { return v.Z == 0 }
491+
func (v GoVer) IsMajor() bool { return v.Z == 0 }
470492

471493
// IsMinor reports whether version v is considered to be a minor Go release.
472494
// For example, Go 1.14.1 and 1.13.9 are minor Go releases.
473-
func (v Version) IsMinor() bool { return v.Z != 0 }
495+
func (v GoVer) IsMinor() bool { return v.Z != 0 }
474496

475497
// Date represents the date (year, month, day) of a Go release.
476498
//

0 commit comments

Comments
 (0)