In NodeJS, I can declare a callback in one place and use it in one place to avoid breaking the structure of the project.
A.js
module.exports = class A(){
constructor(name, callback){
this.name = name;
this.callback = callback;
}
doSomeThingWithName(name){
this.name = name;
if(this.callback){
this.callback();
}
}
}
B.js
const A = require(./A);
newA = new A("KimKim", ()=> console.log("Say Oyeah!"));
In Go, I also want to do the same thing like this with interface and implement.
A.go
type canDoSomething interface {
DoSomething()
}
type AStruct struct {
name string
callback canDoSomething
}
func (a *AStruct) DoSomeThingWithName(name string){
a.name = name;
a.callback.DoSomething()
}
B.go
import (A);
newA = A{}
newA.DoSomeThingWithName("KimKim");
Can I overwrite logic for interface functions in file B.go? How could I do to make them equivalent to NodeJS's style?
I try
import (A);
newA = A{}
// I want
//newA.callback.DoSomething = func(){}...
// or
// func (a *AStruct) DoSomething(){}...
// :/
newA.DoSomeThingWithName("KimKim");