Lab 01: Hello World & Basics

Objective

Write your first Go programs: understand the package system, variables, constants, basic types, type inference, and Go's opinionated syntax.

Background

Go (Golang) was created at Google in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson. It combines the performance of C with the readability of Python, and ships with built-in concurrency, a fast compiler, and a comprehensive standard library.

Time

20 minutes

Prerequisites

  • Basic programming familiarity (any language)

Tools

  • Docker image: zchencow/innozverse-go:latest (Go 1.22)


Lab Instructions

Step 1: Hello, World

💡 Every Go file starts with package. The main package is special — it's the entry point for executables. import "fmt" pulls in the format package from the standard library. Unlike Python, unused imports are a compile error in Go — the compiler enforces clean code.

📸 Verified Output:


Step 2: Variables & Type Inference

💡 := is the short declaration operator — it declares AND assigns in one step, inferring the type. It only works inside functions. var works anywhere. Go variables always have a zero value (0, "", false, nil) — there's no concept of "uninitialized" memory.

📸 Verified Output:


Step 3: Constants & iota

💡 iota is a constant counter that resets to 0 in each const block and increments by 1 for each constant. Combined with bit shifts (<<), it elegantly defines powers of 2. This is how Go defines os.O_RDONLY, os.O_WRONLY, os.O_CREATE as bit flags.

📸 Verified Output:


Step 4: Basic Types & Conversions

💡 Go has no implicit type conversion — you must cast explicitly. This prevents subtle bugs where a float64 silently becomes an int. The _ (blank identifier) discards the error return from strconv.Atoi — in production code, always check errors.

📸 Verified Output:


Step 5: Control Flow

💡 Go's for is the only loop — no while or do/while. You get three forms: classic for init; cond; post, while-style for condition, and infinite for. switch in Go doesn't fall through by default (no break needed) and can match any expression, not just integers.

📸 Verified Output:


Step 6: Strings & fmt

💡 Go strings are UTF-8 encoded byte slices. len(s) returns bytes, not characters — "世界" is 6 bytes but 2 characters. Always use range over a string to iterate by Unicode code points (runes), not bytes. This avoids corrupting multi-byte characters.

📸 Verified Output:


Step 7: Functions Preview

💡 Multiple return values are one of Go's most important features. Functions return (result, error) by convention — forcing callers to explicitly handle errors. This is Go's error handling philosophy: errors are values, not exceptions. The _ blank identifier discards values you don't need.

📸 Verified Output:


Step 8: Capstone — Temperature Converter CLI

📸 Verified Output:


Summary

Concept
Go syntax

Variable

var x int or x := 42

Constant

const Pi = 3.14 or iota

Loop

for i := 0; i < n; i++

Condition

if x > 0 { ... }

Multiple returns

func f() (int, error)

String format

fmt.Printf("%d %s %v\n", ...)

Type method

func (t Type) Method() {}

Further Reading

Last updated