tezvyn:

How does Go's variable declaration and mutability differ from Rust?

AI-drafted, machine-checkedSource: go.devbeginner
WHAT IT TESTS

Mutability defaults and syntax.

ANSWER OUTLINE

Contrast Rust let (immutable) and let mut (mutable) with Go var and := (mutable), noting Go uses const for immutability.

RED FLAG

Claiming Go variables are immutable or that := behaves like const.

WHAT THIS TESTS: This question checks whether you understand how two modern systems languages handle mutability defaults and what syntax they expose. Interviewers want to see that you know Rust enforces immutability by default as a memory-safety mechanism, while Go prioritizes brevity and programmer convenience by making variables mutable unless explicitly declared as constants. They also want to hear the exact keywords and operators: var, :=, and const in Go versus let and let mut in Rust.

A GOOD ANSWER COVERS: Four things in order. First, state that Rust binds variables with let and makes them immutable by default; you must write let mut to allow reassignment. Second, explain that Go takes the opposite approach: variables declared with var or the short declaration operator := are mutable by default and can be reassigned without extra syntax. Third, note that Go uses const for values that cannot change, and const declarations require compile-time determinable values and cannot use the := operator. Fourth, briefly mention the philosophical difference: Rust forces you to opt into mutability to prevent accidental side effects and data races, while Go treats mutability as the common case and keeps syntax minimal.

COMMON WRONG ANSWERS: Claiming that Go variables are immutable by default. Confusing the short declaration operator := with a constant declaration. Asserting that Go has no immutability mechanism at all. Stating that var in Go makes variables immutable while := makes them mutable. Mixing up the syntax by saying Go uses let or that Rust uses var.

LIKELY FOLLOW-UPS: How does Go's const interact with composite types like slices or maps? Why does Rust require mut to be explicit at the binding site rather than the usage site? When would you prefer var over := inside a function in Go? How does Rust's ownership model interact with mutability in a way that Go's garbage collector does not? Can you mutate a slice or map in Go even if the variable holding it is not reassigned, and how does that compare to Rust's borrow checker?

ONE CONCRETE EXAMPLE: In Rust, let x = 5 creates an immutable binding; attempting x = 10 causes a compile error unless you originally wrote let mut x = 5. In Go, x := 5 or var x = 5 creates a variable that you can freely reassign with x = 10 later. If you want true immutability in Go, you must write const x = 5, which prevents reassignment entirely and requires the value to be known at compile time.

Read the original → go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.