Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: run does not support Go scripts without .go extension #26848

Closed
ijt opened this issue Aug 7, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@ijt
Copy link
Contributor

commented Aug 7, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.3 darwin/amd64

Does this issue reproduce with the latest release?

It gives a different error.

ijt:~/gopath/src/github.com/google/go-cloud/samples/guestbook/gcp$ go run deploy
go run: no go files listed

ijt:/tmp$ go1.11beta1 run hi
package hi: cannot find package "hi" in any of:
        /Users/issactrotts/sdk/go1.11beta1/src/hi (from $GOROOT)
        /Users/issactrotts/gopath/src/hi (from $GOPATH)

What operating system and processor architecture are you using (go env)?

I don't think this matters.

What did you do?

ijt:/tmp$ cat hi
package main

import "fmt"

func main() {
        fmt.Println("hi")
}
ijt:/tmp$ go run hi

What did you expect to see?

hi

What did you see instead?

go run: no go files listed

@mvdan mvdan added the NeedsDecision label Aug 7, 2018

@mvdan

This comment has been minimized.

Copy link
Member

commented Aug 7, 2018

I'm going to ask the perhaps obvious question - why do you need this? In general, all Go files should have the .go extension. This will work nicely with other tools, such as gofmt -l -w . formatting the file, or go vet catching errors if there are any.

If you're having issues because this file is a package main file in an already existing package, consider using // +build ignore instead. This is what go/types uses with its gotype.go executable.

Having said all that, I agree in a way that go run doesn't need to be strict about this. But I wonder if there's really a need to change how it works.

@ALTree

This comment has been minimized.

Copy link
Member

commented Aug 7, 2018

This look intentional. It's even explicitly documented:

$ go help run

Run compiles and runs the main package comprising the named Go source files.
A Go source file is defined to be a file ending in a literal ".go" suffix.
[...]

Stress on

A Go source file is defined to be a file ending in a literal ".go" suffix.

@ALTree

This comment has been minimized.

Copy link
Member

commented Aug 7, 2018

Correction: on tip the doc has been changed to:

$ gotip help run

Run compiles and runs the named main Go package.
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'.
@ijt

This comment has been minimized.

Copy link
Contributor Author

commented Aug 7, 2018

@mvdan, need is a strong word for it, but (edit: as far as I was aware) go run is meant to support running Go programs as scripts.

The Google Style Guide for bash scripts says it's strongly preferred to not have a filename extension on the scripts. Here's some discussion of the topic on an Ubuntu forum: https://askubuntu.com/questions/503127/should-i-save-my-scripts-with-the-sh-extension.

Python allows this:

ijt:/tmp$ cat hi
print "hi"
ijt:/tmp$ python hi
hi

So does Bash:

ijt:/tmp$ cat hi
echo "hi"
ijt:/tmp$ bash hi
hi

So does Perl, and it's recommended for perl as well.

The point is that it should be possible to run one of these Go scripts like it's a program, and whoever runs it shouldn't have to care what language it's written in if it has a shebang line (or Go's equivalent).

@dominikh

This comment has been minimized.

Copy link
Member

commented Aug 7, 2018

Go is not a scripting language, and go run isn't meant to emulate scripts. go run is a simple helper for building a set of files in a temporary place and running it.

That there are no "Go scripts" is especially obvious due to the fact that shebangs are not supported, intentionally.

The issue tracker is filled with discussions about the lack of shebang support, which ties into the whole "there's no Go scripting" issue.

Finally, supporting this syntax would preclude adding support for go run <import path>, which has come up before and is still potentially on the table.

Edit: and like @ALTree has pointed out earlier, go run <import path> is actually supported now.

@dominikh dominikh closed this Aug 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.