面试题答案
一键面试package main
import (
"fmt"
"reflect"
)
// 定义接口
type BaseInterface interface {
DoSomething()
}
// 定义结构体并实现接口
type MyStruct struct{}
func (m MyStruct) DoSomething() {
fmt.Println("MyStruct's DoSomething method")
}
// 通用处理函数
func handleData(data interface{}) {
// 使用反射获取接口值的类型
valueOf := reflect.ValueOf(data)
// 判断是否为空接口
if valueOf.Kind() == reflect.Invalid {
fmt.Println("The data is nil")
return
}
switch valueOf.Kind() {
case reflect.Struct:
// 判断结构体是否实现了 BaseInterface 接口
if valueOf.Type().Implements(reflect.TypeOf((*BaseInterface)(nil)).Elem()) {
valueOf.Interface().(BaseInterface).DoSomething()
} else {
fmt.Println("The struct does not implement BaseInterface")
}
case reflect.Slice:
for i := 0; i < valueOf.Len(); i++ {
// 获取切片中的元素
elem := valueOf.Index(i)
// 判断元素是否实现了 BaseInterface 接口
if elem.Type().Implements(reflect.TypeOf((*BaseInterface)(nil)).Elem()) {
elem.Interface().(BaseInterface).DoSomething()
} else {
fmt.Printf("Element at index %d does not implement BaseInterface\n", i)
}
}
case reflect.Map:
// 遍历 map
for _, key := range valueOf.MapKeys() {
// 获取 map 的值
mapValue := valueOf.MapIndex(key)
// 判断值是否实现了 BaseInterface 接口
if mapValue.Type().Implements(reflect.TypeOf((*BaseInterface)(nil)).Elem()) {
mapValue.Interface().(BaseInterface).DoSomething()
} else {
fmt.Printf("Value for key %v does not implement BaseInterface\n", key)
}
}
default:
fmt.Println("Unsupported type")
}
}
你可以使用以下方式调用这个函数:
func main() {
var s MyStruct
var data1 interface{} = s
handleData(data1)
var data2 interface{} = []MyStruct{{}, {}}
handleData(data2)
var data3 interface{} = map[string]BaseInterface{"key1": s}
handleData(data3)
}
代码说明
- 接口和结构体定义:
- 定义了
BaseInterface
接口,包含DoSomething
方法。 - 定义了
MyStruct
结构体并实现了BaseInterface
接口的DoSomething
方法。
- 定义了
handleData
函数:- 使用
reflect.ValueOf
获取空接口值的反射对象。 - 首先判断接口是否为空。
- 使用
switch
根据空接口值的类型进行处理:- 结构体:判断结构体是否实现了
BaseInterface
接口,如果实现则调用DoSomething
方法。 - 切片:遍历切片元素,判断每个元素是否实现了
BaseInterface
接口,如果实现则调用DoSomething
方法。 - 映射:遍历映射,判断每个值是否实现了
BaseInterface
接口,如果实现则调用DoSomething
方法。 - 其他类型:打印不支持的类型信息。
- 结构体:判断结构体是否实现了
- 使用
main
函数:- 创建不同类型的空接口变量,并调用
handleData
函数进行处理,展示函数的使用方式。
- 创建不同类型的空接口变量,并调用