liveBook · Manning

Go run

As explained in Go in Action, Second Edition

go run is the standard Go command that compiles and executes a Go program in one operation.

go run is the standard Go command for compiling and executing a program in one operation. It supports fast iteration without requiring a separate build step. section 1.6 section 2.4.3

Run it from the program’s directory with a Go source file such as main.go. The command compiles the program and immediately displays its output. section 2.4.3

Run a word-counting program

Execute `go run main.go` to process the program. For a file-backed version, create `words.txt`, run the same command, and change the file contents to test the result. Removing the file exercises the program’s error handling. section 2.4.3 section 2.6.5

Listing 2.5 Running the program
$ go run main.go
Found 4 words

Shows the command and output for running the initial program. section 2.4.3

Listing 2.28 Running the application on words.txt
$ echo this is four words > words.txt
$ go run main.go
found 4 words

Shows go run processing a words.txt input file. section 2.6.5

Listing 2.29 Running the application with no input file
$ rm words.txt
$ go run main.go
2022/05/05 20:33:39 open words.txt: no such file or directory

Shows the error produced when the expected input file is missing. section 2.6.5

How Go in Action, Second Edition covers it

How deep it goes
Demonstrates command usage and input-file testing but does not explain compilation internals.
Examples
Demonstrates main.go execution, word counting, words.txt input, and missing-file behavior.
What it assumes
Assumes a Go installation, a Go source file, and any input files expected by the program.
What you can do after
Apply go run to execute Go programs quickly and test file-input and error-handling behavior.
How it connects
Connects go run with go build, go fmt, go test, and error handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.