Variables, Constants & iota

beginner go syntax

A variable is just a named box that holds a value we can change later. A constant is the same idea, except its value is locked in forever once we set it.

Go gives us a few ways to declare variables, and one slick trick (iota) for making enums. Let’s walk through all of it.

Declaring variables with var

The full, explicit way uses the var keyword. We write var, then the name, then the type.

var name string = "Manish" // explicit type and value
var age int = 28           // type is int

// Go can infer the type from the value, so we can drop it:
var city = "Mumbai" // Go figures out this is a string

// If we don't give a value, Go uses the zero value (more on that later):
var count int // count is 0

Short declaration :=

Inside functions, there’s a shortcut. The := operator declares and assigns in one go, and Go infers the type.

In simple language, := means “make a new variable and set it to this.”

func main() {
	score := 100      // same as: var score int = 100
	name := "Go"      // string
	pi := 3.14        // float64

	score = 200 // we can reassign with plain = (no colon)
}

The only difference is that := only works inside functions. At the package level (outside any function) we must use var.

Constants

Use const for values that never change. The big rule: a constant’s value must be known at compile time — we can’t set it from a function call or user input.

const pi = 3.14159
const greeting = "hello"

// We can group them in a block for tidiness:
const (
	maxUsers   = 100
	appName    = "Gyaan"
	isReleased = true
)

Typed vs untyped constants

This is a subtle but interview-worthy detail.

An untyped constant has no fixed type until it’s used. This makes it flexible.

A typed constant locks itself to one type.

const untyped = 5     // untyped — can act as int, float64, etc.
const typed int = 5   // typed — strictly an int

var f float64 = untyped // OK — untyped 5 happily becomes a float64
// var g float64 = typed // ERROR — typed int won't auto-convert to float64

Think of an untyped constant like a chameleon — it takes on whatever type the situation needs.

iota — the enum trick

Go has no built-in enum keyword. Instead we use iota, which is a counter that auto-increments inside a const block.

In simple language, iota starts at 0 and goes up by 1 for each line in the block. It’s perfect for numbering related constants without typing 0, 1, 2 by hand.

type Weekday int

const (
	Sunday    Weekday = iota // 0
	Monday                   // 1 (iota keeps counting automatically)
	Tuesday                  // 2
	Wednesday                // 3
	Thursday                 // 4
	Friday                   // 5
	Saturday                 // 6
)

We only write iota once — every line below it keeps incrementing.

It even works with expressions. A classic example is defining byte sizes by shifting bits:

const (
	_  = iota             // skip 0 with the blank identifier _
	KB = 1 << (10 * iota) // 1 << 10 = 1024
	MB                    // 1 << 20
	GB                    // 1 << 30
)

Interview soundbite

Go has three declaration styles: explicit var x type = val, inferred var x = val, and the short form x := val which only works inside functions. Constants are compile-time fixed and can be typed or untyped — untyped ones flex to whatever type they’re used as. And since Go has no enum keyword, we use iota, an auto-incrementing counter inside a const block, to generate sequential values.