当前位置: > > > > 如何使用其中一个方法返回同一接口的对象的接口?
如何使用其中一个方法返回同一接口的对象的接口?
来源:stackoverflow
2024-04-22 09:18:34
0浏览
收藏
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天米云就整理分享《如何使用其中一个方法返回同一接口的对象的接口?》,聊聊,希望可以帮助到正在努力赚钱的你。
问题内容
我仍然对 go 中的接口很感兴趣,但我正在编写一个包,它将获取结构图并将它们作为树打印到 stdout。为此,我定义了一个名为 treenode 的 interface,其中包含一个方法 getchildren,该方法需要返回一个实现 treenode 的结构切片。这样我就可以从根开始递归树。即,这是完整的接口定义:
type treenode interface {
getname() string
getchildren() []treenode
}
我听说“接受接口并返回结构”是最佳实践。这很有意义。但是,当我尝试在 struct 上实现这些方法时,我开始遇到问题。这是一个完整的示例:
package main
import "fmt"
type mystruct struct {
name string
children []*mystruct
}
type treenode interface {
getname() string
getchildren() []treenode
}
func printtree(root treenode) {
// print the nodes recursively
fmt.println(root.getname())
// etc.
}
func main() {
child1 := &mystruct{
name: "child 1",
}
child2 := &mystruct{
name: "child 2",
}
root := &mystruct{
name: "root",
children: []*mystruct{child1, child2},
}
printtree(root)
}
func (my_struct *mystruct) getname() string {
return my_struct.name
}
func (my_struct *mystruct) getchildren() []*mystruct {
return my_struct.children
}
在 main 中对 printtree 的调用中,编译器抱怨
cannot use root (type *mystruct) as type treenode in argument to printtree:
*mystruct does not implement treenode (wrong type for getchildren method)
have getchildren() []*mystruct
want getchildren() []treenode
这有点令人惊讶,因为接口表示 getchildren() 应该返回实现 treenode 的对象,而 *mystruct 确实实现 treenode。但显然,关于编译器如何处理这种情况(需要递归地需要接口类型),我显然缺少一些东西。
如果我接受编译器的建议(以及这个类似问题的建议)并更改 getchildren 的 getchildren 实现以返回 []treenode 我会在 getchildren() 实现中收到一个新的编译器错误
cannot use my_struct.Children (type []*MyStruct) as type []TreeNode in return argument
这也是一个惊喜。另外,共识似乎是我应该从方法返回 structs,而不是返回 interfaces。我绝对可以接受从该方法返回 []treenode ,但我一定忽略了一些东西,因为编译器不高兴。
go版本:go版本go1.13.8 darwin/amd64
解决方案
“接受接口,返回结构”不是一条规则,而是一种让生活更轻松的实践。但是,在这种情况下,对于您的接口定义,您没有太多选择。
您可以保留您的设计,但实现 getchildren:
func (my_struct *mystruct) getchildren() []treenode {
ret:=make([]treenode,0,len(my_struct.children))
for _,x:=range my_struct.children {
ret=append(ret,x)
}
return ret
}
由于 go 类型系统的严格性质,这是必需的。
或者,您可以稍微修改一下您的设计:
type MyStruct struct {
Name string
Children []TreeNode
}
如果您在使用 children 时需要访问底层结构,则需要使用类型断言。但是,它可以让您构建具有不同类型节点的树。
今天关于《如何使用其中一个方法返回同一接口的对象的接口?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在米云公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
