How to Embed a Struct in Another Struct in Go
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
type <Outer> struct {
<Field> <type>
<Inner>
}
| Part | What 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 runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Structs
Create a file named 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)
}
TimestampsinsideNotehas 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
go run main.go
09:30
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
| Mistake | What happens | Do 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 archive | Pass note.Timestamps |
| Two embedded structs with the same field name | Reading that field directly fails with ambiguous selector, such as ambiguous selector c.X | Read it through the embedded struct's name |
| Setting promoted fields in a literal in code that older Go versions build | Go 1.26 and older don't accept it | Set them through the embedded struct |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ambiguous selector | Two embedded types share a field name | Name the embedded type in the selector |
cannot use … as … value | The outer struct was passed where the embedded type is expected | Pass 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
- Struct types, in the Go specification.
- Effective Go: embedding.
