Conversions
Go conversions are explicit and rule-bound; they are not free-form casts that erase type semantics.
Canonical guidance
- Go requires explicit conversions in many places where other languages coerce implicitly
- conversions can change representation, precision, or interpretation
- interface assertions and unsafe pointer tricks are different mechanisms entirely
Use when
- fixing type mismatches
- reviewing numeric narrowing
- reasoning about string and slice conversion behavior
Avoid
- calling every type change a cast and assuming C-like behavior
- assuming conversions preserve information automatically
- using
unsafewhen an ordinary conversion exists
Preferred pattern
timeout := time.Duration(ms) * time.Millisecond
Anti-pattern
- converting between
[]byteandstringwhile assuming no allocation or aliasing cost by default
Explanation: This is tempting because the syntax is short, but ordinary conversions follow language rules, not zero-copy performance wishes.
Why
- conversion mistakes show up in APIs, numerics, text handling, and interface work across the whole language