When would you use String Immutability in a real project
Fast recall
Quick revision
Core answer
Practical understanding of String Immutability.
Repeating a definition without saying when to use it.
Give one trade-off and say how you would test it.
Interview-ready guide
Interview answer
The zero value is "" — an empty string, fully usable:
var s string // ""
len(s) // 0
s + "more" // "more"
s == "" // true
fmt.Sprintf("<%s>", s) // "<>"There is no nil string. This differs from the other header types: slice zero is nil, map zero is nil, pointer zero is nil — but string's zero is a valid empty string.
The mechanics: a nil-able type is a POINTER to a structure that can be absent. A string is (pointer, length) where pointer-nil + length-0 IS the empty string — the representation of "" is exactly (nil, 0), a coherent value.
Practical consequence: strings never need nil-check ceremony. Emptiness is s == "" or len(s) == 0.
The JSON boundary case: an empty string marshals to "" never null; to express 'absent', *string (pointer to string) is the type — its nil marshals as null. When an API needs to distinguish 'absent' from 'empty', that's the tool.
When would you use String Immutability in a real project
Direct Answer
"" (the empty string) — NOT nil. A declared-but-unset string is immediately usable: len 0, concatenation works, comparisons work. No nil state exists for the string type.
Deep Dive & Architecture
Why no nil — the mechanics: a nil-able type is a POINTER to a structure that can be absent. A map's nil has no hash table; a slice's nil has no backing array. A string is (pointer, length) where pointer-nil plus length-zeronil, 0), a coherent value rather than an absence.
The design consequence vs other header types: slices and maps need nil-check ceremony (a nil map write panics; a nil slice can't be indexed) — strings need none. Every operation works on "": len, comparison, slicing (""[0:0]), range (zero iterations).
The empty-vs-nil boundary cases: (1) JSON — a string marshals as "" never null; to express 'absent' in JSON, *string is the type — APIs that need the distinction document it; (2) SQL nullable columns — database drivers use sql.NullString (a struct {String string; Valid bool}) — the standard library's own admission that the string type can't express null and the workaround is a wrapper. Knowing NullString exists signals production experience with nullable text columns.
The deeper consequence: the zero-value design means every API taking a string never needs the nil ceremony — a signature (s string) is complete as written.
Common Mistakes & Pitfalls
Repeating a definition without saying when to use it.
Check your understanding
Answer these without looking back at the article.
- What is String Immutability, and when would you use it?
- How would you compare String Immutability with an alternative?
- What is a common mistake when using String Immutability?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.