ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Go (Golang) Tutorial• 6 min read

Variables, Types & Zero Values

The := short declaration operator, typed constants, and default zero values

In Go, uninitialized variables are never undefined or random garbage memory; they are automatically assigned safe default "Zero Values".

Variable Declaration & Zero Values

var x int = 10: Explicit declaration.

x := 10: Short variable declaration (available inside function bodies only).

Zero Values: Numbers default to 0, booleans to false, strings to "" (empty string), and pointers/slices/maps to nil.

Inspecting Zero Values in Go
package main

import "fmt"

func main() {
    var count int
    var balance float64
    var isActive bool
    var username string

    // Uninitialized variables have safe zero values!
    fmt.Printf("int: %d | float: %f | bool: %t | string: '%s'\n", 
        count, balance, isActive, username)
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

What is the default "Zero Value" of an uninitialized string variable in Go?