Build and Run a Basic Go Program

Learn to build and execute programs in Go.

After that quick recap of some commonly used concepts of Go, let's try to use what we've learned so far. We'll put together a small Go program.

Let's start with a simple “Hello World” program.

Note: If you're already familiar with building small programs in Go, feel free to skip this lesson.

Writing “Hello World” in Go

Press + to interact
package main
import "fmt"
func main() {
fmt.Printf("Hello World")
}

Let's try to understand what's going on in the code above:

  • Line 1: We declare a package called main, which is the namespace where the code in the file lies. This namespace is typically used for the main function, which we'll get into in a bit.

  • Line 2: We then import the built-in fmt package. As we have already seen in the previous lesson, it's used for I/O formatting in Go.

  • Line 4: We declare the main function. The main function is a special function and serves as the point of entry when the code is executed. It doesn't take any arguments and is automatically run by Go when the code is run using commands like go run.

  • Line 5: We print the string Hello World by passing it as an argument to the Printf function of the fmt package.

Finding all odd numbers in a list using Go

Let's try to do something a bit more complex and try to find out all the odd numbers from an array of integers using Go.

We'll implement a function called findAllOddNumbers.

This will use some of the Go concepts we learned earlier. It will take an array of integers and return all the odd numbers in it.

Press + to interact
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
fmt.Printf("Printing all odd numbers ", findAllOddNumbers(numbers))
}
func findAllOddNumbers(numbers []int) []int {
result := []int{}
for _, num := range numbers {
if num % 2 != 0 {
result = append(result, num)
}
}
return result
}

In the above piece of code:

  • Line 9: We define the function findAllOddNumbers, which takes a slice of int as input and returns another slice of int.

  • Line 10: We declare an empty slice called result. This is where we'll store the final result.

  • Lines 12–14: We iterate over a slice. We use the keyword range, which returns the index of the element and the actual element in the array. Since we don't need an index for our use case here, we store it in _. In Go, this is a way to ignore a variable if we don't need it. We store the actual number in the variable num.

Things are pretty simple after that:

  • In line 12, we check if the number is odd. And if it is, we store it in a slice, result, in line 13. This slice is then returned in line 16.

  • The function is called from main, and the return value is printed in line 6. The output, as expected, has all the odd elements [1, 3, 5] .

Executing the code

Now that we understand the little code snippet above, let's see what happens when the Run" button is clicked in the above code widget.

When we press the “Run” button, it builds the code using go build main.go. This produces an executable. This executable then runs to execute the code. But there is something else going on under the hood. To understand that, copy the code snippet above into a file and try to execute it in a terminal.

One simple way to execute the code is:

Press + to interact
go run main.go

This compiles the source code and runs the executable. However, we won't be able to see any executable file because it doesn't generate one explicitly.

But what if we want to distribute the executable? For that, we can use the following command:

Press + to interact
go build

This command generates an executable containing the code and all of its dependencies.

The resulting file has the same name as the folder it's in. To give it a specific name, use the -o flag.

Press + to interact
go build -o <choose a name>

We may see the following error on building the code:

go: go.mod file not found in current directory or any parent directory; see ‘go help modules’

This is because Go v1.18 needs Go modules to handle dependencies. If you see the error message above, don't worry. We'll soon see what go.mod is, what this error means, and how we can resolve it.