Skip to content

Instantly share code, notes, and snippets.

@sugarme
Last active December 22, 2022 12:04
Show Gist options
  • Save sugarme/1447c0951c6837dfeb5686ccef13040d to your computer and use it in GitHub Desktop.
Save sugarme/1447c0951c6837dfeb5686ccef13040d to your computer and use it in GitHub Desktop.
Golang - struct embedding and overriding a method of parent
package main
import (
"fmt"
)
// This is a demonstration of embedding struct and overriding
// a method of parent struct.
// https://go.dev/play/p/nIdwUV8Qlaj
// Trait is a set of methods that a Base struct
// should implement with default value and a custom
// struct that embedded Base struct should implement
// with a custom value rather than default value inherited
// from Base struct.
type Trait interface {
Name() string
MyName() string
}
type Base struct {
trait Trait
}
func NewBase() *Base {
return new(Base)
}
// Implement Trait interface
// =========================
func (b *Base) Name() string {
panic("NotImplemented. It should be implemented by a struct that embeds Base struct.")
// Or can return a default value. E.g.
// return "Base"
}
func (b *Base) MyName() string {
if b.trait != nil {
return b.trait.Name()
}
return b.Name()
}
// Custom1 overrides Base.Name() method.
type Custom1 struct {
*Base
}
func NewCustom1() *Custom1 {
base := NewBase()
c := &Custom1{base}
c.trait = c
return c
}
// Overriding Name() method.
func (c *Custom1) Name() string {
return "Custom1"
}
// Custom2 does not override Base.Name() method.
type Custom2 struct {
*Base
}
func NewCustom2() *Custom2 {
base := NewBase()
c := &Custom2{base}
c.trait = c
return c
}
func main() {
c1 := NewCustom1()
fmt.Println(c1.MyName()) // "Custom1"
c2 := NewCustom2()
fmt.Println(c2.MyName()) // panic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment