Skip to content

Instantly share code, notes, and snippets.

@meeech
Created June 18, 2022 04:09

Revisions

  1. meeech created this gist Jun 18, 2022.
    32 changes: 32 additions & 0 deletions field_promotion.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    package main

    import "fmt"

    type Original struct {
    foo string
    bar string
    }

    func (o *Original) Foo() string {
    return o.foo
    }

    func (o *Original) Bar() string {
    return o.bar
    }

    type Extended struct {
    Original
    }

    func (o *Extended) Bar() string {
    return "woot"
    }

    func main() {
    o := Original{foo: "foo", bar: "bar"}
    e := Extended{Original: o}

    fmt.Printf("%+v\n", o) // {foo:foo bar:bar}
    fmt.Printf("%+v\n", e.Bar()) // woot
    }