With current-day Golang, go run .
isn't sufficient by itself.
First, you need to initialize the folder with a one-time setup:
go mod init noname
Now you can run your program with:
go run .
Although it doesn't explain the significance of the name chosen for use with go mod init
, this method is demonstrated here: Tutorial: Get started with Go. A key point is that "Enable dependency tracking for your code" is optional for the most trivial cases like a typical go run .
, but is essentially required for any non-trivial applications or workflows (for example using non-standard imports).
The use of noname
in the above example can be replaced by anything that looks like a relative path-name. It basically doesn't matter what you use as the module name if you're building a main module that you don't plan to import elsewhere.
A basic explanation of go mod init
was previously asked for here: Can someone please dumb down go mod init for me?
Note: Without go mod init (somename)
, go run .
returns an error like so...
$ go run .
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
There may be alternative solutions: Error message "go: go.mod file not found in current directory or any parent directory; see 'go help modules'"