github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/internal/run/run.go (about) 1 // Copyright 2011 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 // Package run implements the ``go run'' command. 6 package run 7 8 import ( 9 "fmt" 10 "os" 11 "strings" 12 13 "cmd/go/internal/base" 14 "cmd/go/internal/cfg" 15 "cmd/go/internal/load" 16 "cmd/go/internal/str" 17 "cmd/go/internal/work" 18 ) 19 20 var CmdRun = &base.Command{ 21 UsageLine: "go run [build flags] [-exec xprog] package [arguments...]", 22 Short: "compile and run Go program", 23 Long: ` 24 Run compiles and runs the named main Go package. 25 Typically the package is specified as a list of .go source files, 26 but it may also be an import path, file system path, or pattern 27 matching a single known package, as in 'go run .' or 'go run my/cmd'. 28 29 By default, 'go run' runs the compiled binary directly: 'a.out arguments...'. 30 If the -exec flag is given, 'go run' invokes the binary using xprog: 31 'xprog a.out arguments...'. 32 If the -exec flag is not given, GOOS or GOARCH is different from the system 33 default, and a program named go_$GOOS_$GOARCH_exec can be found 34 on the current search path, 'go run' invokes the binary using that program, 35 for example 'go_nacl_386_exec a.out arguments...'. This allows execution of 36 cross-compiled programs when a simulator or other execution method is 37 available. 38 39 The exit status of Run is not the exit status of the compiled binary. 40 41 For more about build flags, see 'go help build'. 42 For more about specifying packages, see 'go help packages'. 43 44 See also: go build. 45 `, 46 } 47 48 func init() { 49 CmdRun.Run = runRun // break init loop 50 51 work.AddBuildFlags(CmdRun) 52 CmdRun.Flag.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "") 53 } 54 55 func printStderr(args ...interface{}) (int, error) { 56 return fmt.Fprint(os.Stderr, args...) 57 } 58 59 func runRun(cmd *base.Command, args []string) { 60 work.BuildInit() 61 var b work.Builder 62 b.Init() 63 b.Print = printStderr 64 i := 0 65 for i < len(args) && strings.HasSuffix(args[i], ".go") { 66 i++ 67 } 68 var p *load.Package 69 if i > 0 { 70 files := args[:i] 71 for _, file := range files { 72 if strings.HasSuffix(file, "_test.go") { 73 // GoFilesPackage is going to assign this to TestGoFiles. 74 // Reject since it won't be part of the build. 75 base.Fatalf("go run: cannot run *_test.go files (%s)", file) 76 } 77 } 78 p = load.GoFilesPackage(files) 79 } else if len(args) > 0 && !strings.HasPrefix(args[0], "-") { 80 pkgs := load.PackagesAndErrors(args[:1]) 81 if len(pkgs) == 0 { 82 base.Fatalf("go run: no packages loaded from %s", args[0]) 83 } 84 if len(pkgs) > 1 { 85 var names []string 86 for _, p := range pkgs { 87 names = append(names, p.ImportPath) 88 } 89 base.Fatalf("go run: pattern %s matches multiple packages:\n\t%s", args[0], strings.Join(names, "\n\t")) 90 } 91 p = pkgs[0] 92 i++ 93 } else { 94 base.Fatalf("go run: no go files listed") 95 } 96 cmdArgs := args[i:] 97 98 if p.Error != nil { 99 base.Fatalf("%s", p.Error) 100 } 101 p.Internal.OmitDebug = true 102 if len(p.DepsErrors) > 0 { 103 // Since these are errors in dependencies, 104 // the same error might show up multiple times, 105 // once in each package that depends on it. 106 // Only print each once. 107 printed := map[*load.PackageError]bool{} 108 for _, err := range p.DepsErrors { 109 if !printed[err] { 110 printed[err] = true 111 base.Errorf("%s", err) 112 } 113 } 114 } 115 base.ExitIfErrors() 116 if p.Name != "main" { 117 base.Fatalf("go run: cannot run non-main package") 118 } 119 p.Target = "" // must build - not up to date 120 var src string 121 if len(p.GoFiles) > 0 { 122 src = p.GoFiles[0] 123 } else if len(p.CgoFiles) > 0 { 124 src = p.CgoFiles[0] 125 } else { 126 // this case could only happen if the provided source uses cgo 127 // while cgo is disabled. 128 hint := "" 129 if !cfg.BuildContext.CgoEnabled { 130 hint = " (cgo is disabled)" 131 } 132 base.Fatalf("go run: no suitable source files%s", hint) 133 } 134 p.Internal.ExeName = src[:len(src)-len(".go")] // name temporary executable for first go file 135 a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p) 136 a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}} 137 b.Do(a) 138 } 139 140 // buildRunProgram is the action for running a binary that has already 141 // been compiled. We ignore exit status. 142 func buildRunProgram(b *work.Builder, a *work.Action) error { 143 cmdline := str.StringList(work.FindExecCmd(), a.Deps[0].Target, a.Args) 144 if cfg.BuildN || cfg.BuildX { 145 b.Showcmd("", "%s", strings.Join(cmdline, " ")) 146 if cfg.BuildN { 147 return nil 148 } 149 } 150 151 base.RunStdin(cmdline) 152 return nil 153 }