Created
May 25, 2021 13:15
-
-
Save Integralist/0058659d37c2bf0691cc7bde8c9579c3 to your computer and use it in GitHub Desktop.
[Go nested struct embedding] #go #golang #struct #embed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment