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. Themainpackage 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.varworks anywhere. Go variables always have a zero value (0,"",false,nil) — there's no concept of "uninitialized" memory.
📸 Verified Output:
Step 3: Constants & iota
💡
iotais a constant counter that resets to 0 in eachconstblock and increments by 1 for each constant. Combined with bit shifts (<<), it elegantly defines powers of 2. This is how Go definesos.O_RDONLY,os.O_WRONLY,os.O_CREATEas 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
float64silently becomes anint. The_(blank identifier) discards the error return fromstrconv.Atoi— in production code, always check errors.
📸 Verified Output:
Step 5: Control Flow
💡 Go's
foris the only loop — nowhileordo/while. You get three forms: classicfor init; cond; post, while-stylefor condition, and infinitefor.switchin Go doesn't fall through by default (nobreakneeded) 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 userangeover 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
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
