Learn the Go Programming Language: Start Here
The Go programming language (aka Golang) is a versatile programming language that is used for building low-level infrastructure, web applications and services, cloud native applications, distributed systems, networked applications, concurrent processing tasks, networking tools, proxies and command-line tools, and also works well for containers, IoT and embedded systems. The main reason why Go is so popular is its speed. Developers who use Golang as their go-to option will tell you that it has one of the fastest compile times of any language. Unlike Python, Go is a compiled language that is statically typed, explicit and modeled after the C programming language. Even so, Go was inspired by the simplicity of Python. And because Go uses goroutines (a lightweight execution thread and function that executes concurrently with the rest of a Go program), it can produce programs that are able to run multiple processes at the same time (concurrency). Go was created in 2007 by Google to eliminate some of the issues that they identified when developing software internally (such as slow build times and better concurrency). Google also wanted to create a language that would help make the development process more productive and scalable. What they ultimately wanted was a simple, efficient and easy-to-use programming language. Hence, Golang.
Pros and Cons of Golang
| PRO | CON |
| Simplicity and easy to learn and use | Allows for sloppy coding practices |
| Concurrency | Lack of libraries |
| Fast compilation | Static typing can create less flexible code |
| Built-in testing (with good error messages) | Comparatively small community |
| Garbage collection | While compile time is very fast, compiled code isn’t the fastest. |
| Can use all CPU cores efficiently | No inheritance |
| Best-in-class documentation | |
| Fairly powerful profiling tools | |
| Eliminates the distinction between synchronous and asynchronous code |
Installing Go on Ubuntu Linux
Before you can use Go, it must be installed. There are several methods for installing Go, but we’ll stick with the two primary paths. Since we’re dealing with Ubuntu, one route to installing Go is via Snap, which is done with this command:
sudo snap install go --classic
sudo apt-get install golang-go -y
go version
go version go1.22.1 linux/amd64
Your First Go Application
As you’ve probably predicted, the first application we’ll create is “Hello, World” — because that’s where every language tutorial should begin. Create a new directory to house your Go projects:
mkdir go_projects
cd go_projectsNow create the “Hello, World” file with this command:
nano hello.goIn that file, paste the following:
package main
import "fmt"
func main() {
fmt.Println("Hello, New Stack")
}
What does it all mean? Let’s break it down, line by line.
- package main: This line calls the main package, which is included with Go.
- import “fmt”: This imports the package that is used to format basic strings, values, inputs, and outputs.
- func main(): This is a special type of function, and it is the entry point of the executable programs. Every executable program must contain this function.
- fmt.Println(“Hello, New Stack”): This uses the default formats for its operands and writes to standard output.
go run hello.go
Hello, New StackYou’ll notice that Go does not create an executable file. Unlike languages like Python, Go includes the ability to generate an executable application from your .go file. Sticking with our hello.go file, the command for this would be:
go build hello.go
./hello
- GOOS: defines the OS to be compiled for
- GOARCH: defines the architecture to be compiled for
GOOS=darwin GOARCH=amd64 go build hello.go
GOOS=darwin GOARCH=arm64 go build hello.go
go tool dist list