« Back to Index

Go: nested struct embedding

View original Gist on GitHub

Tags: #go

Nested struct embedding.go

package main

import (
	"fmt"
)

type Optional struct {
	WasSet bool
}

type OptionalString struct {
	Optional
	Value string
}

type OptionalFoo struct {
	OptionalString
}

func main() {
	var o OptionalFoo
	o.WasSet = true
	
	// The following two field lookups are identical
	fmt.Printf("%+v\n", o.WasSet)
	fmt.Printf("%+v\n", o.OptionalString.Optional.WasSet)
}