From 2a99f2fb2a736a9a4575f937cf7af57589beba9b Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Thu, 4 Apr 2013 12:04:35 +1100 Subject: [PATCH] cmd/go: run main package when no files are listed Fixes 5164. R=golang-dev, iant, adg CC=golang-dev https://golang.org/cl/8119049 --- doc/go1.1.html | 9 +++++++++ src/cmd/go/doc.go | 3 ++- src/cmd/go/run.go | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/doc/go1.1.html b/doc/go1.1.html index c367875c4f..5f56a6afe0 100644 --- a/doc/go1.1.html +++ b/doc/go1.1.html @@ -392,6 +392,15 @@ To update pre-Go 1 code to Go 1.1, use a Go 1.0 tool chain to convert the code to Go 1.0 first.

+

Changes to the go run command

+ +

+The go run command now runs all files in the current working +directory if no file arguments are listed. Also, the go run +command now returns an error if test files are provided on the command line. In +this sense, "go run" replaces "go run *.go". +

+

Additional platforms

diff --git a/src/cmd/go/doc.go b/src/cmd/go/doc.go index 6ffcf9ab6c..3b22e0f04a 100644 --- a/src/cmd/go/doc.go +++ b/src/cmd/go/doc.go @@ -367,9 +367,10 @@ Compile and run Go program Usage: - go run [build flags] gofiles... [arguments...] + go run [build flags] [gofiles...] [arguments...] Run compiles and runs the main package comprising the named Go source files. +If no files are named, it compiles and runs all non-test Go source files. For more about build flags, see 'go help build'. diff --git a/src/cmd/go/run.go b/src/cmd/go/run.go index b505693635..d8ba4dbddd 100644 --- a/src/cmd/go/run.go +++ b/src/cmd/go/run.go @@ -8,14 +8,16 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "strings" ) var cmdRun = &Command{ - UsageLine: "run [build flags] gofiles... [arguments...]", + UsageLine: "run [build flags] [gofiles...] [arguments...]", Short: "compile and run Go program", Long: ` Run compiles and runs the main package comprising the named Go source files. +If no files are named, it compiles and runs all non-test Go source files. For more about build flags, see 'go help build'. @@ -44,7 +46,18 @@ func runRun(cmd *Command, args []string) { } files, cmdArgs := args[:i], args[i:] if len(files) == 0 { - fatalf("go run: no go files listed") + allFiles, err := filepath.Glob("*.go") + if err != nil { + fatalf("go run: %s", err) + } + for _, file := range allFiles { + if !strings.HasSuffix(file, "_test.go") { + files = append(files, file) + } + } + if len(files) == 0 { + fatalf("go run: no go files found") + } } for _, file := range files { if strings.HasSuffix(file, "_test.go") { -- 2.50.0