小编Akh*_*ran的帖子

Go 接口:接口没有实现,即使它是

有人可以向我解释为什么在 go 中不允许这种实现吗?我有一个函数,它将函数定义的接口作为参数。这会引发错误。

package main

import (
    "fmt"
)

type Anode int

func (a Anode) IsLess(node Anode) bool { return a < node }

type Node interface {
    IsLess(node Node) bool
}

func test(a, b Node) bool {
    return a.IsLess(b)
}
func main() {
    a := Anode(1)
    b := Anode(2)
    fmt.Println(test(a, b))
}

Run Code Online (Sandbox Code Playgroud)

go go-interface

-1
推荐指数
1
解决办法
93
查看次数

标签 统计

go ×1

go-interface ×1