100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Variables and Data Types in Go

Learn how Go declares variables with var and :=, and the built-in types like int, float64, bool, and string.

BasicsBeginner9 min readJul 8, 2026
Analogies

Introduction

Go is a statically typed language, meaning every variable has a fixed type known at compile time. Variables can be declared explicitly with the var keyword, or implicitly using the short declaration operator := inside functions, where Go infers the type from the assigned value. Go also gives every declared-but-unassigned variable a sensible zero value instead of leaving it undefined.

🏏

Cricket analogy: A player's role, batsman or bowler, is fixed on the team sheet before the toss just as a Go variable's type is fixed at compile time, while shorthand notes like 'VK' inferred to mean Kohli mirror the := operator inferring type from context.

Syntax

go
// var declaration with explicit type
var age int = 25

// var declaration with inferred type
var name = "Gopher"

// short declaration (function scope only)
count := 10

// multiple variables at once
var x, y int = 1, 2
a, b := "hello", true

Explanation

The var form is required at package level (outside any function), since := only works inside function bodies. Go's basic data types include bool, string, the signed integers int, int8, int16, int32, int64, the unsigned integers uint, uint8, uint16, uint32, uint64, the floating point types float32 and float64, and the complex types complex64 and complex128. byte is an alias for uint8 and rune is an alias for int32, used to represent a single Unicode code point. Any variable declared without an initial value automatically receives its type's zero value: 0 for numeric types, false for bool, "" for string, and nil for pointers, slices, maps, channels, functions, and interfaces.

🏏

Cricket analogy: Team sheet entries filed before the tournament (var, at package level) versus quick notes scribbled during a match (:= inside functions) parallel Go's rule, while a spare batsman with no assigned position defaults to zero runs, like a zero value.

Example

go
package main

import "fmt"

func main() {
	var isActive bool
	var temperature float64 = 36.6
	var initial rune = 'G'
	message := "Learning Go"

	fmt.Println(isActive, temperature, initial, message)
	fmt.Printf("Type of temperature: %T\n", temperature)
}

Output

go
false 36.6 71 Learning Go
Type of temperature: float64

Key Takeaways

  • Use var for package-level declarations; use := only inside functions.
  • Go infers types automatically when an initial value is provided.
  • Unassigned variables get a zero value: 0, "", false, or nil.
  • byte is an alias for uint8; rune is an alias for int32 (a Unicode code point).
  • Every variable has one fixed, static type for its entire lifetime.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#VariablesAndDataTypesInGo#Variables#Data#Types#Syntax#StudyNotes#SkillVeris