WeCodeAcademy

Go Variables

Declare with var, or use the short form := inside functions, which works out the type for you. Constants use const.

ExampleGo
package main

import "fmt"

func main() {
  var name string = "Ada"
  var age int = 36
  height := 1.68        // short declaration: float64
  student := false
  const Year = 2026

  fmt.Println(name, age, height, student, Year)
  age = 37
  fmt.Printf("%s is %d (%T)\n", name, age, height)

  var count int   // unset variables get a zero value
  var text string
  fmt.Printf("%d %q\n", count, text)
}
Output
Ada 36 1.68 false 2026
Ada is 37 (float64)
0 ""

In Go, every unused variable is a compile error, which keeps programs tidy. Variables you do not set get a zero value: 0, "", false.

Quick check

What does := do?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.