Defining and Using Structs
Struct literals, field access, and embedding
What Is a Struct?
A struct groups related fields of different types under one named type. It's Go's primary mechanism for defining custom data structures — similar to a class but without inheritance.
Struct Declaration
Define a struct type with the type keyword:
package main
import "fmt"
type Person struct {
Name string
Age int
City string
}
func main() {
p := Person{Name: "Alice", Age: 30, City: "London"}
fmt.Println(p)
}All lessons in this course
- Defining and Using Structs
- Methods on Structs
- Constructor Patterns
- Anonymous Structs and Composition