Out of the box, Go ships with many development tools. You access these tools via the go
command. They include a compiler, code formatter, linter, dependency manager, test runner, and more. As we learn how to build high-quality idiomatic Go, we’ll explore many of these tools throughout the book. Let’s start with the ones that we use to build Go code and use the go
command to build a simple application.
go run
We’ll start with go run
. Create a directory called ch1, open up a text editor, enter the following text, and save it inside ch1 to a file named hello.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
After the file is saved, open up a terminal or command prompt and type:
go run hello.go
You should see Hello, world!
printed in the console. If you look inside the directory after running the go run
command, you see that no binary has been saved there; the only file in the directory is the hello.go file we just created. You might be thinking: I thought Go was a compiled language. What’s going on?
The go run
command does in fact compile your code into a binary. However, the binary is built in a temporary directory. The go run
command builds the binary, executes the binary from that temporary directory, and then deletes the binary after your program finishes. This makes the go run
command useful for testing out small programs or using Go like a scripting language.
Use go run
when you want to treat a Go program like a script and run the source code immediately.
go build
Most of the time you want to build a binary for later use. That’s where you use the go build
command. On the next line in your terminal, type:
go build hello.go
This creates an executable called hello
(or hello.exe on Windows) in the current directory. Run it and you unsurprisingly see Hello, world!
printed on the screen.
The name of the binary matches the name of the file or package that you passed in. If you want a different name for your application, or if you want to store it in a different location, use the -o
flag. For example, if we wanted to compile our code to a binary called “hello_world,” we would use:
go build -o hello_world hello.go
Getting Third-Party Go Tools
While some people choose to distribute their Go programs as pre-compiled binaries, tools written in Go can also be built from source and installed into your Go workspace via the go install
command.
Go’s method for publishing code is a bit different than most other languages. Go developers don’t rely on a centrally hosted service, like Maven Central for Java or the NPM registry for JavaScript. Instead, they share projects via their source code repositories. The go install
command takes an argument, which is the location of the source code repository of the project you want to install, followed by an @
and the version of the tool you want (if you just want to get the latest version, use @latest
). It then downloads, compiles, and installs the tool into your $GOPATH/bin directory.
Let’s look at a quick example. There’s a great Go tool called hey
that load tests HTTP servers. You can point it at the website of your choosing or an application that you’ve written. Here’s how to install hey
with the go install
command:
$ go install github.com/rakyll/hey@latest
go: downloading github.com/rakyll/hey v0.1.4
go: downloading golang.org/x/net v0.0.0-20181017193950-04a2e542c03f
go: downloading golang.org/x/text v0.3.0
This downloads hey
and all of its dependencies, builds the program, and installs the binary in your $GOPATH/bin directory.
Now that we have built and installed hey, we can run it with:
$ hey https://www.golang.org
Summary:
Total: 0.6864 secs
Slowest: 0.3148 secs
Fastest: 0.0696 secs
Average: 0.1198 secs
Requests/sec: 291.3862
Of course, you don’t need to leave tools written in Go in your Go workspace; they are regular executable binaries and can be stored anywhere on your computer. Likewise, you don’t have to distribute programs written in Go using go install
; you can put a binary up for download. However, go install
is a very convenient way to distribute Go programs to other Go developers.
Formatting Your Code
The Go development tools include a command, go fmt
, which automatically reformats your code to match the standard format. It does things like fixing up the whitespace for indentation, lining up the fields in a struct, and making sure there is proper spacing around operators.
There’s an enhanced version of go fmt
available called goimports
that also cleans up your import statements. It puts them in alphabetical order, removes unused imports, and attempts to guess any unspecified imports. Its guesses are sometimes inaccurate, so you should insert imports yourself.
You can download goimports
with the command go install golang.org/x/tools/cmd/goimports@latest
.