Basics & Packages
Go was designed as a small language that reads immediately: a handful of keywords, one way to declare a variable up front and a short := for locals, fixed-width basic types, and the go tool that builds, runs, and tests without a separate build system. Because of that simplicity the basics often slip by — "a package is a package, a variable is a variable."
Under the simple syntax sits the model interviews actually probe: a variable with no initializer gets not garbage but a strictly defined zero value; := in an inner scope can shadow an outer variable; a name is exported exactly when it starts with a capital letter; iota counts lines, not values. This topic walks the start of a program layer by layer — from packages and declarations to name visibility and the go commands.
Topic map
- Packages & imports — a program is assembled from packages;
package mainandfunc mainare the entry point, and an import pulls in other packages by path. - Declarations:
var,const,:=—vardeclares a variable (with or without a type),constan immutable constant, and:=is short declaration with type inference inside a function. - Basic types — fixed-width integers and floats,
bool,string; type conversion in Go is always explicit. - Zero values — a variable with no initializer gets its type's zero value (
0,false,"",nil), useful by default. - iota and constants — the
iotacounter increments per line in aconstblock (counting_) and resets in a new block. - Names & visibility — a capital first letter makes a name exported (visible from other packages); lowercase keeps it package-private.
- Build & run —
go run,go build,go mod, andgofmt: how to build, run, and format a program.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
| Assuming an uninitialised variable holds garbage like in C | Go always zeroes it to the type's zero value |
Thinking a zero-value string is nil | It is the empty ""; s == nil does not even compile |
Writing to a nil map without make | A read yields zero, a write panics assignment to entry in nil map |
Using := in a nested block without noticing it shadows the outer variable | The outer variable is unchanged — a common source of "lost" assignments and err bugs |
Expecting iota to skip the value on a _ line | A _ line consumes its iota, shifting every later constant |
| Naming an exported function with a lowercase letter | The name stays package-private and is invisible from other packages |
Relying on implicit int to float64 conversion | Go requires an explicit float64(x) — otherwise a compile error |
Why it matters for interviews
The basics are the junior-section filter: the question is never the syntax but the model under it. A candidate who learned "how to write it" but not "how it works" stumbles right here.
What interviewers usually check:
- How
var,const, and:=differ and where each applies. - What zero value a variable with no initializer gets, and why a
nilmap cannot be written. - What
iotais and how enums and bit flags are built from it. - How Go decides whether a name is exported (by the case of the first letter).
- What
go run,go build, andgo moddo.