Data Types in Go
Data types define the kind of data a variable can store. Go is a statically typed language, meaning every variable has a fixed type that is checked at compile time.
Understanding data types is essential for writing correct, efficient, and safe Go programs.
Why Data Types Matter
Data types help Go:
- Allocate the correct amount of memory
- Prevent invalid operations
- Improve program performance
- Catch errors before execution
Go will not allow mixing incompatible types without explicit conversion.
Categories of Data Types in Go
Go data types can be broadly grouped into:
- Numeric types
- Boolean type
- String type
- Derived types (arrays, slices, maps, structs)
In this lesson, we focus on the core built-in data types.
Integer Types
Integers store whole numbers (no decimals). Go provides different integer sizes for efficiency and precision.
intint8,int16,int32,int64uint,uint8,uint16,uint32,uint64
Example using real numeric values:
var users int = 150
var population int64 = 7800000000
Use:
intfor general-purpose countingint64for very large numbers
Floating-Point Types
Floating-point numbers store decimal values. Go supports two floating types:
float32float64(default and recommended)
Example:
price := 29.99
interestRate := 7.5
Go infers both variables as float64.
Boolean Type
The bool type represents logical values.
It can store only:
truefalse
Example using a real-world condition:
isLoggedIn := true
hasSubscription := false
Boolean values are heavily used in decision-making and control flow.
String Type
Strings store text data and are enclosed in double quotes. Strings in Go are immutable (cannot be changed once created).
name := "Dataplexa"
city := "San Jose"
Strings are commonly used for:
- User names
- Messages
- File paths
- URLs
Using Multiple Data Types Together
A real program often uses multiple data types together.
package main
import "fmt"
func main() {
username := "Alex"
age := 28
balance := 1540.75
active := true
fmt.Println("User:", username)
fmt.Println("Age:", age)
fmt.Println("Balance:", balance)
fmt.Println("Active:", active)
}
Each variable has a different data type, and Go manages them safely.
Type Safety in Go
Go does not allow mixing types without explicit conversion.
This will cause an error:
// invalid
total := 10 + 2.5
Correct version:
total := float64(10) + 2.5
This strict behavior prevents hidden bugs.
Zero Values for Data Types
If a variable is declared without initialization, Go assigns a zero value:
int→ 0float64→ 0.0bool→ falsestring→ ""
Example:
var count int
var status bool
fmt.Println(count, status)
Real-World Example
In an e-commerce system:
productName→ stringprice→ float64stock→ intavailable→ bool
Choosing the correct data type ensures accuracy and performance.
Practice Exercise
Task
Create variables for:
- Total students (int)
- Average score (float)
- Passed exam (bool)
- Course name (string)
Print all values using fmt.Println().
What You Will Learn Next
In the next lesson, you will learn about constants and iota, which are used for fixed values and enumerations in Go programs.