当前位置: > > > > 接口方法引用相同的接口?
来源:stackoverflow
2024-04-23 13:15:30
0浏览
收藏
今天将给大家带来《接口方法引用相同的接口?》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
问题内容
我是一名java程序员,我现在正在学习go。 我尝试编写一个名为 vo 的自嵌套接口,以及一个名为 score 的 vo 实现, 代码如下:
type vo interface { merge(v vo) vo } type score int func (this score) merge(s score) score { return score(this + s) } func testthepkg(t *testing.t) { s := score(5) var v vo = s //compile error }
详细编译错误是:
cannot use s (variable of type Score) as Vo value in variable declaration: Score does not implement Vo (wrong type for method Merge) have Merge(s Score) Score want Merge(v Vo) VocompilerInvalidIfaceAssign
如何解决这个问题? 谢谢!
正确答案
func (this Score) Merge(s Vo) Vo { if typeAssersion, ok := s.(Score); ok { return Score(this + typeAssersion) } //log err or the `Vo` interface should return (Vo,error) return nil }
理论要掌握,实操不能落!以上关于《接口方法引用相同的接口?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注公众号吧!