0Pricing
Go Academy · Lesson

Struct Embedding

Compose structs.

Struct Embedding is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Compose Structs

Go has no class inheritance. Instead it offers embedding: place one struct inside another without a field name to reuse its fields and methods.

Anonymous Fields

An embedded field is declared by type only, no name. Its members are promoted to the outer struct.

package main

import "fmt"

type Engine struct{ Power int }

type Car struct {
	Engine
	Brand string
}

func main() {
	c := Car{Engine: Engine{Power: 150}, Brand: "Coddy"}
	fmt.Println(c.Power, c.Brand)
}

Accessing Promoted Fields

You read c.Power directly even though Power belongs to the embedded Engine. That is field promotion.

The Full Path Still Works

You can always reach the embedded struct explicitly via its type name.

package main

import "fmt"

type Engine struct{ Power int }
type Car struct{ Engine }

func main() {
	c := Car{Engine{200}}
	fmt.Println(c.Engine.Power)
	fmt.Println(c.Power)
}

Embedding vs a Named Field

A named field (engine Engine) requires c.engine.Power. Embedding (Engine) promotes members so you write c.Power. Embedding expresses an is-a-kind-of relationship.

Initializing Embedded Structs

Use the type name as the field key in a composite literal.

package main

import "fmt"

type Address struct{ City string }
type User struct {
	Address
	Name string
}

func main() {
	u := User{Address: Address{City: "Istanbul"}, Name: "Mert"}
	fmt.Println(u.Name, "in", u.City)
}

Multiple Embeddings

A struct can embed several types, mixing their fields together.

package main

import "fmt"

type Timestamps struct{ Created string }
type Author struct{ Name string }
type Post struct {
	Timestamps
	Author
	Title string
}

func main() {
	p := Post{Timestamps{"today"}, Author{"Ada"}, "Hello"}
	fmt.Println(p.Title, p.Name, p.Created)
}

Name Collisions

If two embedded types share a field name, the ambiguous reference is a compile error — you must qualify it with the type name to disambiguate.

Shadowing by the Outer Struct

A field declared on the outer struct shadows a promoted one of the same name. The outer field wins for unqualified access.

package main

import "fmt"

type Base struct{ ID int }
type Item struct {
	Base
	ID int
}

func main() {
	i := Item{Base{1}, 99}
	fmt.Println(i.ID, i.Base.ID)
}

Pointer Embedding

You can embed a pointer type. The members are still promoted, but you must initialize the pointer or risk a nil dereference.

package main

import "fmt"

type Logger struct{ Prefix string }
type Service struct{ *Logger }

func main() {
	s := Service{Logger: &Logger{Prefix: "[svc]"}}
	fmt.Println(s.Prefix)
}

When to Embed

  • Reuse common fields (timestamps, IDs)
  • Build richer types from smaller ones
  • Avoid deep nesting and verbose access paths

Quick Check

Test your struct embedding knowledge.

Recap

You learned struct embedding.

  • Anonymous fields promote members to the outer struct
  • Access via c.Power or c.Engine.Power
  • Outer fields shadow promoted ones
  • Collisions require explicit qualification
  • Pointer embedding works too

Frequently asked questions

Is the “Struct Embedding” lesson free?

Yes — the full text of “Struct Embedding” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Struct Embedding”?

Compose structs. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Struct Embedding” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Struct Embedding
  2. Interface Embedding
  3. Method Promotion
  4. Composition over Inheritance
← Back to Go Academy