Skip to main content

Learn Go

Chapter 1: Go Fundamentals, Interfaces, and CLI Tooling Explained

In this chapter, you will learn Go's fundamental building blocks including variables, functions, and control structures, then advance to interfaces and type assertions, and discover how to turn your Go programs into system-wide Linux CLI tools.

In this first lesson of our Golang series, we will cover the fundamental building blocks you need to start writing Go programs, and then we go further into more advanced concepts like interfaces and type assertions that you will use throughout this series.

Part 1: Go Fundamentals

Before we build real-world projects, you need to understand Go's core concepts, which are designed to be simple, and once you grasp these basics, you will feel confident building anything.

Variables and Data Types

In Go, you declare variables using the var keyword or the shorthand := operator. Go is statically typed, meaning every variable has a specific type determined at compile time.

Here is how you declare variables:

package main

import "fmt"

func main() {
    // Using var keyword
    var name string = "Alice"
    var age int = 30

    // Using shorthand (Go infers the type)
    city := "New York"
    isActive := true

    fmt.Println(name, age, city, isActive)
}

Go has several basic data types:

  • string - Text values like "Hello"
  • int - Whole numbers like 42
  • float64 - Decimal numbers like 3.14
  • bool - true or false values

The shorthand := is convenient and commonly used inside functions. It automatically infers the type based on the value you assign.

However, := can only be used when declaring a new variable. If you want to assign a new value to an existing variable, use = instead.

Example:

age := 30  // declaration
age = 31   // reassignment

Functions

Functions are reusable blocks of code that perform specific tasks. In Go, you define functions using the func keyword.

Here is a simple function: