Essential Go Object oriented programming  Edit on GitHub      File Issue

Embedded structs

because a struct is also a data type, it can be used as an anonymous field, the outer struct can directly access the fields of the embedded struct even if the struct came from a different package. this behaviour provides a way to derive some or all of your implementation from another type or a set of types.

package main

type Admin struct {
    Username, Password string

type User struct {
    ID uint64
    FullName, Email string
    Admin // embedded struct

func main() {
    admin := Admin{

    user := User{
        "Zelalem Mekonen",

    fmt.Println(admin) // {zola supersecretpassword}

    fmt.Println(user) // {1 Zelalem Mekonen {zola supersecretpassword}}

    fmt.Println(user.Username) // zola

    fmt.Println(user.Password) // supersecretpassword
  ↑ ↓ to navigate     ↵ to select     Esc to close