【发布时间】:2020-05-01 13:08:59
【问题描述】:
我在 go 中声明一个接口
type comparable interface {
GetKey() float32
Compare(comparable) int
}
并通过创建这个结构来实现这个接口
type Note struct {
id int
text string
priority float32
}
func (note Note) GetKey() float32 {
return note.priority
}
func (note Note) Compare(note2 Note) int {
if note.priority < note2.priority {
return -1
} else if note.priority > note2.priority {
return 1
} else {
return 0
}
}
但是,当我将注释对象传递给接受可比较接口作为参数的函数时,我收到“方法比较的类型错误”错误。
我是否遗漏了什么或做错了什么?请帮忙
提前致谢
【问题讨论】: