Types & Data
The Go type landscape
Section titled “The Go type landscape”Every value in Go has a static type known at compile time. Go divides its types into two broad families:
Value types — variables hold the data directly. Assigning one variable to another copies the data. The primitive value types are booleans, numeric types (int, int64, float64, uint8, …), and string. Composite value types are arrays and structs.
Reference-like types — the variable holds a small header that points to a backing data structure allocated elsewhere. Slices, maps, channels, functions, and pointers all fall into this family. Assigning copies the header, not the underlying data, so two variables can observe the same data.
flowchart TD
Root["Go Type Taxonomy"]
Root --> Value["Value types (copy on assign)"]
Root --> Ref["Reference-like (header copied)"]
Value --> V1["bool, int, float64, string"]
Value --> V2["[N]T (array)"]
Value --> V3["struct { ... }"]
Ref --> R1["*T (pointer)"]
Ref --> R2["[]T (slice)"]
Ref --> R3["map[K]V"]
Ref --> R4["chan T, func(...)"] Module map
Section titled “Module map”| Lesson | Topic |
|---|---|
| This page | Type landscape and a first program |
| Structs | Defining structs, embedding, comparison |
| Slices | Arrays vs slices, make, append, copy |
| Maps | make(map[K]V), comma-ok, delete, iteration |
| Pointers | & and *, mutation, nil pointers |
Your first composite-type program
Section titled “Your first composite-type program”The program below defines a Point struct, builds a []Point slice, then looks up a name in a map[string]int. It demonstrates value types (Point is copied), slice literals, and map access with the zero-value default.
package main
import "fmt"
type Point struct { X, Y int}
func main() { // Struct literal with named fields p := Point{X: 3, Y: 4} fmt.Println(p.X, p.Y)
// Slice of structs points := []Point{ {X: 0, Y: 0}, {X: 1, Y: 2}, {X: 3, Y: 4}, } for _, pt := range points { fmt.Printf("(%d, %d)\n", pt.X, pt.Y) }
// Map: string -> int score := map[string]int{ "Alice": 95, "Bob": 87, } fmt.Println(score["Alice"]) fmt.Println(score["Charlie"]) // missing key -> zero value (0)}Loading Go runtime (first run only, ~8 MB)…