github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/modcmd/init.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // go mod init 6 7 package modcmd 8 9 import ( 10 "context" 11 12 "github.com/go-asm/go/cmd/go/base" 13 "github.com/go-asm/go/cmd/go/modload" 14 ) 15 16 var cmdInit = &base.Command{ 17 UsageLine: "go mod init [module-path]", 18 Short: "initialize new module in current directory", 19 Long: ` 20 Init initializes and writes a new go.mod file in the current directory, in 21 effect creating a new module rooted at the current directory. The go.mod file 22 must not already exist. 23 24 Init accepts one optional argument, the module path for the new module. If the 25 module path argument is omitted, init will attempt to infer the module path 26 using import comments in .go files, vendoring tool configuration files (like 27 Gopkg.lock), and the current directory (if in GOPATH). 28 29 See https://golang.org/ref/mod#go-mod-init for more about 'go mod init'. 30 `, 31 Run: runInit, 32 } 33 34 func init() { 35 base.AddChdirFlag(&cmdInit.Flag) 36 base.AddModCommonFlags(&cmdInit.Flag) 37 } 38 39 func runInit(ctx context.Context, cmd *base.Command, args []string) { 40 if len(args) > 1 { 41 base.Fatalf("go: 'go mod init' accepts at most one argument") 42 } 43 var modPath string 44 if len(args) == 1 { 45 modPath = args[0] 46 } 47 48 modload.ForceUseModules = true 49 modload.CreateModFile(ctx, modPath) // does all the hard work 50 }