Running Go Code
Explore the two main ways to run Go code: using the go build command to compile and create executables, and using go run to execute code like a script. Learn how to name executable files and understand key Go formatting and syntax rules to avoid common errors.
We'll cover the following...
We now need to know how to execute hw.go or any other Go application. There are two ways to execute Go code:
As a compiled language using
go build.As a scripting language using
go run.
So, let’s find out more about these two ways of running Go code.
Compiling Go code
In order 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 the go build command requires an additional step for running our code.
The generated executable is automatically named after the source code file name without the .go file extension. Therefore, because of the hw.go source file, the executable will be called hw. In case this is not what we want, go build supports the -o option, which allows us to change the file name and the path of the generated executable file. As an example, if we want to name the executable file helloWorld, we 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, we need to execute the generated executable binary file on our own. In our case, this means executing either hw or helloWorld. This is shown in the next output:
# go build hw.go# ./hwHello World!
Now that we know how to compile Go code, let’s continue with using Go as a scripting language.
Using Go like 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. In our case, we can do the following:
# go run hw.goHello World!
If we want to test our code, then using go run is a better choice. However, if we want to create and distribute an executable binary, then go build is the way to go.
Let’s try our code
Let’s try the commands that we have learned above one by one in the following widget. Please follow the commented instructions given in script.sh:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}Important formatting and coding rules
We should know that Go comes with some strict formatting and coding rules that help the developer avoid beginner mistakes and bugs—once we learn these rules and Go gofmt) that can format our code for us, so we never have to think about it.
The following is a list of important Go rules that will help us while reading this chapter:
Go code is delivered in packages, and we are free to use the functionality found in existing packages. However, if we are going to import a package, we should use some of this functionality—there are some exceptions to this rule that mainly have to do with initializing connections, but they are not important for now.
We either use a variable or we do not declare it at all. This rule helps us avoid errors such as misspelling an existing variable or function name.
There is only one way to format curly braces in Go.
Coding blocks in Go are embedded in curly braces, even if they contain just a single statement or no statements at all.
Go functions can return multiple values.
We cannot automatically convert between different data types, even if they are of the same kind. As an example, we cannot implicitly convert an integer to a floating point.
Go has more rules but these rules are the most important ones and will keep us going for most of the course. We 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:
Although it looks just fine if we try to execute it, we will be fairly disappointed because the code will not compile, and therefore we will get the following syntax error message:
# go run curly.go# command-line-arguments./curly.go:6: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 automatically 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: