面试题答案
一键面试package main
import (
"fmt"
)
type Person struct {
Name string
Age int
}
func main() {
var sliceI []interface{}
sliceI = append(sliceI, 10, "hello", Person{Name: "John", Age: 20})
for _, v := range sliceI {
switch v := v.(type) {
case int:
fmt.Printf("int类型的值: %d\n", v)
case string:
fmt.Printf("string类型的值: %s\n", v)
case Person:
v.Age++
fmt.Printf("Person类型的值: Name: %s, Age: %d\n", v.Name, v.Age)
}
}
}