Structs
Defining a struct
Section titled “Defining a struct”A struct groups related fields under a single named type. Each field has a name and a type:
type Point struct { X, Y int}
type User struct { Name string Email string Age int}Fields whose names begin with an uppercase letter are exported (visible outside the package). Lowercase names are unexported.
Struct literals
Section titled “Struct literals”You can create a struct value in two ways:
// Positional — order must match the field declaration orderp := Point{3, 4}
// Named fields — order does not matter; unset fields get zero valuesNamed-field form is strongly preferred: it is self-documenting and resilient to field reordering.
Field access
Section titled “Field access”Use a dot to read or write any field:
fmt.Println(u.Name) // readu.Age = 30 // writeAnonymous structs
Section titled “Anonymous structs”For one-off shapes — configuration, test fixtures, local grouping — you can define a struct type inline without naming it:
cfg := struct { Host string Port int}{Host: "localhost", Port: 8080}Struct comparison
Section titled “Struct comparison”Two struct values are comparable with == if all their fields are comparable types. The comparison is field-by-field:
a := Point{1, 2}b := Point{1, 2}fmt.Println(a == b) // trueStructs containing slices, maps, or functions are not comparable with ==.
Struct embedding (composition)
Section titled “Struct embedding (composition)”Go has no class inheritance. Instead, you embed one struct type inside another to promote its fields and methods:
type Address struct { City string Country string}
type Person struct { Name string Age int Address // embedded — City and Country are promoted}After embedding, person.City is shorthand for person.Address.City. This is composition, not inheritance — the embedded type does not know it is embedded, and the outer type is not a subtype of the inner type.
package main
import "fmt"
type Address struct { City string Country string}
type Person struct { Name string Age int Address // embedded struct}
func main() { // Named-field literal p1 := Person{ Name: "Alice", Age: 30, Address: Address{City: "Bangkok", Country: "Thailand"}, } fmt.Println(p1.Name, p1.Age) fmt.Println(p1.City) // promoted from Address fmt.Println(p1.Country) // promoted from Address
// Struct copy: p2 is an independent value p2 := p1 p2.Name = "Bob" fmt.Println(p1.Name, p2.Name) // Alice Bob
// Anonymous struct cfg := struct { Host string Port int }{Host: "localhost", Port: 8080} fmt.Printf("%s:%d\n", cfg.Host, cfg.Port)
// Struct comparison (all fields comparable) a1 := Address{City: "Bangkok", Country: "Thailand"} a2 := Address{City: "Bangkok", Country: "Thailand"} fmt.Println(a1 == a2) // true}Loading Go runtime (first run only, ~8 MB)…