Interface – 2
Then interface-1 started learning new things about interfaces!
Query interface
What does interface query mean? It means to query whether the class implements an interface. As I mentioned above, because there is no clear inheritance relation “non-immersion” in Go language, we do not know which interfaces are implemented by this class, so is interface query introduced?
In the Go language, whether an object satisfies one interface and queries other interfaces through one interface can be done automatically. In Windows, there is a COM object, and this COM object has a QueryInterface. In Go, interface query is very similar to COM interface query. You can use one interface to query other interfaces implemented by the object. Implementing interface query process in COM seems very complicated. COM interface query is described as follows, which is also applicable to Go.
Can you fly? // IFly > no. Can you swim? // ISwim > yes. Can you eat? // IShout > yes. >...Copy the code
Understanding: I’m going to keep asking until I’ve asked all the questions? . My goodness is beyond the realm of humanity !!!! So is it possible to know in Go whether an interface is pointing to an object of a certain type? Such as:
var file1 Writer = ...
if file6, ok := file1.(*File); ok {
...
}
Copy the code
This if statement determines whether the object instance pointed to by the file1 interface is of type *File and executes specific code if it is.
This use of querying whether an object pointed to by an interface is of a certain type can be considered only a special case of interface querying. An interface is an abstraction of the common features of a set of types, so the difference between a query interface and a query concrete type is like the difference between these two questions:
> are you a monkey? > is. Word-wrap: break-word! Important; "> < p style =" max-width: 100%; > is.Copy the code
The first query is a group, is the query interface; And the second sentence has been to the specific individual, is to query the specific type. Difficult words! Is that the equivalent of you being human? Is the interface to query. Are you Lei Xiaohong? Is a specific type of query.
Similar queries are available in Java, such as whether an object inherits from a type (base class queries) or implements an interface (interface-derived queries), but dynamic queries in Java are not the same as dynamic queries in the Go language. Such as:
Word-wrap: break-word! Important; "> < p style =" max-width: 100%;Copy the code
For the above question, the base query looks like asking, “Is your dad funny?” Interface-derived queries look like this: “Do you have a license to be funny?” In Go language, it is determined to meet what kind of conditions is funny, such as funny than what skills requirements (can tell jokes, walk funny) and then according to the conditions one by one torture, as long as you meet the conditions you are funny than, and do not care whether you have a funny license. So how does interface derivation determine that I am a programmer? Requires a full programmer license……
Types of queries
In Go, it is more straightforward to ask about the type of object instance that the interface points to. As follows:
var v1 interface{} = ... Switch v := v1.(type) {case int: // Now v type int case string: // now v type string... }Copy the code
Just as there are too many species in real life, there are too many types in languages, so type queries are not used very often. It is more of a supplement and needs to be used with interface queries. As follows:
type Stringer interface { String() string } func Println(args ... Interface {}) {for _, arg := range args {switch v := v1.(type) {case int: // Default: if v, ok := arg.(Stringer); Ok {// now the type of v is Stringer val := v.string () //... } else { // ... }}}}Copy the code
The interface combination
It is easy to understand that the methods in an interface are combined into a new interface, such as IO with both read and write interfaces.
Read the interface
type Read interface {
Read(p []byte) (n int, err error)
}
Copy the code
Write the interface
type Writer interface {
Write(p []byte) (n int, err error)
}
Copy the code
The new interface is combined as follows:
type ReadWriter interface {
Reader
Writer
}
Copy the code
Equivalent to writing as follows:
type ReadWriter interface {
Read(p []byte) (n int, err error)
Write(p []byte) (n int, err error)
}
Copy the code
You can think of interface composition as a particular case of type anonymous composition, except that the interface contains only methods and does not contain any member variables. This is a bit like the way we often have to refactor multiple methods together in development, exposing only one method for others to use, rather than allowing others to use each method call.
Any type
Since Any object instance in Go satisfies the empty interface{}, interface{} looks like an Any type that can point to Any object, as follows:
Var v1 interface{} = 1 var v2 interface{} = "ABC" var v2 interface{} = "ABC" var v3 interface{} Var v5 interface{} = &struct{X int}{1} var v5 interface{} = &struct{X int}{1}Copy the code
When a function can accept any object instance, it will be declared as interface{}. The most typical example is the function of PrintXXX series in standard library FMT. Generally speaking, interface{} is similar to IUnknown in COM, about which we know nothing at first. But you can step into it through interface queries and type queries.
conclusion
- Are you a monkey?
- Type query: are you a monkey invited by the funny than?
- Interface combination: the “are you the monkey invited funny than?” “And” Are you sun Wukong? “Are you a funny monkey monkey?”
note
This article is participating in the “Nuggets Golang Theme Learning Month”. Click here for more details.