In the Go language, type assertion is an operation used on interface values. It is used to check whether the value held by the interface type variable implements the expected interface or specific type. The syntax is "value, ok := x.(T)". What can be done through type assertions: 1. Check whether i is nil; 2. Check whether the value stored in i is of a certain type.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Assertion is a programming term expressed as some Boolean expression. When writing code, we always make some assumptions, and assertions are used to capture these assumptions in the code. To simply understand assertion, it means judgment. So in Go, we can understand type assertions as type judgments.
Type assertion (Type Assertion) is an operation used on interface values to check whether the value held by the interface type variable is implemented The desired interface or specific type.
The syntax format of type assertions in Go language is as follows:
value, ok := x.(T)
Among them, x represents the type of an interface, and T represents a specific type (can also be an interface type).
This assertion expression will return the value of x (that is, value) and a Boolean value (that is, ok). You can judge whether x is of type T based on the Boolean value:
If T is a specific type, the type assertion will check whether the dynamic type of x is equal to the specific type T. If the check succeeds, the type assertion returns a dynamic value of x whose type is T.
If T is an interface type, type assertion checks whether the dynamic type of x satisfies T. If the check is successful, the dynamic value of x will not be extracted and the return value is an interface value of type T.
No matter what type T is, if x is a nil interface value, the type assertion will fail.
There are two main forms of type assertions in Go
Variable.(type)
. For example: i.(int)
variable, bool = variable.(type)
. For example: num,ok = i.(int). ok means to determine whether the type is successful.
Usage of type assertion
You can do the following things through type assertion
Check if i
is nil
Check if i
the stored value is of a certain type
There are two specific ways to use it:
The first one:
t := i.(T)
This expression can assert an interface object (i) It is not nil, and the type of the value stored in the interface object (i) is T. If the assertion succeeds, the value will be returned to t. If the assertion fails, panic will be triggered.
Let’s write a piece of code to test it
package main import "fmt" func main() { var i interface{} = 10 t1 := i.(int) fmt.Println(t1) fmt.Println("=====分隔线=====") t2 := i.(string) fmt.Println(t2) }
The output after running is as follows. You can find that it failed when executing the second assertion and triggered panic
10 =====分隔线===== panic: interface conversion: interface {} is int, not string goroutine 1 [running]: main.main() E:/GoPlayer/src/main.go:12 +0x10e exit status 2
If you want The asserted interface value is nil, so let’s see if it will trigger panic as expected
package main func main() { var i interface{} // nil var _ = i.(interface{}) }
The output is as follows, it will indeed trigger panic
panic: interface conversion: interface is nil, not interface {} goroutine 1 [running]: main.main() E:/GoPlayer/src/main.go:5 +0x34 exit status 2
Second type
t, ok:= i.(T)
Same as above, this expression can also assert that an interface object (i) is not nil, and the type of the value stored in the interface object (i) is T. If the assertion is successful, it will be returned Its type is t, and the value of ok is true at this time, indicating that the assertion is successful.
If the type of the interface value is not the T we asserted, the assertion will fail, but unlike the first expression, this will not trigger panic, but will set the value of ok to false , indicating that the assertion failed, and t is the zero value of T at this time.
Slightly modify the above example, as follows
package main import "fmt" func main() { var i interface{} = 10 t1, ok := i.(int) fmt.Printf("%d-%t\n", t1, ok) fmt.Println("=====分隔线1=====") t2, ok := i.(string) fmt.Printf("%s-%t\n", t2, ok) fmt.Println("=====分隔线2=====") var k interface{} // nil t3, ok := k.(interface{}) fmt.Println(t3, "-", ok) fmt.Println("=====分隔线3=====") k = 10 t4, ok := k.(interface{}) fmt.Printf("%d-%t\n", t4, ok) t5, ok := k.(int) fmt.Printf("%d-%t\n", t5, ok) }
The output after running is as follows. It can be found that when the second assertion was executed, although it failed, it did not trigger a panic.
10-true =====分隔线1===== -false =====分隔线2===== <nil> - false =====分隔线3===== 10-true 10-true
In the above output, you should pay attention to that the output of the second assertion before -false
is not whether there is any value of t2 output, but because the assertion failed, so What t2 gets is that the zero value of the string is also ""
, which is zero length, so you can't see its output.
If you need to distinguish between multiple types, you can use type switch assertions. This will be simpler, more direct, and more efficient than making type assertions one by one.
package main import "fmt" func findType(i interface{}) { switch x := i.(type) { case int: fmt.Println(x, "is int") case string: fmt.Println(x, "is string") case nil: fmt.Println(x, "is nil") default: fmt.Println(x, "not type matched") } } func main() { findType(10) // int findType("hello") // string var k interface{} // nil findType(k) findType(10.23) //float64 }
The output is as follows
10 is int hello is string <nil> is nil 10.23 not type matched
Extra explanation:
case nil
[Related recommendations: Go video tutorial,Programming Teaching】
The above is the detailed content of What is type assertion in go language?. For more information, please follow other related articles on the PHP Chinese website!