1 Type assertion
Writing: x (T)..
x: a need to assert the interface type, T: assertion types.
For: determining the port T x satisfies the query contains the T x of the interface.
2 determines whether a variable implements the specified interface
example:
if err,ok :=x.(error); ok {
return err.Error()
}
3 types of conversions
Definition: If we want to reverse this interface variables ⾥ ⾯ know the actual storage is the type of the object which can be recorded using the following ⽅ be performed because the conversion
var t int
var x interface{}
x = t
y, ok = x.(int) //转成int,带检查
Gets the type 4
func justify(items ...interface{}) {
for index, v := range items {
switch v.(type) {
case int:
fmt.Printf("第 %d 个参数 is int\n", index)
case int32:
fmt.Printf("第 %d 个参数 is int32\n", index)
case float32:
fmt.Printf("第 %d 个参数 is float32\n", index)
}
}
}
func main(){
var a int
var b float32
var c int32
justify(a, b, c)
}