类型开关的俱乐部值

发布于 2025-01-23 12:08:47 字数 1115 浏览 0 评论 0原文

以下代码工作正常

var requestMap map[string]interface{}
for _, value := range requestMap {
    switch v := value.(type) {
    case []interface{}:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    case string:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    }
}

,但是以下代码给出了len function的无效参数,我读过这个问题

var requestMap map[string]interface{}
for _, value := range requestMap {
    switch v := value.(type) {
    case []interface{}, string:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    }
}

此情况语句难道不够识别[]接口{}string作为值类型? 为什么仍将接口{}作为len()的参数

Following code is working fine

var requestMap map[string]interface{}
for _, value := range requestMap {
    switch v := value.(type) {
    case []interface{}:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    case string:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    }
}

But following code is giving invalid argument for len function, I have read this question

var requestMap map[string]interface{}
for _, value := range requestMap {
    switch v := value.(type) {
    case []interface{}, string:
        if len(v) == 0 {
            // if is empty then no need to throw NA
            return http.StatusOK, nil
        }
    }
}

Isn't this case statement enough to identify []interface{} or string as value type ?
Why is it still considering interface{} as parameter of len()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2025-01-30 12:08:47

如果您在类型开关的案例中列出了多种类型,则switch> switch变量的静态类型将是原始变量的类型。 spec:switch语句:

在案例列出一种类型的子句中,该变量具有该类型;否则,该变量具有类型的表达方式。

因此,如果开关表达式为v:= value。(type),在匹配案例中v(列表多种类型)将是value的类型,在您的情况下,它将是接口{}。 andinin len() 函数不允许通过值传递值类型接口{}

If you list multiple types in a case of a type switch, the static type of the switch variable will be of the type of the original variable. Spec: Switch statements:

In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.

So if the switch expression is v := value.(type), the of v in the matching case (listing multiple types) will be the type of value, in your case it will be interface{}. And the builtin len() function does not allow to pass values of type interface{}.

浅唱ヾ落雨殇 2025-01-30 12:08:47

这是因为在类型开关的情况下,应将v转换为接口([]接口)和String同时。编译器无法决定要使用哪个,因此它将值还原为接口{},因为它可以是任何东西。

This is because in the case of the type switch, the v should be converted to a slice of interface ([]interface) and to a string at the same time. The compiler can't decide which to use, so it revert back the value to interface{}, since it can be anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文