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.
Embedded interfaces have the exact same dynamic capabilities as when the same interfaces are accessed via Tempo or sites. The difference is that they do not have the surrounding navigation bar and background as in Tempo and sites, allowing the host web page to provide its own navigation.
interface{} means you can put value of any type, including your own custom type. All types in Go satisfy an empty interface ( interface{} is an empty interface). In your example, Msg field can have value of any type.
Implementing an interface in Go To implement an interface, you just need to implement all the methods declared in the interface. Unlike other languages like Java, you don't need to explicitly specify that a type implements an interface using something like an implements keyword.
By accepting an interface, you create a flexible API that returns a more tenable and readable struct. Structs and interfaces are Go's way of organizing methods and data handling. Where structs define the fields of an object, like a Person's first and last name.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With