Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing interface in golang gives method has pointer receiver [duplicate]

Tags:

go

am working in Golang, am trying to use interfaces to agroup a set of structs that are using a similar method. Briefly I have this:

type Person interface {
    BasicInfo() MemberBasicInfo
}

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

Then, the structs that should work as interfaces:

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)

What am I missing? or what am I doing wrong?

like image 319
Sredny M Casanova Avatar asked Nov 28 '22 22:11

Sredny M Casanova


1 Answers

If your interface is declared like this:

type Person interface {
    BasicInfo() MemberBasicInfo
}

Then any type that implements a BasicInfo() MemberBasicInfo will fulfil the interface.

In your case, you then created this method:

func (member *Member) BasicInfo() MemberBasicInfo

That means that the type *Member fulfils the interface.

But.. note the * there. Member does not implement the interface, it is *Member who does.

So when trying to assign a type to an interface variable, make sure to use a pointer and not a direct value.

This should work:

var p Person
p = &Member{}

But this will not:

var p Person
p = Member{}
like image 60
eugenioy Avatar answered Dec 21 '22 23:12

eugenioy