Clarification: I'm just learning GO, and came across this problem.
I'm trying to implement a "class" that inherit a method that invokes a "virtual" method that should be implemented by the child class. Here's my code:
package main
import (
"fmt"
"sync"
)
type Parent struct {
sync.Mutex
MyInterface
}
func (p *Parent) Foo() {
p.Lock()
defer p.Unlock()
p.Bar()
}
func (p *Parent) B(){
panic("NOT IMPLEMENTED")
}
func (p *Parent) A() {
p.Lock()
defer p.Unlock()
p.B()
}
type MyInterface interface {
Foo()
Bar()
}
type Child struct {
Parent
Name string
}
func (c *Child) Bar(){
fmt.Println(c.Name)
}
func (c *Child) B(){
fmt.Println(c.Name)
}
func main() {
c := new(Child)
c.Name = "Child"
// c.A() panic
c.Foo() // pointer error
}
I left out some code regarding the sync.Mutex that does some async update to the values of Child.
So apparently in A() or Foo() the pointer p have type Parent. How should I change my code so that A/Foo refer to B/Bar defined in the Child class?