我正在编写类型断言辅助方法,它接受一个 interface{}
并返回 string
和 map[string]interface{}
.我在 case 语句中返回这两个对象。为什么它要求在函数结束时返回?我错过了休息时间吗?
func typeAssertionHelper(r interface{}) (string, map[string]interface{}) {
switch g := r.(type) {
case string:
return g, nil
case []interface{}:
for _, v := range g {
switch s := v.(type) {
case string:
return s, nil
case map[string]interface{}:
return "", s
}
}
}
// missing return end of function
}
最佳答案
传入的 r interface{}
不一定与您在 switch
block 中的情况之一匹配。
本质上,switch
并不详尽。
您可以使用默认大小写扩展 switch
:
switch g := r.(type) {
case string:
...
case []interface{}:
...
default:
...
}
或者只是在函数末尾返回一些东西
返回 "", nil
关于go - 如何处理在 switch case 语句中返回两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49924341/