Go

Variables, Types & Control Flow

Go is statically typed, exactly like C — every variable has one fixed type, known at compile time. What's different is how you get there: Go can often infer a variable's type from its initial value, removing a lot of the type-name repetition C requires.

Quick reference
What it is
Statically-typed variable declarations (var or :=), Go's built-in types, and if/for/switch for control flow.
Mental model
Same rigid typing discipline as C, but the compiler is willing to figure out the type itself whenever it obviously can, so you don't have to write it twice.
Key numbers
Exactly one loop keyword (for) covers every shape C needs three keywords for (for, while, do-while).
Reach for it when
Any time you'd declare a variable or branch on a condition — this is Go's most-used syntax, used in literally every program.

Declaring variables: var and :=

var age int = 25        // explicit type, C-style order reversed (name THEN type)
var name string = "Ada"
var price float64         // no initializer — defaults to the type's "zero value" (0 here)

count := 10               // := infers the type from the value — count is an int, automatically
message := "Hello"        // message is a string, automatically

:= is the idiomatic, far more common form inside functions — you only reach for the longer var form when you need to declare a variable without immediately initializing it, or when you're declaring it outside any function.

Go's built-in types

Go typeClosest C equivalentNotes
intintPlatform's natural word size (32 or 64 bits) — like C, size isn't fixed by the spec
int32, int64int32_t, int64_tFixed-width, exactly like C's stdint.h types from Embedded C
float64doubleThe default floating-point type in Go — used far more often than float32
stringchar* / char arrayA real, built-in type — not a convention layered on char arrays like C strings
bool_Bool / intA genuine boolean type from the start — Go never had C's "0 is false" convention

Every variable declared without an explicit initial value gets a well-defined zero value (0 for numbers, "" for strings, false for booleans) — unlike a plain C local variable, which starts as uninitialized garbage.

if — no parentheses required

temperature := 85

if temperature > 90 {
    fmt.Println("Too hot")
} else if temperature < 60 {
    fmt.Println("Too cold")
} else {
    fmt.Println("Comfortable")
}

Functionally identical to C's if/else if/else — the only real difference is syntax: no parentheses around the condition, and the braces are mandatory even for a single statement (removing an entire category of C bug from C's Control Flow gotchas about a dangling if with no braces).

for — Go's only loop keyword

Go has exactly one loop keyword, shaped to cover every case C needs three separate keywords for:

// C-style three-part for (like C's for loop)
for i := 0; i < 5; i++ {
    fmt.Println(i)
}

// condition-only (like C's while loop)
count := 0
for count < 5 {
    fmt.Println(count)
    count++
}

// no condition at all (like C's for (;;) or while (1))
for {
    fmt.Println("forever, until a break")
    break
}

All three are the same keyword, just with different parts omitted — see C's Loops for the three separate forms Go collapses into this one.

switch — no fall-through by default

day := 3

switch day {
case 1:
    fmt.Println("Monday")
case 2:
    fmt.Println("Tuesday")
case 3:
    fmt.Println("Wednesday")
default:
    fmt.Println("Some other day")
}
// Wednesday — and execution stops here automatically, no break needed

This is the single biggest behavioral difference from C's switch: Go's cases do not fall through by default — each case implicitly breaks. Go still supports intentional fall-through, but only with an explicit fallthrough keyword, inverting C's "opt out of fall-through" into "opt in."

Real-world applications

Gotchas

Field notes