我正在通過閱讀“Effective Go”來學習 Go 語言。我找到了一個關于類型切換的例子:var t interface{}t = functionOfSomeType()switch t := t.(type) {default: fmt.Printf("unexpected type %T\n", t) // %T prints whatever type t hascase bool: fmt.Printf("boolean %t\n", t) // t has type boolcase int: fmt.Printf("integer %d\n", t) // t has type intcase *bool: fmt.Printf("pointer to boolean %t\n", *t) // t has type *boolcase *int: fmt.Printf("pointer to integer %d\n", *t) // t has type *int}我的理解是switch從上到下評估案例并在匹配條件下停止。那么這個例子不是總是停留在default并打印“意外類型......”嗎?
添加回答
舉報
0/150
提交
取消