By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A slice of ints can hold Penny's amounts, but not what they mean. Each expense has an identifier, a description, an amount and a category: four values that belong together. A struct lets you name that shape once and then create values of it.
The type is the plan; each value created from it is a filled-in form. Because every expense has the same fields, a function that takes an Expense can rely on all four being present.
You declare a struct with type Name struct { ... } and list each field with a name and a type. The field names start with a capital letter, which makes them exported; that matters in later sections when JSON and tests read them from outside the package.
A declaration creates a new type but no data. var lunch Expense gives you a value with every field at its zero value: 0 for ints and an empty string for strings. You can then assign each field with a dot and the field name.
package main
import "fmt"
type Expense struct {
ID int `json:"id"`
Description string `json:"description"`
Cents int `json:"cents"`
Category string `json:"category"`
}
func main() {
var lunch Expense
lunch.ID = 1
lunch.Description = "Lunch with a friend"
lunch.Cents = 1250
lunch.Category = "food"
fmt.Println(lunch.ID, lunch.Description)
fmt.Println(lunch.Cents, lunch.Category)
}A composite literal fills in a struct value in one expression. There are two forms. The keyed form lists Field: value pairs and can appear in any order. The positional form lists values in declaration order with no field names.
Prefer the keyed form. It survives someone adding a field later, and every value in it is labelled, so a reader never has to count slots.
package main
import "fmt"
type Expense struct {
ID int `json:"id"`
Description string `json:"description"`
Cents int `json:"cents"`
Category string `json:"category"`
}
func main() {
lunch := Expense{
ID: 1,
Description: "Lunch with a friend",
Cents: 1250,
Category: "food",
}
coffee := Expense{2, "Flat white", 450, "food"}
fmt.Println(lunch.Description, lunch.Cents)
fmt.Println(coffee.Description, coffee.Cents)
}Structs whose fields are all comparable can be compared with ==, which checks that every field is equal. Two expenses with the same four values are equal; any different value makes them unequal. That is useful in tests and when checking whether a value changed.
Assignment copies a struct. copied := lunch creates a second value with the same field values. Changing copied.Cents cannot change lunch.Cents. Section 6 shows why that copying matters when methods need to edit the original.
package main
import "fmt"
type Expense struct {
ID int `json:"id"`
Description string `json:"description"`
Cents int `json:"cents"`
Category string `json:"category"`
}
func main() {
lunch := Expense{1, "Lunch", 1250, "food"}
secondLunch := Expense{1, "Lunch", 1250, "food"}
coffee := Expense{2, "Flat white", 450, "food"}
fmt.Println("same value:", lunch == secondLunch)
fmt.Println("same as coffee:", lunch == coffee)
copied := lunch
copied.Cents = 900
fmt.Println("lunch cents:", lunch.Cents)
fmt.Println("copied cents:", copied.Cents)
}Structs and slices combine naturally: a slice of Expense is Penny's list of expenses. A function can take []Expense and total the Cents fields, and a range loop can print each description with its formatted amount.
package main
import "fmt"
type Expense struct {
ID int `json:"id"`
Description string `json:"description"`
Cents int `json:"cents"`
Category string `json:"category"`
}
func formatCents(cents int) string {
return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}
func total(expenses []Expense) int {
sum := 0
for _, expense := range expenses {
sum += expense.Cents
}
return sum
}
func main() {
expenses := []Expense{
{ID: 1, Description: "Lunch", Cents: 1250, Category: "food"},
{ID: 2, Description: "Bus pass", Cents: 4200, Category: "travel"},
{ID: 3, Description: "Coffee", Cents: 450, Category: "food"},
}
fmt.Println("count:", len(expenses))
fmt.Println("total:", formatCents(total(expenses)))
for _, expense := range expenses {
fmt.Printf("%d %-12s %s\n", expense.ID, expense.Description, formatCents(expense.Cents))
}
}Read the output line by line. len counts expenses, total adds cents, and formatCents turns the total into dollars. The %-12s verb left-aligns the description in 12 characters so the amounts line up; that is the fmt alignment from step 8 doing real work.
This exact struct is the Penny data model. Later sections build on it: files in section 7, JSON in step 34, tests in section 8 and the HTTP API in section 9. Keeping the field names and types stable is what lets those steps reuse earlier work.
| Field | Type | JSON tag | What it holds |
|---|---|---|---|
ID | int | id | A number that identifies the expense. |
Description | string | description | What was bought. |
Cents | int | cents | The amount in integer cents. Never a float. |
Category | string | category | A bucket such as food or travel. |
The tags after each type are strings that the encoding/json package reads later. They say which JSON field name to use. They do not change how the struct works now, and nothing in this section uses them.
Money is always integer cents. Floating point cannot represent every cent value exactly, and rounding errors in a money app are unacceptable. formatCents exists so a cents value becomes $12.50 only at the edge, when something is shown to a person.
Build an expense for something you bought today using the keyed form: pick an ID, a description, a cents amount and a category. Print its description and formatCents(expense.Cents). Then create a second Expense with the same field values and check first == second. Finally, put both into a slice, write func total(expenses []Expense) int, and print the formatted total. Expected for expenses of 450 and 1250 cents: $17.00.
Key takeaways
A struct type names a fixed set of fields; each value of that type carries one value per field.
Keyed literals such as Expense{ID: 1, Cents: 1250} are safer than positional ones.
Use value.Field to read or assign a field; structs compare with == when every field is comparable.
Assigning a struct copies it, so changing the copy leaves the original alone.
Penny's Expense stores money as integer cents and formats only for display.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.