Variables, Types & Zero Values
Variables in TypeScript vs Go
Section titled “Variables in TypeScript vs Go”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.
Variable declaration
Section titled “Variable declaration”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.
// TypeScriptlet count: number = 0;let name = "Alice"; // inferred as stringconst MAX = 100;// Govar count int = 0name := "Alice" // inferred as stringconst MAX = 100Type inference with :=
Section titled “Type inference with :=”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.
let score = 42; // numberlet ratio = 3.14; // numberlet label = "hello"; // stringlet active = true; // booleanscore := 42 // intratio := 3.14 // float64label := "hello" // stringactive := true // boolConstants
Section titled “Constants”Go constants work like TypeScript const — immutable, evaluated at compile time. Go also has iota for auto-incrementing enum-like sequences (covered later).
const PI = 3.14159;const APP_NAME = "MyApp";const PI = 3.14159const AppName = "MyApp"Try it
Section titled “Try it”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)}Loading Go runtime (first run only, ~8 MB)…