Introduction to the Go programming language
This post is the start of a new series about Go.
Go is an awesome, simple, modern, fast programming language.
It’s compiled, open source, strongly typed.
It was created by Google engineers with these main goals:
- make their projects compile (and run) faster
- be simple so people can pick it up in little time
- be low level enough but also avoid some pitfalls of being too low level
- be portable (compiled Go programs are binaries that do not require other files to run and are cross-platform, so they can be distributed easily)
- be boring, stable, predictable, offer less opportunities to make mistakes
- make it easy to take advantage of multiprocessor systems
and it was meant to be a replacement for C and C++.
Also, it was built to work along with C and C++ codebases, thanks to its C interoperability features.
Go can be used for many different needs, and it can solve both simple needs and very complex ones.
You can create command line utilities, networking servers, and it is widely used in many different scenarios.
Docker and Kubernetes are written in Go.
My favorite Static Site Generator (Hugo) is written in Go.
Caddy, a quite popular web server, is written in Go.
There’s lots of different widely used tools that use this programming language under the hood.
This handbook will introduce you to this language.
Here are a few things you should know before we dive into the specifics of the language.
First, https://go.dev is the homepage of the language. This will be your go-to resource to:
- Download the Go binaries (the
go
command and other related tools) from https://go.dev/doc/install - Reference the official Go documentation https://go.dev/doc/
- See all the the Go packages https://pkg.go.dev/
- Access the Go Playground https://go.dev/play/
- … and more
Go to https://go.dev/doc/install and download the package for your Operating System.
Run the installer, and at the end of the process you will have the go
command available in your terminal:
Open the terminal and run go version
and you should see something like this:
NOTE: you might have to open a new terminal before you can run the program, as the installer added the Go binaries folder to the path.
The exact location of the Go installation files will depend on your Operating System.
On macOS it’s under /usr/local/go
, with binaries in /usr/local/go/bin
.
On Windows it will be under C:\Program Files\go
.
The Windows and Mac installers will set the Go binaries path automatically.
On a Mac you might also want to install Go via Homebrew using brew install golang
. This will make it easier to update later.
On Linux you will have to add the Go binaries folder to your terminal path before you can run the go
command after unpackaging the Linux package to /usr/local/go
with
echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.profile
source $HOME/.profile
I recommend VS Code (aka VS Code) as your editor.
Read Go in VS Code for a quick “up and running” setup. At the bare miminum, install the Go extension.
This extension will make your life easier providing IntelliSense (syntax highlighting, autocompletion, on hover information, error highlighting…) and other things like auto formatting, menu options to install packages, testing, and more.
I recommend you enable in the VS Code Settings “Format on Save” and “Format on Paste”:
Comments in Go are done using the usual C / C++ / JavaScript / Java syntax:
// this is a line comment
/*
multi
line
comment
*/
The language has no semantically significant whitespace. Like C, C++, Rust, Java, JavaScript. Unlike Python, where whitespace is meaningful and is used to create blocks instead of curly brackets.
Semicolons are optional, like in JavaScript. Unlike C, C++, Rust or Java.
Go takes indentation and visual order very seriously.
When we install Go we also get access to the gofmt
command line tool which we can use to format Go programs. VS Code uses that under the hood to format Go source files.
This is very interesting and innovative because formatting and issues like tabs vs spaces or “should I put the curly brackets on the same line of the loop definition or in the next line” are a huge waste of time.
The language creators defined the rules, everyone uses those.
This is great for projects with large teams.
In the next days I’ll introduce new Go concepts getting more and more into the language. Stay tuned.
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025