GO Go

Go: Variables & Data Types

What you will learn

How to declare variables, how Go infers types, and the concept of zero values. Go is statically typed — the compiler checks types at compile time.

Declaring variables

var name string = "Alice"   // explicit: var name type = value
var age int                  // zero value (0), assigned later
height := 5.6                // short declaration — type inferred as float64
isStudent := true            // inferred bool

The := operator (short declaration) declares and assigns in one step. It infers the type from the value on the right. You can only use := inside functions.

Zero values

Go has no uninitialized variables. Every type has a zero value:

Type Zero Value
int, int32, int64 0
float64 0.0
string "" (empty string)
bool false
pointers, interfaces, slices, maps, channels nil
var s string
fmt.Println(s == "")  // true — s is empty string, not nil
var p *int
fmt.Println(p == nil) // true — pointer is nil

Multiple declarations

var x, y int = 1, 2      // multiple vars, same type
a, b := "hello", true   // mixed types with :=

Common mistakes

  • Using = instead of := for short declaration inside a function
  • Declaring a variable with := outside a function (use var at package level)
  • Forgetting Go is case-sensitive: name and Name are different variables

Quick check below!