Go Data Types
Every value in Go has a type. The type decides how much memory the value needs, which operations are allowed on it, and how it is printed. Because Go is statically typed, the compiler knows every type before the program runs and rejects code that mixes types incorrectly.
The basic types
| Category | Types | Example values |
|---|---|---|
| Boolean | bool |
true, false |
| Signed integers | int, int8, int16, int32, int64 |
-7, 0, 2024 |
| Unsigned integers | uint, uint8, uint16, uint32, uint64, uintptr |
0, 255 |
| Floating point | float32, float64 |
3.14, -0.5, 1e6 |
| Complex | complex64, complex128 |
2 + 3i |
| Text | string |
"hello" |
| Aliases | byte (= uint8), rune (= int32) |
'A', '€' |
On top of these, Go has composite types that you will meet later: arrays, slices, maps, structs, pointers, functions, interfaces and channels.
package main
import "fmt"
func main() {
var active bool = true
var year int = 2025
var ratio float64 = 0.75
var title string = "Go Basics"
var letter rune = 'G'
var small byte = 200
fmt.Printf("%v is %T\n", active, active)
fmt.Printf("%v is %T\n", year, year)
fmt.Printf("%v is %T\n", ratio, ratio)
fmt.Printf("%v is %T\n", title, title)
fmt.Printf("%v is %T (%c)\n", letter, letter, letter)
fmt.Printf("%v is %T\n", small, small)
}true is bool
2025 is int
0.75 is float64
Go Basics is string
71 is int32 (G)
200 is uint8
Notice that a rune prints as a number with %v — it is just an int32 holding a Unicode code point. Use %c to see the character.
Default types of literals
When you use :=, Go picks a type from the literal:
| Literal | Inferred type |
|---|---|
42 |
int |
4.2 |
float64 |
"hi" |
string |
'h' |
rune |
true |
bool |
1 + 2i |
complex128 |
No automatic conversions
Go never converts between types for you — not even between int and float64, or between int and int64. Mixing them is a compile error:
package main
import "fmt"
func main() {
count := 3
price := 1.5
fmt.Println(count * price)
}# command-line-arguments
./example.go:8:14: invalid operation: count * price (mismatched types int and float64)
Explicit conversion
Convert a value by writing the target type like a function call: T(value).
package main
import "fmt"
func main() {
count := 3
price := 1.5
total := float64(count) * price
measured := 2.99
rounded := int(measured) // conversion truncates toward zero
var big int64 = int64(count) * 1_000_000
fmt.Println(total, rounded, big)
}4.5 2 3000000
Note: Converting a number to a string with
string(65)does not give"65"— it gives"A", the character with code 65. To turn numbers into text usestrconv.Itoaorfmt.Sprint; you will see this in the Strings chapter.
Defining your own types
The type keyword creates a new named type from an existing one. The new type is distinct: you cannot mix a Celsius with a plain float64 without converting, which helps catch bugs.
package main
import "fmt"
type Celsius float64
type UserID int
func main() {
var t Celsius = 21.5
var id UserID = 1001
fmt.Printf("%v %T\n", t, t)
fmt.Printf("%v %T\n", id, id)
fmt.Println(float64(t) + 0.5)
}21.5 main.Celsius
1001 main.UserID
22
Test yourself
What type does Go infer for x := 3.0?
float32float64intdouble
Which type is rune an alias for?
uint8stringint32int64
How do you multiply an int variable n by a float64 variable f?
n * fworks automaticallyfloat64(n) * f(float) n * fn.toFloat() * f