-1

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");
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Kim Ericko
  • 21
  • 1
  • 4
  • 1
    Your Go code is invalid, and won't even compile. But more directly related to your question: Please explain your end goal. What exactly are you trying to accomplish? You probably can't do it the same way you would in Node, but you can probably do it somehow. Focus on the problem you're trying to solve, not your imagined solution. – Jonathan Hall Nov 27 '18 at 10:32
  • Thank for commenting. I want to overwrite func DoSomething of canDoSomething that AStruct implements in file b.go. I know my code cann't compile and run but i think everyone can understand what i want to do – Kim Ericko Nov 27 '18 at 10:47
  • How does a callback relate to wanting to overwrite a method? – Jonathan Hall Nov 27 '18 at 11:32
  • The term "overwrite" looks like a translation artifact to me. There is no overwriting going on in the NodeJS code either. It looks like the intended meaning is "assign". – Peter Nov 27 '18 at 11:36

2 Answers2

1

Functions are first class values in Go, just like they are in JavaScript. You don't need an interface here (unless there is some other goal that you are not stating):

type A struct {
    name string
    callback func()
}

func (a *A) DoSomeThingWithName(name string){
    a.name = name;
    a.callback()
}

func main() {
    a := &A{
        callback: func() { /* ... */ },
    }

    a.DoSomeThingWithName("KimKim")
}

Since all types can have methods, all types (including function types) can implement interfaces. So if you really want to you can let A depend on an interface and define a function type for providing implementations on-the-fly:

type Doer interface {
    Do()
}

// DoerFunc is a function type that turns any func() into a Doer.
type DoerFunc func()

// Do implements Doer
func (f DoerFunc) Do() { f() }

type A struct {
    name     string
    callback Doer
}

func (a *A) DoSomeThingWithName(name string) {
    a.name = name
    a.callback.Do()
}

func main() {
    a := &A{
        callback: DoerFunc(func() { /* ... */ }),
    }

    a.DoSomeThingWithName("KimKim")
}
Peter
  • 29,454
  • 5
  • 48
  • 60
0

Can I overwrite logic for interface functions in file B.go?

No, interfaces in Go (and other languages) do not have any logic or implementation.

To implement an interface in Go, we just need to implement all the methods in the interface.

How you may do A and B types implement same interface with different logic:

type Doer interface {
    Do(string)
}

type A struct {
    name string
}
func (a *A) Do(name string){
    a.name = name;
    // do one thing
}

type B struct {
    name string
}
func (b *B) Do(name string){
    b.name = name;
    // do another thing
}
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143