This is a creation in Article, where the information may have evolved or changed.
In Java there are instanceof such as the keyword judgment type go in the natural also have a corresponding method to determine the type
That is, the Comma-ok assertion
The notation is value, OK: = em. (T) If you make sure that EM is of the same type, you can use Value:=em directly. (T) commonly used in switch statements will be explained below
EM represents the variables to be judged
T stands for the type being judged
Value represents the returned values
OK indicates if the type is changed
Type assertion should be understood at a glance here will not introduce the main is the introduction of my own several problems
1 em must be of type initerface for type assertion
For example, the following code will error
S: = "Brainwu" if V, OK: = S. (string); OK {fmt. Println (v)}
Invalid type assertion:s. (string) (Non-interface type string on left)
In this case, the type assertion is a return non-interface error, as long as the arguments that are passed in the declaration or function are not interface types.
So we can only make type assertions by using S as a interface{} method, as shown in the following code
S: = "Brainwu" if V, OK: = interface{} (s). (string); OK {fmt. Println (v)}
converting s display to interface{} interface type can be type asserted.
2 When the function is a parameter and the called function specifies the parameter type as interface{}, there is no way to call the method directly
For example, the following code is wrong and will be error during compilation
Cannot convert in (type interface {}) to type Handler:need type assertion
Func servehttp (s string) {Fmt. Println (s)}type Handler func (String) func Panduan (in interface{}) {Handler (in) ("Wujunbin")}func Main () {Panduan ( Handler (servehttp))}
According to the error, we have to make the type assertion before we can continue to use this type of function
If V, OK: = in. (Handler); OK {//with what type to judge can only invoke what type of method V ("Brainwu")}
Only allow incoming in parameters to be typed with handler first if the return value is OK means the same type to make the corresponding method call
In addition to the type assertion, if the assertion succeeds, only the method of that type is used, such as for a struct s and a interface assertion
s actually implemented a B two interface
A interface has a () method B interface has a B () method if the struct S is passed into a function as a parameter and is a interface{} type in the function
Then the type assertion with a is only called a () and cannot be called B () because the compiler knows that you are currently a type but you do not know that you are currently type B
3 In addition, it is convenient to explain the combination of switch and type assertion.
Like the following example
Package Mainimport ("FMT") type Element interface {}func main () {var e Element = 100switch Value: = E. (type) {case int:fmt. PRINTLN ("int", value) case string:fmt. Println ("string", value) default:fmt. Println ("Unknown", Value)}}
Type is a keyword
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.