Master the fundamentals and advanced features of the Go programming language
Master the fundamentals and advanced features of the Go programming language
Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.
This course is also available on my website and as an ebook on leanpub. Please leave a ⭐ as motivation if this was helpful!
Getting Started
Chapter I
Chapter II
Chapter III
Chapter IV
Appendix
Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.
It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed, and safety of a statically typed and compiled language with the ease of programming of a dynamic language to make programming more fun again.
In a way, they wanted to combine the best parts of Python and C++ so that they can build reliable systems that can take advantage of multi-core processors.
Before we start this course, let us talk about why we should learn Go.
Go is quite easy to learn and has a supportive and active community.
And being a multipurpose language you can use it for things like backend development, cloud computing, and more recently, data science.
Which makes it highly suitable for distributed systems. Projects such as Kubernetes and Docker are written in Go.
Go has just 25 keywords which makes it easy to read, write and maintain. The language itself is concise.
But don't be fooled by the simplicity, Go has several powerful features that we will later learn in the course.
Go is growing fast and is being adopted by companies of any size. and with that, comes new high-paying job opportunities.
I hope this made you excited about Go. Let's start this course.
In this tutorial, we will install Go and setup our code editor.
We can install Go from the downloads section.
These instructions are from the official website.
Open the package file you downloaded and follow the prompts to install Go.
The package installs the Go distribution to /usr/local/go. The package should put the /usr/local/go/bin directory in your PATH environment variable.
You may need to restart any open Terminal sessions for the change to take effect.
Verify that you've installed Go by opening a command prompt and typing the following command:
$ go version/usr/local/go folder (if it exists),
then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.1.linux-amd64.tar.gzNote: You may need to run the command as root or through sudo.
Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.
/usr/local/go/bin to the PATH environment variable.
You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):export PATH=$PATH:/usr/local/go/binNote: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.
$ go versionBy default, the installer will install Go to Program Files or Program Files (x86). You can change the location as needed. After installing, you will need to close and reopen any open command prompts so that changes to the environment made by the installer are reflected at the command prompt.
$ go versionIn this course, I will be using VS Code and you can download it from here.
Feel free to use any other code editor you prefer.
Make sure to also install the Go extension which makes it easier to work with Go in VS Code.
This is it for the installation and setup of Go, let's start the course and write our first hello world!
Let's write our first hello world program, we can start by initializing a module. For that, we can use the go mod command.
$ go mod init exampleBut wait...what's a module? Don't worry we will discuss that soon! But for now, assume that the module is basically a collection of Go packages.
Moving ahead, let's now create a main.go file and write a program that simply prints hello world.
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}If you're wondering, fmt is part of the Go standard library which is a set of core packages provided by the language.
Now, let's quickly break down what we did here, or rather the structure of a Go program.
First, we defined a package such as main.
package mainThen, we have some imports.
import "fmt"Last but not least, is our main function which acts as an entry point for our application, just like in other languages like C, Java, or C#.
func main() {
...
}Remember, the goal here is to keep a mental note, and later in the course, we'll learn about functions, imports, and other things in detail!
Finally, to run our code, we can simply use go run command.
$ go run main.go
Hello World!Congratulations, you just wrote your first Go program!
In this tutorial, we will learn about variables. We will also learn about the different data types that Go provides us.
Let's start with declaring a variable.
This is also known as declaration without initialization:
var foo stringDeclaration with initialization:
var foo string = "Go is awesome"Multiple declarations:
var foo, bar string = "Hello", "World"
// OR
var (
foo string = "Hello"
bar string = "World"
)Type is omitted but will be inferred:
var foo = "What's my type?"Shorthand declaration, here we omit var keyword and type is always implicit. This is how we will see variables being declared most of the time. We also use the := for declaration plus assignment.
foo := "Shorthand!"Note: Shorthand only works inside function bodies.
We can also declare constants with the const keyword. Which as the name suggests, are fixed values that cannot be reassigned.
const constant = "This is a constant"It is also important to note that, only constants can be assigned to other constants.
const a = 10
const b = a // ✅ Works
var a = 10
const b = a // ❌ a (variable of type int) is not constant (InvalidConstInit)Perfect! Now let's look at some basic data types available in Go. Starting with string.
In Go, a string is a sequence of bytes. They are declared either using double quotes or backticks which can span multiple lines.
var name string = "My name is Go"
var bio string = `I am statically typed.
I was designed at Google.`Next is bool which is used to store boolean values. It can have two possible values - true or false.
var value bool = false
var isItTrue bool = trueOperators
We can use the following operators on boolean types
| Type | Syntax |
|---|---|
| Logical | && || ! |
| Equality | == != |
Now, let's talk about numeric types.
Signed and Unsigned integers
Go has several built-in integer types of varying sizes for storing signed and unsigned integers
The size of the generic int and uint types are platform-dependent. This means it is 32-bits wide on a 32-bit system and 64-bits wide on a 64-bit system.
var i int = 404 // Platform dependent
var i8 int8 = 127 // -128 to 127
var i16 int16 = 32767 // -2^15 to 2^15 - 1
var i32 int32 = -2147483647 // -2^31 to 2^31 - 1
var i64 int64 = 9223372036854775807 // -2^63 to 2^63 - 1Similar to signed integers, we have unsigned integers.
var ui uint = 404 // Platform dependent
var ui8 uint8 = 255 // 0 to 255
var ui16 uint16 = 65535 // 0 to 2^16-1
var ui32 uint32 = 2147483647 // 0 to 2^32-1
var ui64 uint64 = 9223372036854775807 // 0 to 2^64-1
var uiptr uintptr // Integer representation of a memory addressIf you noticed, there's also an unsigned integer pointer uintptr type, which is an integer representation of a memory address. It is not recommended to use this, so we don't have to worry about it.
So which one should we use?
It is recommended that whenever we need an integer value, we should just use int unless we have a specific reason to use a sized or unsigned integer type.
Byte and Rune
Golang has two additional integer types called byte and rune that are aliases for uint8 and int32 data types respectively.
type byte = uint8
type rune = int32A rune represents a unicode code point.
var b byte = 'a'
var r rune = ''Floating point
Next, we have floating point types which are used to store numbers with a decimal component.
Go has two floating point types float32 and float64. Both type follows the IEEE-754 standard.
The default type for floating point values is float64.
var f32 float32 = 1.7812 // IEEE-754 32-bit
var f64 float64 = 3.1415 // IEEE-754 64-bitOperators
Go provides several operators for performing operations on numeric types.
| Type | Syntax |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != `` = |
| Bitwise | & | ^ `> |
No open issues yet, or sync has not completed.