Skip to main content

Go Ep 1: Toolchain, Syntax & Primitive Types

Rachmat Hidayat
Author
Rachmat Hidayat
Learn & sharing insights on TypeScript, Go, Kubernetes, DevOps, DevSecOps, SRE, Platform Engineering, AI/ML Engineering, and MLOps.
go - This article is part of a series.
Part 1: This Article
Designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson, Go (Golang) is an open-source programming language built for simplicity, concurrency, and high-performance systems engineering.

TL;DR (Quick Summary)
#

  • Go Philosophy: Simple, explicit, statically typed, garbage-collected, and blazingly fast compilation.
  • Go Modules (go mod init <name>): The standard dependency management system introduced in Go 1.11+.
  • Variable Declarations: Explicit (var x int = 10) vs Short Declaration Operator (x := 10).
  • Zero Values: Uninitialized variables automatically receive default zero values (0 for numbers, "" for strings, false for booleans, nil for pointers/slices/maps).

1. Installing Go & Setting Up Your Workspace
#

Let’s begin by installing the Go toolchain and initializing a workspace.

# Ubuntu / Debian Installation
sudo apt update
sudo apt install golang-go -y

# Verify Installation
go version

Expected Terminal Output:

go version go1.22.0 linux/amd64

Initializing a Go Module
#

Create a new directory and initialize a Go module:

mkdir go-masterclass && cd go-masterclass
go mod init github.com/username/go-masterclass

Expected Terminal Output:

go: creating new go.mod: module github.com/username/go-masterclass

2. Writing Your First Go Program (main.go)
#

In Go, executable programs must belong to package main and contain a main() entrypoint function.

Create main.go:

package main

import "fmt"

func main() {
 fmt.Println("Hello, Gophers! Welcome to Go.")
}

Run the program directly using go run:

go run main.go

Expected Terminal Output:

Hello, Gophers! Welcome to Go.

3. Variables, Constants & Type Inference
#

Go is strictly and statically typed, but provides short variable declaration syntax (:=) for local scope type inference.

package main

import "fmt"

// Package-level variables MUST use the 'var' keyword
var PackageName string = "Go Masterclass"

func main() {
 // 1. Explicit declaration with type
 var age int = 25

 // 2. Type inference (compiler infers string)
 var name = "Rachmat"

 // 3. Short variable declaration operator (Local scope only!)
 isEngineer := true

 // 4. Multiple variable declaration
 var (
  serverHost = "localhost"
  serverPort = 8080
 )

 // 5. Constants (Cannot be changed after compile time)
 const MaxConnections = 100

 fmt.Printf("User: %s (Age: %d, Engineer: %t)\n", name, age, isEngineer)
 fmt.Printf("Server: %s:%d (Max: %d)\n", serverHost, serverPort, MaxConnections)
}

4. Zero Values Table
#

In Go, there is no undefined. Uninitialized variables are automatically assigned their type’s Zero Value.

Go TypeDefault Zero Value
int, float64, byte0 / 0.0
string"" (Empty string)
boolfalse
Pointers, Slices, Maps, Channels, Interfacesnil
package main

import "fmt"

func main() {
 var count int
 var title string
 var active bool

 fmt.Printf("count: %d, title: '%s', active: %t\n", count, title, active)
 // Output: count: 0, title: '', active: false
}

5. Troubleshooting & Common Errors
#

Error 1: x declared and not used
#

The Cause: Go enforces strict compiler hygiene. If you declare a local variable and never read it, the compiler will refuse to build! The Fix: Remove the unused variable, or use the blank identifier _ to discard the value.

// 🔴 Compiler Error: unused declared and not used
unused := 100

// 🟢 Good: Discard value using blank identifier
_ = 100

Summary & Next Steps
#

In this episode:

  • We installed the Go toolchain and initialized a go.mod module.
  • We wrote and executed our first package main program.
  • We declared variables using var and := syntax.
  • We analyzed Go Zero Values and compiler unused variable errors.

In Episode 2: Control Flow & Loops, we will master if/else branching, switch pattern matching, and Go’s only loop keyword: for!

go - This article is part of a series.
Part 1: This Article