Grouping related values

An order in a shop has an ID, a customer name and a total. You could keep them in three separate variables, but they belong together. A struct lets you make your own type that groups related values into one.

package main

import "fmt"

type Order struct {
	ID       int
	Customer string
	Total    int
}

func main() {
	order := Order{ID: 101, Customer: "Alice", Total: 80}
	fmt.Println(order.Customer, "pays", order.Total)

	order.Total = 95
	fmt.Println(order.Customer, "pays", order.Total)
}

Output:

Alice pays 80
Alice pays 95

type Order struct { ... } creates a new type called Order. The values inside it (ID, Customer and Total) are its fields, and each field has a name and a type. The type itself does not make an order yet. It only describes what an order looks like.

Order{ID: 101, Customer: "Alice", Total: 80} makes a real order. This is called a struct literal. You reach each field with a dot, like order.Customer, and a field works like a normal variable: you can read it, change it, or use it in a calculation.

Printing a whole struct

fmt.Println can print the whole struct at once. It prints the field values in braces. The %+v verb of Printf also prints the field names, which helps when you check your work:

package main

import "fmt"

type Order struct {
	ID       int
	Customer string
	Total    int
}

func main() {
	a := Order{ID: 101, Customer: "Alice", Total: 80}
	b := Order{ID: 7}
	fmt.Println(a)
	fmt.Printf("%+v\n", a)
	fmt.Printf("%+v\n", b)
}

Output:

{101 Alice 80}
{ID:101 Customer:Alice Total:80}
{ID:7 Customer: Total:0}

Order b only got an ID. A field you leave out gets its zero value, so Customer is the empty string and Total is 0.

If you write a field name that the struct does not have, Go stops before the program runs:

package main

import "fmt"

type Order struct {
	ID       int
	Customer string
	Total    int
}

func main() {
	order := Order{ID: 101, Name: "Alice"}
	fmt.Println(order)
}

Output:

# command-line-arguments
./main.go:12:26: unknown field Name in struct literal of type Order

Copies and slices of structs

When you assign a struct to another variable, Go copies every field. The two variables are then separate:

package main

import "fmt"

type Order struct {
	ID       int
	Customer string
	Total    int
}

func main() {
	a := Order{ID: 1, Customer: "Bob", Total: 30}
	b := a
	b.Total = 5
	fmt.Println(a.Total, b.Total)

	orders := []Order{
		a,
		{ID: 2, Customer: "Emma", Total: 45},
	}
	for _, o := range orders {
		fmt.Println(o.ID, o.Customer)
	}
}

Output:

30 5
1 Bob
2 Emma

Changing b.Total did not touch a.Total. Remember this copying. It is the reason the Pointers lesson exists.

A slice can hold structs like any other type. Inside []Order{ ... } you can leave out the type name Order before each literal, because Go already knows it.

The field names in this course start with a capital letter. Inside one program that is only a style choice, but it matters when other packages use your type, and the JSON lesson shows why.

In the exercise, define the Order struct with its three fields and build an order from the input.