为何接口 c 在第 106 行上被 p 拒绝了?
来源:stackoverflow
时间:2024-02-24 11:36:15 216浏览 收藏
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《为何接口 c 在第 106 行上被 p 拒绝了?》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
问题内容
import "fmt"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type NodeType uint
const (
COMMAND NodeType = iota
PROPERTY
)
type Node interface {
setChildren(...*Node)
getChildren() []*Node
setParent(*Node)
getParent() *Node
getFlavor() NodeType
getValue() string
}
// Command Node
type CommandNode struct {
self *Node
parent *Node
children []*Node
command string
level int
partial, complete bool
}
func (cn *CommandNode) setChildren(child ...*Node) {
for _,v := range child {
cn.children = append(cn.children,v)
}
}
func (cn *CommandNode) getChildren() []*Node {
return cn.children
}
func (cn *CommandNode) setParent(parent *Node) {
cn.parent = parent
}
func (cn *CommandNode) getParent() *Node {
return cn.parent
}
func (cn *CommandNode) getFlavor() NodeType {
return COMMAND
}
func (cn *CommandNode) getValue() string {
return cn.command
}
// Property Node
type PropertyNode struct {
self *Node
parent *Node
children []*Node
property string
level int
partial, complete bool
}
func (pn *PropertyNode) setChildren(child ...*Node) {
for _,v := range child {
pn.children = append(pn.children,v)
}
}
func (pn *PropertyNode) getChildren() []*Node {
return pn.children
}
func (pn *PropertyNode) setParent(parent *Node) {
pn.parent = parent
}
func (pn *PropertyNode) getParent() *Node {
return pn.parent
}
func (pn *PropertyNode) getFlavor() NodeType {
return PROPERTY
}
func (pn *PropertyNode) getValue() string {
return pn.property
}
func main() {
c := CommandNode{
command: "command",
}
p := PropertyNode{
property: "data 1, data 2, data 3",
}
c.setChildren(&p)
x := c.getChildren()
for k,v := range x {
fmt.Printf("x[%d] is %v\n",k,v)
}
}
这一行 -> c.setchildren(p) 在编译时失败,说我不能使用 propertynode 作为 *node,我的印象是 propertynode 具有 node 接口上定义的接口方法,我可以互换使用它们吗?
我的最终目标是能够拥有一个使用相同方法进行树遍历的节点树(不同类型的节点)。我认为我可以使用不同节点类型上的接口来完成此任务。
解决方案
PropertyNode 未实现 Node,因为 setChildren 在接口中声明为 setChildren(...*Node),但实现有 setChildren(*Node)。
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
Golang · Go问答 | 2天前 | go · 性能 · bufio · 日志处理 · 错误排查 · 分块读取 Go bufio.Scanner token too long Scanner.Buffer 大日志行501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
112 收藏
-
193 收藏
-
391 收藏
-
488 收藏
-
Golang · Go问答 | 8小时前 | net/http · Go问答 · HTTP重试 · 请求体 · 接口稳定性 · net/http Go HTTP重试 Request.GetBody Request.Clone 请求体复用273 收藏
-
Golang · Go问答 | 8小时前 | go · 安全 · net/http · HTTP重定向 · 请求头 · 请求头 Authorization 安全边界 CheckRedirect Go HTTP重定向268 收藏
-
Golang · Go问答 | 9小时前 | 错误处理 · go · SQL · database/sql · 线上排查 · SCAN 查询结果 database/sql Go问答 rows.Next Rows.Err292 收藏
-
Golang · Go问答 | 9小时前 | 超时 · 错误处理 · go · Context · errors.Is Go context deadline exceeded WithCancelCause309 收藏
-
Golang · Go问答 | 10小时前 | JSON · 超时控制 · Go问答 · HTTP测试 · 接口验收 · JSON Go 超时 接口测试 httptest.NewServer 烟雾测试385 收藏
-
286 收藏
-
182 收藏
-
Golang · Go问答 | 23小时前 | 标准库 · bufio · 网络协议 · Go问答 · 流式读取 · peek Go bufio.Reader 协议解析 Go问答 Discard UnreadByte414 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习