-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Mastering Go
By :

You now need to know how to execute hw.go
or any other Go application. As will be explained in the two subsections that follow, there are two ways to execute Go code: as a compiled language, using go build
, or by mimicking a scripting language, using go run
. So let us find out more about these two ways of running Go code.
To compile Go code and create a binary executable file, we need to use the go build
command. What go build
does is create an executable file for us to distribute and execute manually. This means that when using go build
, an extra step is required to run the executable file.
The generated executable is automatically named after the source code filename without the .go
file extension. Therefore, because of the hw.go
source filename, the executable will be called hw
. If this is not what you want, go build
supports the -o
option, which allows you to change the filename and the path of the generated executable file. As an example, if you want to name the executable file a helloWorld
, you should execute go build -o helloWorld hw.go
instead. If no source files are provided, go build
looks for a main
package in the current directory.
After that, you need to execute the generated executable binary file on your own. In our case, this means executing either hw
or helloWorld
. This is shown in the following output:
$ go build hw.go
$ ./hw
Hello World!
Now that we know how to compile Go code, let us continue using Go as if it were a scripting language.
The go run
command builds the named Go package, which in this case is the main
package implemented in a single file, creates a temporary executable file, executes that file, and deletes it once it is done—to our eyes, this looks like using a scripting language while the Go compiler still creates a binary executable. In our case, we can do the following:
$ go run hw.go
Hello World!
Using go run
is a better choice when testing code. However, if you want to create and distribute an executable binary, then go build
is the way to go.
You should know that Go comes with some strict formatting and coding rules that help a developer avoid rookie mistakes and bugs—once you learn these few rules and Go idiosyncrasies as well as the implications they have on your code, you will be free to concentrate on the actual functionality of your code. Additionally, the Go compiler is here to help you follow these rules with its expressive error messages and warnings. Last, Go offers standard tooling (gofmt
) that can format your code for you, so you never have to think about it.
The following is a list of important Go rules that will help you while reading this chapter:
Go has more rules, but the preceding ones are the most important and will keep you going for most of the book. You are going to see all these rules in action in this chapter as well as in other chapters. For now, let’s consider the only way to format curly braces in Go because this rule applies everywhere.
Look at the following Go program named curly.go
:
package main
import (
"fmt"
)
func main()
{
fmt.Println("Go has strict rules for curly braces!")
}
Quick tip: Enhance your coding experience with the AI Code Explainer and Quick Copy features. Open this book in the next-gen Packt Reader. Click the Copy button (1) to quickly copy code into your coding environment, or click the Explain button (2) to get the AI assistant to explain a block of code to you.
The next-gen Packt Reader is included for free with the purchase of this book. Unlock it by scanning the QR code below or visiting https://www.packtpub.com/unlock/9781805127147.
Although it looks just fine, if you try to execute it, you will be disappointed because the code will not compile and, therefore, you will get the following syntax error message:
$ go run curly.go
# command-line-arguments
./curly.go:7:6: missing function body
./curly.go:8:1: syntax error: unexpected semicolon or newline before {
The official explanation for this error message is that Go requires the use of semicolons as statement terminators in many contexts, and the compiler implicitly inserts the required semicolons when it thinks that they are necessary. Therefore, putting the opening curly brace ({
) in its own line will make the Go compiler insert a semicolon at the end of the previous line (func main()
), which is the main cause of the error message.
The correct way to write the previous code is the following:
package main
import (
"fmt"
)
func main() {
fmt.Println("Go has strict rules for curly braces!")
}
After learning about this global rule, let us continue by presenting some important characteristics of Go.