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.