Created
May 13, 2023 10:43
-
-
Save supercaracal/2fb779037d0458cfa6ebc1b1e25749c7 to your computer and use it in GitHub Desktop.
How to call child methods from parent methods
This file contains hidden or 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 parent struct { | |
b func() string | |
} | |
func (p *parent) a() string { | |
if p.b == nil { | |
return "not implemented yet: #b" | |
} | |
return p.b() | |
} | |
type child struct { | |
parent | |
} | |
func (c *child) b() string { | |
return "child" | |
} | |
func main() { | |
c := &child{} | |
c.parent.b = c.b | |
fmt.Println(c.a()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment