Structs
A struct groups related fields under one named type — Go has no classes, so a struct plus its associated functions (covered in the next lesson) is how you model a "thing" with data.
main.go
Try it yourself →package main
import "fmt"
type Person struct {
Name string
Age int
}
func main() {
alice := Person{Name: "Alice", Age: 30}
fmt.Println(alice.Name, "is", alice.Age, "years old")
}Output
Alice is 30 years old
Structs are values
Assigning or passing a struct copies it by default — rect.Width = 12 only changes the local rect variable in main, not some shared instance elsewhere. Printing a struct directly with Println shows all its field values in order.
main.go
Try it yourself →package main
import "fmt"
type Rectangle struct {
Width, Height float64
}
func main() {
rect := Rectangle{Width: 10, Height: 5}
rect.Width = 12
fmt.Println(rect)
}Output
{12 5}Next: methods — attaching behavior to a struct with receivers.
Practice exercise
Define a struct Book with fields Title string and Pages int. Create a book with Title "Dune" and Pages 412, then print book.Title and book.Pages using fmt.Println(book.Title, book.Pages).