...
Source file src/pkg/cmd/go/internal/modcmd/init.go
1
2
3
4
5
6
7 package modcmd
8
9 import (
10 "cmd/go/internal/base"
11 "cmd/go/internal/modload"
12 "os"
13 "strings"
14 )
15
16 var cmdInit = &base.Command{
17 UsageLine: "go mod init [module]",
18 Short: "initialize new module in current directory",
19 Long: `
20 Init initializes and writes a new go.mod to the current directory,
21 in effect creating a new module rooted at the current directory.
22 The file go.mod must not already exist.
23 If possible, init will guess the module path from import comments
24 (see 'go help importpath') or from version control configuration.
25 To override this guess, supply the module path as an argument.
26 `,
27 Run: runInit,
28 }
29
30 func runInit(cmd *base.Command, args []string) {
31 modload.CmdModInit = true
32 if len(args) > 1 {
33 base.Fatalf("go mod init: too many arguments")
34 }
35 if len(args) == 1 {
36 modload.CmdModModule = args[0]
37 }
38 if os.Getenv("GO111MODULE") == "off" {
39 base.Fatalf("go mod init: modules disabled by GO111MODULE=off; see 'go help modules'")
40 }
41 if _, err := os.Stat("go.mod"); err == nil {
42 base.Fatalf("go mod init: go.mod already exists")
43 }
44 if strings.Contains(modload.CmdModModule, "@") {
45 base.Fatalf("go mod init: module path must not contain '@'")
46 }
47 modload.InitMod()
48 }
49
View as plain text