0

I have few methods in my interface. I have struct that implements those methods. I noticed that it is not possible to implements set of methods as a pointer receiver and few as value receiver.

below is an interface

type ContractCRUD interface {
    addContract() bool
    deleteContract() bool
    updateContract() bool
    addAPI(apipath string) bool
    getContractByNameAndGroup(user string, group APIGroup) error
    getObject() Contract
}

struct that will implement the ContractCRUD Interface

type Contract struct {
    id             int64
    User           string
    Group          APIGroup
    AllowedRequest int64
    Window         int16
}

just listing out the functions definition..

func (c Contract) getObject() Contract {...}
func (c Contract) addContract() bool {...}
.
.
.
func (c *Contract) getContractByNameAndGroup(user string, group APIGroup) error {..}

with such an implementation, even the getObject and addContact, expect the pointer receiver.

func RegisterAPI(c ContractCRUD) bool {
    contract := c.getObject()
    fmt.Printf("Register the user %s under the group %s with the limit %d per %d minute(s)\n", contract.User, contract.Group, contract.AllowedRequest, contract.Window)
    return c.addContract()
}

somewhere in main

...
registration.RegisterAPI(*c)

i get the following error

cannot use *c (type registration.Contract) as type registration.ContractCRUD in argument to registration.RegisterAPI:
    registration.Contract does not implement registration.ContractCRUD (registration.getContractByNameAndGroup method has pointer receiver)

So I understand I cannot have the mix of the implementation, but I don't seem to understand why. I am fairly new to Go. I apologies if this is something very obvious. I tried reading around, but I only found everyone talking about the when to use pointer and value implementation.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
user3602300
  • 129
  • 7
  • 2
    You can actually mix them, but it's generally good practice not to. – Jonathan Hall Jul 14 '19 at 14:22
  • 2
    Related: https://stackoverflow.com/a/27775558/13860 – Jonathan Hall Jul 14 '19 at 14:24
  • 2
    From: https://tour.golang.org/methods/8 "In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both. (We'll see why over the next few pages.)" – colm.anseo Jul 14 '19 at 15:46
  • 3
    The [method set](https://golang.org/ref/spec#Method_sets) value type does not include the pointer receiver methods. The method set for the pointer type does include the value receiver methods. Register the pointer: `registration.RegisterAPI(c)` – Charlie Tumahai Jul 14 '19 at 17:27

1 Answers1

1

You can mix value and pointer receivers in the methods for a type, and the types will be converted if you call a method with a value receiver on a pointer object or if you call a method with a pointer receiver on a non-pointer object (assuming it's addressable). However, if you need a certain type to implement an interface, that type must have all of the methods of the interface defined. In your example, you can have the type Contract implement the interface if all the methods are defined with value receivers, or you can have the type *Contract implement the interface if all the methods are defined with pointer receivers. The important point here is that Contract and *Contract are distinct types, so whichever type you want to implement the interface has to have all of the interface methods defined on it.

Andy Schweig
  • 6,597
  • 2
  • 16
  • 22