0PricingLogin
Go Academy · Lesson

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

  1. Defining and Using Structs
  2. Methods on Structs
  3. Constructor Patterns
  4. Anonymous Structs and Composition
← Back to Go Academy