Skip to content

Structs

A struct is a collection of fields. It is the main way to group data in Go. Structs are value types, but pointers to structs are common for efficiency.

Define a struct, create instances, and access fields.

package main
import "fmt"
type Person struct {
Name string
Age int
}
func main() {
p1 := Person{"Alice", 30}
p2 := Person{Name: "Bob", Age: 25}
var p3 Person
p3.Name = "Charlie"
p3.Age = 35
fmt.Println(p1, p2, p3)
// Pointer to struct
ptr := &p1
ptr.Age = 31 // no need to dereference
fmt.Println(p1)
}
Terminal window
{Alice 30} {Bob 25} {Charlie 35}
{Alice 31}