By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A Go variable is never uninitialized. Declare it and it holds the zero value for its type: 0 for numbers, false for booleans, "" for strings, nil for pointers, slices, maps, functions, channels and interfaces. This removes a whole class of bugs from other languages — there is no undefined, no missing-property crash, no constructor that must run before a field is safe to read.
It also means you can design types whose zero value is already useful. Relay's Severity starts at debug, an aggregate starts at zero counts, and a strings.Builder starts empty and ready. When a type cannot work at its zero value, document the constructor that must be called; when it can, prefer the zero value.
:= declares and infers; = assigns and must match. Go never converts between numeric types implicitly, so a float64 division needs an explicit conversion on at least one side. That strictness looks pedantic until you have debugged an integer division that silently truncated a percentage.
package main
import "fmt"
type Severity int
const (
SeverityDebug Severity = iota
SeverityInfo
SeverityWarn
SeverityError
)
func (s Severity) String() string {
switch s {
case SeverityDebug:
return "debug"
case SeverityInfo:
return "info"
case SeverityWarn:
return "warn"
case SeverityError:
return "error"
default:
return "unknown"
}
}
const maxBatchSize = 3
func main() {
var count int
var message string
var ratio float64
var enabled bool
fmt.Println(count, message == "", ratio, enabled)
level := SeverityWarn
fmt.Println(level)
fmt.Printf("%T %T %T\n", count, message, ratio)
total := 7
average := float64(total) / 2
fmt.Printf("%.1f\n", average)
var small int8 = 127
wide := int(small)
fmt.Println(wide)
fmt.Println(maxBatchSize, SeverityError)
}A constant is evaluated at compile time, so it can carry arbitrary precision and costs nothing at run time. iota numbers a block of constants from zero; the pattern is so common that Severity above reads like an enum, even though Go has no enum keyword. The named type is what makes it useful: you get a compiler error if someone passes a raw int where a Severity is expected, and you can attach a String method for humans.
| Concept | Example | What to remember |
|---|---|---|
| Zero value | var n int | Every variable starts safe: 0, false, "", nil |
| Inference | level := SeverityWarn | := infers once; later assignment must match |
| Conversion | float64(total) / 2 | No implicit numeric conversion, ever |
| Constant | const maxBatchSize = 3 | Compile-time value, no run-time cost |
| iota | SeverityInfo = 1 | Restarts per const block; pair it with String |
Key takeaways
Every Go type has a zero value, so declared variables are always usable.
:= infers a type; conversions between numeric types are always explicit.
Named types such as Severity add safety and can carry a String method.
Use const and iota for values known at compile time.
Unused variables and imports are compile errors — the compiler is part of the review.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.