dongmu7335 2018-07-01 03:01
浏览 73
已采纳

了解接口内部的接口(嵌入式接口)

I was trying to understand the Interface embedding with the following code.

I have the following:

type MyprojectV1alpha1Interface interface {
    RESTClient() rest.Interface
    SamplesGetter
}

// SamplesGetter has a method to return a SampleInterface.
// A group's client should implement this interface.
type SamplesGetter interface {
    Samples(namespace string) SampleInterface
}

// SampleInterface has methods to work with Sample resources.
type SampleInterface interface {
    Create(*v1alpha1.Sample) (*v1alpha1.Sample, error)
    Update(*v1alpha1.Sample) (*v1alpha1.Sample, error)
    Delete(name string, options *v1.DeleteOptions) error
    DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error
    Get(name string, options v1.GetOptions) (*v1alpha1.Sample, error)
    List(opts v1.ListOptions) (*v1alpha1.SampleList, error)
    Watch(opts v1.ListOptions) (watch.Interface, error)
    Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1alpha1.Sample, err error)
    SampleExpansion
}

Now if I have the follwoing:

func returninterface() MyprojectV1alpha1Interface {
//does something and returns me MyprojectV1alpha1Interface
}
temp := returninterface()

Now, from the MyprojectV1alpha1Interface if I want to call the

Create function of SampleInterface

what I need to do?

Also, please explain me how this interfaces work in Golang.

  • 写回答

1条回答 默认 最新

  • doubeng9407 2018-07-01 03:20
    关注

    In this definition:

    type MyprojectV1alpha1Interface interface {
        RESTClient() rest.Interface
        SamplesGetter
    }
    

    Your MyprojectV1alpha1Interface embeds the SamplesGetter interface.

    Embedding an interface inside another interface means all of the methods of the embedded interface (SamplesGetter) can be invoked over the embedding interface (MyprojectV1alpha1Interface).

    That means you can invoke any of the SamplesGetter methods on any object that implements MyprojectV1alpha1Interface.

    So once you get a MyprojectV1alpha1Interface object in your temp variable, you can call the Samples method (with suitable namespace, which I cannot guess from the code you posted):

    sampleInt := temp.Samples("namespace here")
    

    sampleInt will then have a SampleInterface object, so you can then invoke the Create function using your sampleInt variable:

    sample, err := sampleInt.Create(<you should use a *v1alpha1.Sample here>)
    

    For more details about how interfaces work, I'd suggest you go to the official specs and examples:

    https://golang.org/ref/spec#Interface_types

    https://gobyexample.com/interfaces

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入