Go Data Types
Data types tell Go what kind of value a variable holds. Go checks types at compile time — before the program runs. This prevents many bugs that other languages only catch after the program crashes.
Categories of Data Types
Go Data Types
├── Basic Types
│ ├── Numeric
│ │ ├── Integer (int, int8, int32, int64)
│ │ ├── Unsigned (uint, uint8, uint32, uint64)
│ │ └── Float (float32, float64)
│ ├── Boolean (bool)
│ └── String (string)
├── Composite Types
│ ├── Array
│ ├── Slice
│ ├── Map
│ └── Struct
└── Special Types
├── Pointer
└── Interface
This topic covers the basic types. Composite and special types each have their own dedicated topics.
Integer Types
Integers hold whole numbers — no decimal point. Go offers several integer sizes depending on how large the number needs to be.
| Type | Size | Range | Use Case |
|---|---|---|---|
int | 32 or 64 bit | Depends on system | General purpose integer |
int8 | 8 bit | -128 to 127 | Small numbers |
int16 | 16 bit | -32768 to 32767 | Medium small numbers |
int32 | 32 bit | -2 billion to 2 billion | Larger numbers |
int64 | 64 bit | Very large range | Very large numbers |
uint | 32 or 64 bit | 0 and above only | Non-negative integers |
package main
import "fmt"
func main() {
var age int = 25
var score int32 = 980000
var population int64 = 8000000000
fmt.Println(age) // 25
fmt.Println(score) // 980000
fmt.Println(population) // 8000000000
}
Float Types
Float types hold numbers with decimal points.
| Type | Size | Precision | Use Case |
|---|---|---|---|
float32 | 32 bit | ~6–7 decimal digits | Simple decimal values |
float64 | 64 bit | ~15–16 decimal digits | Precise calculations |
package main
import "fmt"
func main() {
var price float32 = 9.99
var distance float64 = 12345.6789012345
fmt.Println(price) // 9.99
fmt.Println(distance) // 12345.6789012345
}
float64 is the default and most commonly used float type in Go. Use float32 only when memory is a concern.
Boolean Type
A boolean holds only two values: true or false. Booleans are used in conditions and decisions.
package main
import "fmt"
func main() {
var isActive bool = true
var isPaid bool = false
fmt.Println(isActive) // true
fmt.Println(isPaid) // false
}
String Type
A string holds a sequence of characters — letters, numbers, symbols, or spaces. Strings always use double quotes in Go.
package main
import "fmt"
func main() {
var name string = "Go Programming"
var site string = "eStudy247"
fmt.Println(name) // Go Programming
fmt.Println(site) // eStudy247
}
String Operations
package main
import (
"fmt"
"strings"
)
func main() {
course := "go programming"
fmt.Println(strings.ToUpper(course)) // GO PROGRAMMING
fmt.Println(strings.Contains(course, "go")) // true
fmt.Println(len(course)) // 14
}
The byte and rune Types
| Type | Alias For | Represents |
|---|---|---|
byte | uint8 | A single ASCII character (0–255) |
rune | int32 | A Unicode character (any language) |
package main
import "fmt"
func main() {
var letter byte = 'A'
var emoji rune = '😀'
fmt.Println(letter) // 65 (ASCII value of A)
fmt.Println(emoji) // 128512 (Unicode code point)
}
Default Zero Values
In Go, every variable has a default zero value if no value is assigned. This prevents the "undefined variable" errors common in other languages.
| Type | Zero Value |
|---|---|
int | 0 |
float64 | 0.0 |
bool | false |
string | "" (empty string) |
Key Points
- Go checks data types at compile time — mismatched types cause errors before running
- Use
intfor most whole numbers andfloat64for decimal numbers - Boolean values are only
trueorfalse - Strings use double quotes; characters use single quotes (byte/rune)
- Every type has a default zero value — Go never leaves a variable undefined
