Go Programming

Golang gopher

Welcome to our main course on the Go programming language (Golang)!

This page contains links to Go tutorials, as well as an in-depth one page tutorial to help you get up and running quickly with Golang.

GoLang Tutorials

Comments
Variables

Constants
Data Types
– Booleans
– Strings
– Integers
– Floats
– Complex Numbers

Go is a programming language that is popular due to its’ ease of use, speed, and safety features.

Go is one of the fastest compiled languages in the world, and it is almost as easy to learn and use as Python.

One of the features unique to Go is its’ garbage collector, which is used to periodically clear memory space that is no longer needed.

Go isn’t quite as safe as Rust, but it is much easier to learn.

What is Go?

Created at Google, Go is a statically typed, compiled programming language.

What makes Go so appealing is its’ unique combination of features:

  • Go is fast: Its speed is comparable to other high performance languages like C, C++, and Rust.
  • Go is easy to learn and use: It’s great for a first language or a startup.
  • Go is safe: It offers safety features similar to Rust (although Rust is definitely safer).

You can think of Go as a language that manages to capture most of the speed and safety of Rust while being almost as easy to learn as Python.

In order to understand Go, it’s helpful to review some of its’ basic features like the garbage collector.

The Golang Garbage Collector

The garbage collector is a bit unique, and its one of the things that make the Go language stand out. It results in much greater speed and safety, and it works in the background so that the programmer doesn’t have to worry about it.

The garbage collector is a background process that runs periodically. It scans the computer memory and clears any unnecessary space. This is great because normally after memory is allocated, it can only be cleared explicitly.

Most programming languages don’t worry about this, which causes them to take up more memory than is strictly necessary. In C/C++, programmers have to work hard to explicitly clean up, essentially doing the ‘garbage collection’ themselves.

The garbage collector in Go does this for the programmer, and this results in far less memory consumption.

Comments

As with many other languages, Go supports both single and multi-line comments.

Single lines can be commented out using two forward slashes // and multiple lines can be commented using a slash asterisk: /* */.

func main() {

    // This is a single line comment

    /* This is a
    multi-line comment
    */
}

Comments are useful not only when annotating code but also for commenting out code while programming and debugging. In other words, comments are used as a general rule to tell the compiler what to execute (and what not to execute).

Main article: Comments

Variables

Variables in Go can be declared using either the var keyword or a colon before the assignment operator :=.

// Declare variable using var keyword
var var1 = 42

// Declare variable using :=
var2 := "Hello!"

//Declare variable without value assignment
var var3 string

// Value can be assigned after declaration
var3 = "Goodbye!"

There are some nuances and limitations with these different methods; check out the main tutorial on to learn all the juicy details.

Main article: Variables

Constants

We can use constants in cases when we know that the value of a variable will not need to be changed. Constants are declared using the keyword const, and the most common convention is to name them using UPPERCASE letters:

const MYNAME = "Bob"
const MYNUM = 42
const MYBOOL = true

Constants can be numeric types, Booleans, or strings. Once a value is assigned, a constant can never be changed. This makes them safer but also more restrictive than variables.

Main article: Constants

Data Types

There are four basic data type classifications in Golang: Boolean, string, numeric, and derived types.

Main article: Data Types

Boolean Data Types

Boolean types can have hold a value of either true or false. They can be declared in a variety of ways:

var b1 bool = true // Explicit declaration of Boolean variable
var b2 = true      // Implicit declaration; compiler infers type
b3 := true         // Simple way of declaring Boolean type var
var b4 bool        // Will default to 'false'

const B1 = false   // Boolean typed constant

Main article: Boolean Data Type