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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝