Skip to content

Variables, Types & Zero Values

In TypeScript you have let for mutable variables, const for immutable bindings, and type inference via :. You’re also comfortable with values starting as undefined before assignment.

Go has similar mechanisms but with cleaner rules and one concept TypeScript has no direct answer for: zero values.

Go offers two styles. The short declaration := (only inside functions) infers the type from the right-hand side — just like let x = in TypeScript. The long form var is used at package level or when you need to declare without an immediate value.

TypeScript
// TypeScript
let count: number = 0;
let name = "Alice"; // inferred as string
const MAX = 100;
Go
// Go
var count int = 0
name := "Alice" // inferred as string
const MAX = 100

The := operator is Go’s workhorse for local variables. It declares AND assigns in one step — no let, no type annotation needed when the type is obvious.

TypeScript
let score = 42; // number
let ratio = 3.14; // number
let label = "hello"; // string
let active = true; // boolean
Go
score := 42 // int
ratio := 3.14 // float64
label := "hello" // string
active := true // bool

Go constants work like TypeScript const — immutable, evaluated at compile time. Go also has iota for auto-incrementing enum-like sequences (covered later).

TypeScript
const PI = 3.14159;
const APP_NAME = "MyApp";
Go
const PI = 3.14159
const AppName = "MyApp"
package main
import "fmt"
func main() {
// Short declaration
name := "Gopher"
age := 3
// Long form (package-level style)
var score int = 100
const greeting = "Hello"
fmt.Printf("%s, %s! Age: %d, Score: %d\n", greeting, name, age, score)
}
What operator is used for short variable declaration inside a function in Go?
What is the zero value of a string in Go?
Which Go declaration style works at package level (outside any function)?
In Go, const values are evaluated at: