This is a creation in Article, where the information may have evolved or changed.
A simple record of the usage scenarios for type assertions (types assertion) are normally developed.
1. Parameters used to convert interface{} types in a function
All types in Golang implement an empty interface interface{}, so it is usually used as an abstract type parameter of a function. To give a simple chestnut:
package mainimport "fmt"func main() { add(1, 2) add(int16(1), int16(2)) add(float32(1.1), float32(2.2)) add(float64(1.1), float64(2.2)) add(true, false)}func add(a, b interface{}) { switch t := a.(type) { case int: fmt.Printf("type [%T] add res[%d]\n", t, a.(int)+b.(int)) case int16: fmt.Printf("type [%T] add res[%d]\n", t, a.(int16)+b.(int16)) case float32: fmt.Printf("type [%T] add res[%f]\n", t, a.(float32)+b.(float32)) case float64: fmt.Printf("type [%T] add res[%f]\n", t, a.(float64)+b.(float64)) default: fmt.Printf("type [%T] not support!\n", t) }}
Output Result:
Type [int] Add res[3]
Type [int16] add res[3]
Type [float32] add res[3.300000]
Type [float64] add res[3.300000]
Type [bool] not support!
Using interface{} As a parameter, is not much like a C + + template function, and type assertion is not very much like the C + + class hierarchy between the downstream conversion (also not necessarily successful). It is important to note that a. (type) can only be used in conjunction with switch. The assertion indicates the type of the variable before it is used, and panic is triggered if an assertion error occurs.
If you do not want to trigger panic, then use the first judgment.
package mainimport "fmt"func main() { a := int16(2) b := int32(3) add(a, b)}func add(a, b interface{}) { _, ok := a.(int32) if !ok { fmt.Println("error type assertion!") } b = b}
Operation Result:
Error type assertion!
2. Use as a field type for a struct
For example, we write handler to receive messages, and it is not possible to write a function to handle every message sent. By using the attributes of the null interface and type assertion, the business can be abstracted:
package mainimport "fmt"import "time"type NetMsg struct { MsgID int16 Data interface{}}type Cat struct { name string age int16}type Dog struct { name string age int32}type human struct { name string age int64}func main() { msg1 := NetMsg{1, Cat{"Qian", 1}} msg2 := NetMsg{2, Dog{"doge", 8}} msg3 := NetMsg{3, Dog{"allu", 18}} msg_handler(msg1) time.Sleep(2000 * time.Millisecond) msg_handler(msg2) time.Sleep(2000 * time.Millisecond) msg_handler(msg3)}func msg_handler(msg NetMsg) { switch msg.MsgID { case 1: cat := msg.Data.(Cat) fmt.Printf("Do Something with Msg 1 %v \n", cat) case 2: dog := msg.Data.(Dog) fmt.Printf("Do Something with Msg 2 %v \n", dog) default: fmt.Printf("Error MsgID [%d] \n", msg.MsgID) }}
Operation Result:
Do Something with MSG 1 {Qian 1}
Do Something with MSG 2 {Doge 8}
Error MsgID [3]