Fundamentals 9 min read

Mastering Go Structs: From Definition to Advanced Embedding

This comprehensive guide walks you through Go structs—covering their definition, instantiation, methods, receivers, anonymous fields, embedding, and method expressions—while providing clear code examples and practical guidelines for effective use in Go programming.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Go Structs: From Definition to Advanced Embedding

Struct Overview

Structs are one of the most important data structures in Go. Unlike many other languages, Go has no class concept; a struct serves a similar role.

This article is relatively long; please read patiently.

1. Understanding Structs

Before diving into syntax, here is a simple struct example to build overall intuition.

package main
import "fmt"

type Person struct {
    Name string
    Age  int8
}

func (p Person) GetName() {
    fmt.Printf("My name: %s
", p.Name)
}

func main() {
    p := Person{
        Name: "zhangsan",
        Age:  18,
    }
    p.GetName()
}

The code is straightforward; following the comments you can understand how to use it.

1.1 Definition

Structs are defined as:

type StructName struct {
    Field1 Type1
    Field2 Type2
    ...
}

1.2 Instantiation

There are several ways to create a struct instance:

var p = new(Person)
var p Person
var p = Person{}
p := Person{
    Name: "zhangsan",
    Age:  18,
}
p := Person{"zhangsan", 18}

After instantiation you can access fields with obj.FieldName, e.g., p.Name.

1.3 Methods

Struct methods correspond to instance methods in object‑oriented languages.

func (p Person) GetName() {
    fmt.Printf("My name: %s
", p.Name)
}

Methods have a receiver, whereas functions do not.

1.4 Receivers

Receivers can be value types or pointer types. Example:

package main
import "fmt"

type Person struct {
    Name string
    Age  int8
}

func (p Person) GetName() {
    fmt.Printf("My name: %s
", p.Name)
}
func (p *Person) GetAge() {
    fmt.Printf("My age: %d
", p.Age)
}

func main() {
    p1 := Person{Name: "张三", Age: 18}
    p2 := &Person{Name: "李四", Age: 16}
    p1.GetName()
    p1.GetAge()
    fmt.Println("---------分割线-------")
    p2.GetName()
    p2.GetAge()
}

Go automatically converts between value and pointer receivers when calling, so the call syntax is identical.

1.5 Pointer vs. Value Receivers

Guidelines for choosing receiver type:

Within the same struct, keep receiver types consistent—either all pointers or all values.

Value receiver: suitable for small structs where copying cost is low and you do not need to modify internal fields.

Pointer receiver: preferred for large structs or when you need to modify the struct's fields.

2. Anonymous Fields and Embedding

Anonymous fields are one of the most useful features of Go structs and are widely used.

2.1 Anonymous Fields

When a field name is omitted, the type name becomes the field name.

package main
import "fmt"

type Data struct {
    uint8
}

func main() {
    d := Data{8}
    fmt.Println(d.uint8)
}

2.2 Struct Embedding

Embedding allows reuse of fields and methods from another struct.

type Person struct {
    Name string
    Age  int8
}

type Student struct {
    ID    int
    Score float32
    Person // anonymous field
}

Student inherits Person's fields and methods.

package main
import "fmt"

type Person struct {
    Name string
    Age  int8
}
func (p Person) GetName() {
    fmt.Printf("My name: %s
", p.Name)
}

type Student struct {
    ID    int
    Score float32
    Person
}
func (s Student) GetScore() {
    fmt.Printf("My score: %v
", s.Score)
}
func main() {
    s := Student{
        ID: 1,
        Score: 98,
        Person: Person{Name: "zhangsan", Age: 18},
    }
    fmt.Printf("My age: %d
", s.Age)
    fmt.Printf("My age p.Person.age: %d
", s.Person.Age)
    s.GetScore()
    s.GetName()
    s.Person.GetName()
}

Embedding rules:

Fields of the embedded (anonymous) struct are directly accessible from the outer struct.

Methods of the embedded struct are directly callable from the outer struct.

Go first looks for a field or method in the current struct; if not found, it searches the anonymous embedded structs.

3. Method Values and Method Expressions

Method values can be assigned to variables, while method expressions require the receiver to be passed explicitly as a parameter.

package main
import "fmt"

type Person struct {
    Name string
    Age  int8
}
func (p Person) GetName() {
    fmt.Printf("My name: %s
", p.Name)
}
func main() {
    p := Person{Name: "zhangsan", Age: 18}
    getName := p.GetName // method value
    getName()
    fmt.Println("--------分割线-------")
    pGetName := Person.GetName // method expression
    pGetName(p)
}
Method expressions need the receiver passed as a parameter.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendGoEmbeddingstructmethodsanonymous-fields
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.