0x01 以下代码能通过编译吗

package main

import "fmt"

type user interface {
    say(string)
}

type man struct{}

func (p *man) say(hello string) {
    fmt.Println(hello)
}

func main() {
    var u user = man{}
    u.say("Hello World")
}

不能通过编译,因为类型man没有实现user接口,实现say方法的是*man类型,两者不能统一。

func (p *man) say(hello string) 改成func (p man) say(hello string) 即可。

最后修改:2018 年 02 月 28 日
如果觉得我的文章对你有用,请随意赞赏