Skip to content
AgileLogics
6 min read

Variables & Data Types

Prerequisites: How Go code runs

Syntax

var age int = 36        // full form: name, type, value
var city = "Lagos"      // type inferred from the value
var count int           // no value: gets the zero value
name := "Ada"           // short declaration (inside functions only)

The basic types you'll use most:

  • int — whole numbers (64-bit on modern machines). Sized variants such as int8, int32, int64, and uint also exist.
  • float64 — decimal numbers (float32 exists but is rarely needed).
  • bool — true or false.
  • string — text, always UTF-8.

Every variable declared without a value gets its type's zero value: 0 for numbers, false for bool, and "" (the empty string) for string. Go has no "uninitialized" variables.

Example

package main

import "fmt"

func main() {
    var age int = 36
    var price float64 = 19.99
    name := "Ada"
    isActive := true

    var count int
    var label string
    var ready bool

    fmt.Println(age, price, name, isActive)
    fmt.Println(count, label == "", ready)

    age = 37 // reassign with =, not :=
    x, y := 1, 2
    fmt.Println(age, x, y)
}

Expected output

36 19.99 Ada true
0 true false
37 1 2

Common mistakes

  • Declaring a variable and never using it. In Go this is a compile error, not a warning:
./main.go:6:5: declared and not used: x
  • Using := outside a function. At package level you must use var.
  • Using := to reassign an existing variable in the same scope — := declares; = assigns. (:= is allowed when at least one variable on the left is new.)
  • Mixing types in an expression: int and float64 never mix without an explicit conversion (next lesson but one).

Practice

Declare a string for your city, an int for its population in millions, and a bool called isCapital, then print all three on one line using short declarations.

Show solution
package main

import "fmt"

func main() {
    city := "Lagos"
    population := 15
    isCapital := false

    fmt.Println(city, population, isCapital)
}