Skip to content

Commit

Permalink
restructure code that derives name for executable and reformat script…
Browse files Browse the repository at this point in the history
… file, remove whitespaces and set tabs as indent
  • Loading branch information
Liberatys authored and Liberatys committed Apr 23, 2019
1 parent ca328dd commit 7e2b739
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
42 changes: 21 additions & 21 deletions src/cmd/go/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package run
import (
"fmt"
"os"
"path/filepath"
"path"
"strings"

"cmd/go/internal/base"
Expand All @@ -23,7 +23,7 @@ var CmdRun = &base.Command{
Short: "compile and run Go program",
Long: `
Run compiles and runs the named main Go package.
Typically the package is specified as a list of .go source files from a single directory,
Typically the package is specified as a list of .go source files,
but it may also be an import path, file system path, or pattern
matching a single known package, as in 'go run .' or 'go run my/cmd'.
Expand Down Expand Up @@ -95,10 +95,10 @@ func runRun(cmd *base.Command, args []string) {
base.Fatalf("go run: no go files listed")
}
cmdArgs := args[i:]

if p.Error != nil {
base.Fatalf("%s", p.Error)
}

p.Internal.OmitDebug = true
if len(p.DepsErrors) > 0 {
// Since these are errors in dependencies,
Expand All @@ -118,25 +118,25 @@ func runRun(cmd *base.Command, args []string) {
base.Fatalf("go run: cannot run non-main package")
}
p.Target = "" // must build - not up to date
var src string
if len(p.GoFiles) > 0 {
src = p.GoFiles[0]
} else if len(p.CgoFiles) > 0 {
src = p.CgoFiles[0]
} else {
// this case could only happen if the provided source uses cgo
// while cgo is disabled.
hint := ""
if !cfg.BuildContext.CgoEnabled {
hint = " (cgo is disabled)"
if p.Internal.CmdlineFiles {
//set executable name if go file is given as cmd-argument
var src string
if len(p.GoFiles) > 0 {
src = p.GoFiles[0]
} else if len(p.CgoFiles) > 0 {
src = p.CgoFiles[0]
} else {
// this case could only happen if the provided source uses cgo
// while cgo is disabled.
hint := ""
if !cfg.BuildContext.CgoEnabled {
hint = " (cgo is disabled)"
}
base.Fatalf("go run: no suitable source files%s", hint)
}
base.Fatalf("go run: no suitable source files%s", hint)
}
p.Internal.ExeName = src[:len(src)-len(".go")] // set name of first go file as temporary executable name
if len(args) == 1 && args[0] == "." {
p.Internal.ExeName = p.Dir // set name of directory as temporary executable name
} else if len(args) == 1 && filepath.Ext(args[0]) == "" {
p.Internal.ExeName = p.Dir // set name of directoy as temporary executable name if args[0] is a directory.
p.Internal.ExeName = src[:len(src)-len(".go")]
} else {
p.Internal.ExeName = path.Base(p.ImportPath)
}
a1 := b.LinkAction(work.ModeBuild, work.ModeBuild, p)
a := &work.Action{Mode: "go run", Func: buildRunProgram, Args: cmdArgs, Deps: []*work.Action{a1}}
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/go/testdata/script/run_set_executable_name.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ stderr 'z'
-- m/x/y/z/foo.go --
package main
import(
"os"
"os"
"path/filepath"
)
func main() {
println(filepath.Base(os.Args[0]))
func main() {
println(filepath.Base(os.Args[0]))
}

-- m/x/y/z/foo.go --
package main
import(
"os"
"os"
"path/filepath"
)
func main() {
println(filepath.Base(os.Args[0]))
func main() {
println(filepath.Base(os.Args[0]))
}

-- m/x/y/z/foo.go --
package main
import(
"os"
"os"
"path/filepath"
)
func main() {
println(filepath.Base(os.Args[0]))
func main() {
println(filepath.Base(os.Args[0]))
}

0 comments on commit 7e2b739

Please sign in to comment.