Skip to main content

How to Embed a Struct in Another Struct in Go

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Go has no inheritance, but a struct can include another struct by naming only its type. The fields of the embedded struct are then "promoted": you can read them straight from the outer struct, as if it had declared them itself.

Syntax​

Syntax
type <Outer> struct {
<Field> <type>
<Inner>
}
PartWhat it does
<Inner>An embedded field: a type with no field name.
outer.<InnerField>Reads a promoted field directly.
outer.<Inner>.<InnerField>Reads the same field through the embedded struct's own name.

The Go specification: "A field declared with a type but no explicit field name is called an embedded field." And: "Promoted fields act like ordinary fields of a struct."

The Example​

The example embeds a Timestamps struct in a Note, then reads Created directly from the note and Updated through the embedded struct.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Structs​

Create a file named main.go:

main.go, which declares a Timestamps struct with Created and Updated, a Note struct with Text and an embedded Timestamps, creates a note and prints note.Created and note.Timestamps.Updated.

main.go
package main

import "fmt"

type Timestamps struct {
Created string
Updated string
}

type Note struct {
Text string
Timestamps
}

func main() {
note := Note{Text: "Buy milk", Timestamps: Timestamps{Created: "09:30", Updated: "09:45"}}
fmt.Println(note.Created)
fmt.Println(note.Timestamps.Updated)
}
  • Timestamps inside Note has no field name, so it's embedded.
  • In the literal, the embedded struct is set through its type name, Timestamps: Timestamps{...}.

Step 2: Run It​

Terminal
go run main.go
Output
09:30
09:45

A zsh terminal where go run main.go prints 09:30 and 09:45.

note.Created worked even though Note declares no Created field: it was promoted from Timestamps. The command is the same in zsh, bash and PowerShell.

Setting Promoted Fields in a Literal (Go 1.27)​

Since Go 1.27, a literal can set a promoted field directly. The Go 1.27 release notes: "A key in a struct literal may now be any valid field selector for the struct type, not just a (top-level) field name of the struct." With Go 1.27.4, Note{Text: "Buy milk", Created: "09:30"} compiled, and both note.Created and note.Timestamps.Created printed 09:30. Code that must also build with older Go versions should keep the Timestamps: Timestamps{...} form.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting embedding to make Note a Timestamps, as in archive(note) for func archive(t Timestamps)cannot use note (variable of struct type Note) as Timestamps value in argument to archivePass note.Timestamps
Two embedded structs with the same field nameReading that field directly fails with ambiguous selector, such as ambiguous selector c.XRead it through the embedded struct's name
Setting promoted fields in a literal in code that older Go versions buildGo 1.26 and older don't accept itSet them through the embedded struct

Troubleshooting​

SymptomLikely causeFix
ambiguous selectorTwo embedded types share a field nameName the embedded type in the selector
cannot use … as … valueThe outer struct was passed where the embedded type is expectedPass the embedded field itself

FAQ​

Are methods promoted too?​

Yes. Methods of the embedded type can be called on the outer struct, the same way as its fields.

Is embedding the same as inheritance?​

No. It's composition: the outer struct contains the inner one, and Go forwards field and method access to it.

Conclusion​

Embed a struct by naming its type without a field name, and its fields become available directly on the outer struct. Set it in literals through its type name, and fall back to outer.Inner.Field when names clash.

Additional Resources​