Go Data Types

Go is a statically typed language, so every variable has a specific type such as int, float64, string, or bool that is fixed at compile time.

Why Types Matter in Go

Unlike dynamically typed languages, Go requires the compiler to know the exact type of every value before the program runs. This catches many bugs early, makes code easier to reason about, and lets the compiler generate faster machine code. Types are usually inferred with the := operator, but you can also declare them explicitly with var.

Numeric Types

Go has several integer types (int, int8, int16, int32, int64 and their unsigned counterparts uint8, uint16, uint32, uint64) plus two floating-point types (float32 and float64). In everyday code you will mostly reach for int for whole numbers and float64 for decimals, since these are the platform's natural word size and the default type Go picks for numeric literals.

Declaring Numeric Variables

package main

import "fmt"

func main() {
	var age int = 30
	var price float64 = 19.99
	temperature := -5

	fmt.Println("Age:", age)
	fmt.Println("Price:", price)
	fmt.Println("Temperature:", temperature)
}

Strings and Runes

A string in Go is an immutable sequence of bytes, typically holding UTF-8 encoded text. You create strings with double quotes, concatenate them with +, and measure their byte length with len(). Because strings are UTF-8, iterating with range gives you runes (Unicode code points) rather than raw bytes, which matters when working with non-ASCII text.

Working with Strings

package main

import (
	"fmt"
	"strings"
)

func main() {
	firstName := "Ada"
	lastName := "Lovelace"
	fullName := firstName + " " + lastName

	fmt.Println(fullName)
	fmt.Println("Length:", len(fullName))
	fmt.Println("Upper:", strings.ToUpper(fullName))
}

Booleans

The bool type holds only two values, true or false. Booleans are the result of comparison operators (==, <, >=) and logical operators (&&, ||, !), and they drive every if statement and for loop condition in Go. Unlike some languages, Go does not allow integers to be used as booleans — the types are strictly separate.

Boolean Logic

package main

import "fmt"

func main() {
	isActive := true
	hasPermission := false

	canEdit := isActive && !hasPermission
	fmt.Println("Can edit:", canEdit)

	score := 85
	passed := score >= 60
	fmt.Println("Passed:", passed)
}
  • int — whole numbers, size depends on platform (32 or 64 bit)
  • float64 — double-precision decimal numbers, the default for float literals
  • string — immutable UTF-8 text
  • bool — true or false, used in conditions and logical expressions
Note: Every variable in Go has a zero value if it is declared without an initial value: 0 for numeric types, "" for strings, and false for booleans. This means Go variables are never left in an undefined state.
TypeExample ValueZero ValueTypical Use
int420Counting, indexing, whole-number math
float643.140Measurements, prices, scientific calculations
string"hello"""Text, names, messages
booltruefalseConditions, flags, comparisons
Note: Watch out for integer division: 7 / 2 evaluates to 3, not 3.5, because both operands are int. Convert at least one operand to float64 first if you need a decimal result.

Exercise: Go Data Types

What values can a Go bool variable actually hold?