Understanding interface inside interface(Embedded Interface)

Multi tool use
Multi tool use


Understanding interface inside interface(Embedded Interface)



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 Answer
1



In this definition:


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



Your MyprojectV1alpha1Interface embeds the SamplesGetter interface.


MyprojectV1alpha1Interface


SamplesGetter



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


SamplesGetter


MyprojectV1alpha1Interface



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


SamplesGetter


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):


MyprojectV1alpha1Interface


temp


Samples


namespace


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



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


sampleInt


SampleInterface


Create


sampleInt


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





Awesome explanation, thanks a lot. I had a understanding of interfaces. But, got confused with interface embedding. But, now its crystal clear. Thanks
– Invictus
Jul 1 at 3:34






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ZvkcwO047m 2QLOqtCL8cXVKI4EfH2FbvoAY7kl9ERKFf7HvgI0uh 6TdkFlPSoZwrNaXQAPbc,ARUitCqLILa2A
JwoU KhSnn4kXv YfTI4JEl2D8P2gYb rU7b,NK,m9pkvgP

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters