In golang, structure A is nested with another structure B, which can expand A's capabilities.
A not only has the attributes of B, but also has the method of B. There is also a concept of field improvement.
Example:
package main import "fmt" type Worker struct { Name string Age int Salary } func (w Worker) fun1() { ("Worker fun1") } type Salary struct { Money int } func (s Salary) fun1() { ("Salary fun1") } func (s Salary) fun2() { ("Salary fun2") } func main() { s := Salary{} w := Worker{Salary: s} // // // // //w.fun1() //w.fun2() //.fun1() //.fun2() }
It is obvious that Worker now has strong dependence on Salary. Sometimes we hope that Worker only depends on one interface, so that as long as the object that implements this interface can be passed in.
After optimization:
package main import "fmt" type Inter1 interface { fun1() fun2() } type Worker struct { Name string Age int Inter1 } func (w Worker) fun1() { ("Worker fun1") } type Salary struct { Money int } func (s Salary) fun1() { ("Salary fun1") } func (s Salary) fun2() { ("Salary fun2") } func main() { s := Salary{} w := Worker{Inter1: s} // // //w.fun1() //w.fun2() //w.Inter1 //w.Inter1.fun1() //w.Inter1.fun2() // The Money attribute cannot be accessed, you can add methods to implement it}
Worker relies on an Inter1 interface, and any object that implements Inter1 can be injected.
Worker also implements the Inter1 interface.
Worker can reimplement the Inter1 interface method.
This is how golang's context standard library implements nesting between contexts.
In addition, it should be noted that a structure contains an interface, so this structure is naturally an implementation of this interface, even if this structure does not implement any method
type man interface { Eat(args ...any) } type dog struct { man } func testDog() { d := dog{} (1) }
Obviously the call here will report an error.
This implicit implementation feature of golang interface will cause an object to unintentionally implement an interface. However, for some underlying interfaces, it is necessary to maintain its enclosure. In order to achieve this goal, it is usually practiced to have special meanings in the interface, such as interfaces, and comments indicate the intention.
// The Error interface identifies a run time error. type Error interface { error // RuntimeError is a no-op function but // serves to distinguish types that are run time // errors from ordinary errors: a type is a // run time error if it has a RuntimeError method. RuntimeError() }
Or define a method that cannot be exported, so that it cannot be implemented outside the package, such as interfaces
// TB is the interface common to T, B, and F. type TB interface { Cleanup(func()) Error(args ...any) Errorf(format string, args ...any) Fail() FailNow() Failed() bool Fatal(args ...any) Fatalf(format string, args ...any) Helper() Log(args ...any) Logf(format string, args ...any) Name() string Setenv(key, value string) Skip(args ...any) SkipNow() Skipf(format string, args ...any) Skipped() bool TempDir() string // A private method to prevent users implementing the // interface and so future additions to it will not // violate Go 1 compatibility. private() }
The first method obviously can only guard against gentlemen, not villains.
The second method looks safer, but based on our knowledge above, what if we use a structure to include this interface? Can this interface be implemented?
type MyTB struct { }
Obviously MyTB has been implemented, but the call will report an error at this time.
func main() { tb := new(MyTB) ("hello", "world") }
Implement one of the methods and call them
func (p *MyTB) Fatal(args ...interface{}) { (args...) } func main() { tb := new(MyTB) ("hello", "world") }
Since MyTB is implemented, it is possible to perform implicit conversion.
var tb = new(MyTB) ("hello", "world")
This is the end of this article about the implementation of structure nested interfaces in golang. For more related contents of structure nested interfaces, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!