doushao1087 2019-07-14 14:20
浏览 169

结构将接口方法实现为指针接收者或值接收者

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 reciever.

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)
", 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.

  • 写回答

1条回答 默认 最新

  • doudanglang5826 2019-07-14 17:49
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法