Thuta Learning
BasicProgrammingbeginner

Variables

Relax. We'll talk through this in plain words — no textbook voice.

You can think of a variable as a little labeled box that holds data. In Go, var lets you declare it with a type, and inside a function you can also write it quickly with :=.

go
package main

import "fmt"

func main() {
    var language string = "Go"
    version := 1.22
    isFast := true

    fmt.Println(language)
    fmt.Println(version)
    fmt.Println(isFast)
}

var language string = "Go" spells out the type explicitly. version := 1.22 lets the compiler infer the type, and this form only works inside a function.

You should see
Go 1.22 true

Info

Use a variable if the value might change later. For values that shouldn't change — like an app name or a timeout constant — const is the better choice.

Easy traps

  • You can't use := at the package level, outside a function. Use var or const out there instead.
Variables | Thuta Learning