函数接受抽象接口和函数接受struct实现之间的golang转换

An issue I met when creating a layer on top of different version of implementations. The goal is to abstract out the implementation details and the caller doesn't need to care which implementation we are using.

Please see the code example here

^ The code can better explain the issue I met.

We have two version of Stream implementation Stream1 and Stream2. They have a common interface Stream.

Both of them have a corresponding BindStreamHandler function accepting StreamHandler1 or StreamHandler2.

We have a function BindStreamHandler, and a general implementation of func StreamHandlerImpl(s Stream). No matter we use Stream1 or Stream2, the general implementation is the same.

Now I face an issue downcasting StreamHandlerImpl (accepting abstract Stream to StreamHandler1 (accepting Stream1).


Update:

I found this version worked.

func BindHandler(h interface{}) {
    if Version == 1 {
        h1 := h.(StreamHandler1)
        BindStreamHandler1(h1)
    } else {
        h2 := h.(StreamHandler2)
        BindStreamHandler2(h2)
    }
}

But the signature of BindHandler becomes too weak :( I prefer using signature func BindHandler(h StreamHandler)

It seems that you are trying to design your classes using class hierarchies (like Java) which is really not how Go tackles OO. I really suggest you design your code around interfaces rather than trying to mimic inheritance. Since we cannot speculate on why you have such types, below is a minimal code snippet which will make BindHandler maintain a stricter signature.

func BindHandler(h StreamHandler) {
    if Version == 1 {
        BindStreamHandler1(func(s Stream1) {
            h(s)
        })
    } else {
        BindStreamHandler2(func(s Stream2) {
            h(s)
        })
    }
}

Playground