When would you use Type Conversion Vs Assertion in a real project
Fast recall
Quick revision
Core answer
Practical understanding of Type Conversion Vs Assertion.
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 lossy conversions, each with its mechanics:
int(3.9) // 3 — truncation toward zero, no rounding
int(-3.9) // -3 — toward zero (not floor)
int8(300) // 44 — 300 overflows int8; high bits dropped
byte('é') // 233? No — é is U+00E9 = 233 — fits. byte('€') = 28 — 8364 mod 256
float32(0.1) // 0.10000000149011612 — precision roundingThe float→int rule: TRUNCATION (toward zero), not rounding — 3.9 becomes 3, -3.9 becomes -3. And a float too large for the int: implementation-specified garbage (undefined in practice) — never convert an unbounded float to int without bounding first.
The integer→smaller-integer rule: wraparound (mod 2^target-bits) — 300 into int8 is 300-256 = 44. Silent. Genuinely dangerous when it's a size/count computed in a wider type flowing into a protocol field.
The precision case: float64→float32 rounds to 24-bit mantissa — no bits 'lost' visibly, but the value CHANGES (0.1's nearest float32 ≠ its nearest float64).
What Go doesn't do: warn, error, or trap on any of these. Conversion is the programmer's assertion 'I know this fits / the loss is intended.' The discipline: bound-check before narrowing (if v > math.MaxInt8 { return error }) when the input isn't provably in range — the alternative is a silent wraparound in production.
When would you use Type Conversion Vs Assertion in a real project
Direct Answer
float64→int truncates the fraction (3.9 → 3). Larger int → smaller int drops high bits. rune → byte drops the code point to its low 8 bits. []byte→string copies bytes (loss of the mutable view). Go doesn't warn — the conversion is always silent.
Deep Dive & Architecture
The truncation-vs-undefined boundary in the spec: int(f) where f is integral and in-range is defined (truncation); where f is out-of-range or non-integral — the Go spec says the behavior is implementation-specific (on common platforms: an unspecified value, not a panic). This is one of Go's genuinely sharp edges — most conversions are total (defined for all inputs), and float→int at the extremes is the documented exception. Code review treats any float→int without a range guard on an unbounded input as a defect for exactly this reason.
The wraparound mechanics, precisely: int8(300) compiles to a truncation of the two's-complement representation to 8 bits (300 = 0b1_0010_1100 → 0b0010_1100 = 44) — mod-256 arithmetic, the same wraparound that integer overflow in C-family unsigned arithmetic exhibits, except Go applies it at CONVERSION time (explicit) rather than on arithmetic (Go's arithmetic wraps too — the constant-overflow rules differ: constant expressions that overflow are COMPILE errors, while variable conversions wrap silently).
The string conversions' 'loss': []byte(s) and []rune(s) copy — the MUTABILITY is lost, never the content. string(b) with invalid-UTF-8 bytes yields a string holding those bytes — no data lost, but the string is now invalid UTF-8 (a downstream rune-decode yields U+FFFD at the bad positions — the data was preserved; the INTERPRETATION degrades). That distinction — bytes preserved, meaning degraded — is the conversion story's final chapter: Go never converts bytes away; it lets you carry exactly what you had, encoding warts included, and the utf8 package is where you check what you're carrying.
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 Type Conversion Vs Assertion, and when would you use it?
- What is a common mistake when using Type Conversion Vs Assertion?
- How would you compare Type Conversion Vs Assertion with an alternative?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.