gpt4 book ai didi

go - 在golang中实现接口(interface)给方法有指针接收者

转载 作者:IT王子 更新时间:2023-10-29 01:16:38 26 4
gpt4 key购买 nike

<分区>

我在 Golang 工作,我正在尝试使用接口(interface)来对一组使用类似方法的结构进行分组。简而言之,我有这个:

type Person interface {
BasicInfo() MemberBasicInfo
}

type MemberBasicInfo struct {
ID uint
MemberFirstName string
MemberNickname string
MemberEmail string
}

然后,应该作为接口(interface)工作的结构:

func (member *Member) BasicInfo() MemberBasicInfo{
return MemberBasicInfo{
ID: member.ID,
...other mapping here...
}
}

func (user *User) BasicInfo() MemberBasicInfo{
return MemberBasicInfo{
ID: uint(user.ID),
...other mapping here...
}
}

cannot use memberData (type Member) as type Member.Person in return argument: Member does not implement Person (BasicInfo method has pointer receiver)
cannot use memberData (type User) as type Person in return argument: User does not implement Person (BasicInfo method has pointer receiver)

我错过了什么?或者我做错了什么?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com