diff --git a/pkg/mod/mod.go b/pkg/mod/mod.go index 8b8d60a..636018b 100644 --- a/pkg/mod/mod.go +++ b/pkg/mod/mod.go @@ -124,9 +124,15 @@ func (mf *File) AddComment(comment string) error { return mf.flush() } + +// GoVersion returns a semver string containing the value of of the go directive. +// For example, it will return "1.2.3" if the go.mod file contains the line "go 1.2.3". +// If no go directive is found, it returns "1.0" because: +// 1. "1.0" is a valid semver string, so it's always safe to parse this value using semver.MustParse(). +// 2. The semantics of the absence of a go directive in a go.mod file means all versions of Go should be able to compile it. func (mf *File) GoVersion() string { if mf.m.Go == nil { - return "" + return "1.0" } return mf.m.Go.Version } diff --git a/pkg/mod/mod_test.go b/pkg/mod/mod_test.go index 4b15994..13e7bc5 100644 --- a/pkg/mod/mod_test.go +++ b/pkg/mod/mod_test.go @@ -43,7 +43,7 @@ func TestFile(t *testing.T) { testutil.Equals(t, "", p) testutil.Equals(t, "", comment) testutil.Equals(t, []string(nil), mf.Comments()) - testutil.Equals(t, "", mf.GoVersion()) + testutil.Equals(t, "1.0", mf.GoVersion()) testutil.Equals(t, 0, len(mf.RequireDirectives())) testutil.Equals(t, 0, len(mf.ReplaceDirectives())) testutil.Equals(t, 0, len(mf.ExcludeDirectives()))