@@ -62,6 +62,8 @@ The -require, -droprequire, -exclude, -dropexclude, -replace,
62
62
and -dropreplace editing flags may be repeated, and the changes
63
63
are applied in the order given.
64
64
65
+ The -go=version flag sets the expected Go language version.
66
+
65
67
The -print flag prints the final go.mod in its text format instead of
66
68
writing it back to go.mod.
67
69
@@ -74,7 +76,8 @@ writing it back to go.mod. The JSON output corresponds to these Go types:
74
76
}
75
77
76
78
type GoMod struct {
77
- Module Module
79
+ Module Module
80
+ Go string
78
81
Require []Require
79
82
Exclude []Module
80
83
Replace []Replace
@@ -102,8 +105,8 @@ by invoking 'go mod edit' with -require, -exclude, and so on.
102
105
}
103
106
104
107
var (
105
- editFmt = cmdEdit .Flag .Bool ("fmt" , false , "" )
106
- // editGo = cmdEdit.Flag.String("go", "", "")
108
+ editFmt = cmdEdit .Flag .Bool ("fmt" , false , "" )
109
+ editGo = cmdEdit .Flag .String ("go" , "" , "" )
107
110
editJSON = cmdEdit .Flag .Bool ("json" , false , "" )
108
111
editPrint = cmdEdit .Flag .Bool ("print" , false , "" )
109
112
editModule = cmdEdit .Flag .String ("module" , "" , "" )
@@ -131,6 +134,7 @@ func init() {
131
134
func runEdit (cmd * base.Command , args []string ) {
132
135
anyFlags :=
133
136
* editModule != "" ||
137
+ * editGo != "" ||
134
138
* editJSON ||
135
139
* editPrint ||
136
140
* editFmt ||
@@ -161,7 +165,11 @@ func runEdit(cmd *base.Command, args []string) {
161
165
}
162
166
}
163
167
164
- // TODO(rsc): Implement -go= once we start advertising it.
168
+ if * editGo != "" {
169
+ if ! modfile .GoVersionRE .MatchString (* editGo ) {
170
+ base .Fatalf (`go mod: invalid -go option; expecting something like "-go 1.12"` )
171
+ }
172
+ }
165
173
166
174
data , err := ioutil .ReadFile (gomod )
167
175
if err != nil {
@@ -177,6 +185,12 @@ func runEdit(cmd *base.Command, args []string) {
177
185
modFile .AddModuleStmt (modload .CmdModModule )
178
186
}
179
187
188
+ if * editGo != "" {
189
+ if err := modFile .AddGoStmt (* editGo ); err != nil {
190
+ base .Fatalf ("go: internal error: %v" , err )
191
+ }
192
+ }
193
+
180
194
if len (edits ) > 0 {
181
195
for _ , edit := range edits {
182
196
edit (modFile )
@@ -344,6 +358,7 @@ func flagDropReplace(arg string) {
344
358
// fileJSON is the -json output data structure.
345
359
type fileJSON struct {
346
360
Module module.Version
361
+ Go string `json:",omitempty"`
347
362
Require []requireJSON
348
363
Exclude []module.Version
349
364
Replace []replaceJSON
@@ -364,6 +379,9 @@ type replaceJSON struct {
364
379
func editPrintJSON (modFile * modfile.File ) {
365
380
var f fileJSON
366
381
f .Module = modFile .Module .Mod
382
+ if modFile .Go != nil {
383
+ f .Go = modFile .Go .Version
384
+ }
367
385
for _ , r := range modFile .Require {
368
386
f .Require = append (f .Require , requireJSON {Path : r .Mod .Path , Version : r .Mod .Version , Indirect : r .Indirect })
369
387
}
0 commit comments