dubbo-go icon indicating copy to clipboard operation
dubbo-go copied to clipboard

how to implement the Java Overload method in dubbogo

Open shenchao861129 opened this issue 2 years ago • 5 comments

// java
public class CommentServiceImpl implements CommentService {
    public boolean isForbidsSpeak(Long bookId, Long userId);
    public boolean isForbidsSpeak(Long bookId, Long userId, String ip);
    ......
}
// go
type CommentServiceProvider struct{}

func (p *CommentServiceProvider) IsForbidsSpeak(ctx context.Context, params []interface{}) (interface{}, error) {
    l := len(params)
    if l == 2 {
        // TODO
    } else if l == 3 {
        // TODO
    } else {
        // TODO
    }
    return nil, nil
}

func (p *CommentServiceProvider) MethodMapper() map[string]string {
    return map[string]string{
        "IsForbidsSpeak": "isForbidsSpeak",
    }
}

shenchao861129 avatar Mar 07 '22 13:03 shenchao861129

更严谨点,就是判断下参数类型序列,但一般情况下没必要,毕竟 Go 的反射太费 cpu。

AlexStocks avatar Mar 07 '22 14:03 AlexStocks

nice!

cjphaha avatar Mar 07 '22 14:03 cjphaha

建议写个小工具做预处理和分发,减少代码复杂度

ChangedenCZD avatar Mar 08 '22 00:03 ChangedenCZD

It should be Overload, not Override.

search-cloud avatar Mar 29 '22 09:03 search-cloud

It should be Overload, not Override.

have changed the title. thx.

AlexStocks avatar Mar 29 '22 10:03 AlexStocks