Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/mod/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/mod/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
Loading