Go is an open-source programming language that was first created at Google by Robert Griesemer, Rob Pike, and Ken Thompson. As Google’s codebase grew in complexity and size, the company needed to create a simple, reliable programming language that was easy to learn. Go ticks all these boxes.
It is statically typed and comes with memory allocation, garbage collection, and natural concurrency. Go’s design gives the language high performance and speed.
Go is mostly used in cloud programming, DevOps, robotics, data science, and artificial intelligence.
Getting Started: Install Go
To get started with Go, install it in your development environment. Before installing it, you need to download it from the Go downloads page. Once there, click on the installer that matches your system to start the download process.
Install Go on Windows
Open the MSI file you downloaded and follow the instructions to install Go. Confirm if the installation was successful by running the following command in the command prompt.
go version
Install Go on macOS
Open the PKG file you downloaded, follow the instructions to install Go, and that’s it. You should be able to use it.
As with Windows, you can use the following command to confirm if the installation was successful:
go version
Install Go on Linux
Before installing Go, remove any previous installations using this command:
rm -rf /usr/local/go
Be very careful using the rm command with the rf options. It will permanently delete the directory you give it, including all files within. Watch for typos!
Now, extract the tarball file you just downloaded to /usr/local . Run the following command, replacing tarball-file.tar.gz with your file name:
tar -C /usr/local -xzf tarball-file.tar.gz
You also need to add the Go path to the path environment variable:
export PATH=$PATH:/usr/local/go/bin
Your First Go Program
Now that you have installed Go on your machine, you are ready to write your first Go program.
Create a Hello World Program
The first program you will create is a simple program that prints “Hello world”. Create a new file, hello-world.go, and open it with your preferred text editor. If you are using VS Code, it will prompt you to install the Go extension. Install it and add the following code to hello-world.go:
package main
import "fmt"
func main () {
fmt.Println("Hello World")
}
Before discussing what is going on in the code you have just written, run the following command to check if it works.
go run hello-world.go
It should print “Hello World” on your terminal.
Breakdown of the Program
Executable Package
package main
Go organizes source files into packages, which aids reusability. A single package can contain multiple files. Each of these files can access the values or functions from other files in the same package without importing them. Furthermore, importing a package into a Go file gives you access to all the values and functions defined in the files it contains.
The main package that you have used in the hello-world.go program is special because it tells Go that this program is executable.
Import Formatting Functions
import "fmt"
fmt is one of the packages provided by Go. It provides ways to implement formatted I/O functionalities like printing a string.
The main Function
func main
Each executable Go program has to have a main function that acts as an entry point.
In this example, you are using fmt.Println to print “Hello World” followed by a new line.
You can find the code for the Hello World program in this tutorial from this Gist.
Continue Learning About Go Programming
This post introduced you to writing programs in the Go programming language. You have learned how to install Go on the operating system you are using and how to create a simple hello world program. Dive deeper into Go by learning about variable types, arrays, and loops.