1. Implement interface through structure
1. When understanding the Iris framework, I often see that I write this way using an empty structure as a receiver to call the method. I am a little curious about what the meaning of doing this is.
Explanation: In the Go language, a struct implements all methods in a certain interface, which is called this struct implements the interface.
2. The empty structure has the following characteristics
A. Do not occupy memory address.
B. The address remains unchanged
3. First of all, we know that interface defines abstract methods, and the func below is its concrete implementation (this means similar to Java), but we know that there is no way to instantiate interface in Java because it is abstract. Then the function of this operation is: First, the NewEntraceRepository method returns a pointer to an empty structure (the empty structure must implement the current interface), and then we can directly call the current interface method as long as we use the variables created by NewEntraceRepository.
func NewEntraceRepository()EntraceRepository { return &entraceRepository{}; }
The above code is equivalent to the following
var variable_value EntraceRepository=&entraceRepository{}
Question: Why can this empty structure be assigned to the interface? We know that only if the structure implements all methods of the interface, can the assignment be performed in this way.
func (n bookRepository) GetBookList(m map[string]interface{})(total int,books []){}
In fact, a struct implements all methods in an interface, which is called this struct implements the interface. Therefore, using a variable to receive this address can be called directly.
Let’s write a demo to implement it. First write a Study interface{}, which requires 4 methods: Listen, Speak, Read, and Write, and then write a study struct{} to implement all the methods in it, and then share your code experience.
2. Code examples
//Go - Struct Implementation Interface package main import ( "fmt" "/pkg/errors" ) var _ Study = (*study)(nil) type Study interface { Listen(msg string) string Speak(msg string) string Read(msg string) string Write(msg string) string } type study struct { Name string } func (s *study) Listen(msg string) string { return + " listen " + msg } func (s *study) Speak(msg string) string { return + " explain " + msg } func (s *study) Read(msg string) string { return + " read " + msg } func (s *study) Write(msg string) string { return + " Write " + msg } func New(name string) (Study, error) { if name == "" { return nil, ("name required") } return &study{ Name: name, }, nil } func main() { name := "Xiao Ming " s, err := New(name) if err != nil { (err) } (("english")) (("english")) (("english")) (("english")) }
This is the article about Go implementing interface through structure (struct) this end. For more relevant content on Go structure implementation of interface, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!