首页 > 后端开发 > Golang > 正文

确定 Golang 中函数返回的接口 {} 值的类型

王林
发布: 2024-02-05 11:49:27
转载
1091 人浏览过

确定 Golang 中函数返回的接口 {} 值的类型

问题内容

我有一个从枚举返回值的函数。枚举定义如下:

type DataType int64

const (
    INT DataType = iota
    FLOAT
    STRING
    BOOL
    CHAR
    VOID
    ERROR
    BREAK
    CONTINUE
)

    func (l *TSwiftVisitor) VisitWhileInstr(ctx *parser.WhileInstrContext) interface{} {        
    if condExpression.ValType == BOOL {             
        condResult := condExpression.asBool()       
        for condResult {            
            for _, currentInstr := range ctx.InstrList().AllInstr() {
                execResult := l.Visit(currentInstr)
                fmt.Printf("TYPE -> %T\n", execResult) // Prints exec.DataType (the type)
                fmt.Printf("VALUE -> %v\n", execResult) // Prints 5 (the enum value)
                if execResult == BREAK { // This never executes
                    fmt.Println("Es break")
                    return VOID
                } else { // This executes
                    fmt.Println("Es otra mierda")
                }
            }           
            condResult = l.Visit(ctx.Expr()).(*Expression).asBool()
        }       
    } else {
        return ERROR
    }
    return VOID
}
登录后复制

Visit方法的签名如下

Visit(tree antlr.ParseTree) interface{}
登录后复制

调用该方法后,我收到一个 DataType 类型的值,并在以下几行中打印该类型和返回值。

fmt.Printf("TYPE -> %T\n", execResult) // Prints exec.DataType (the type)
fmt.Printf("VALUE -> %v\n", execResult) // Prints 5 (the enum value)
登录后复制

输出如下:

TYPE -> exec.DataType                   
VALUE -> 5
登录后复制

到目前为止,一切都很好,但是我需要进行比较,这就是我遇到的问题,那就是我对 Golang 不太了解。我有以下内容:

if execResult == BREAK { // This never executes
    fmt.Println("It's a break")
    return VOID
} else { // This executes
    fmt.Println("It's another thing")
}
登录后复制

这就是我不知道如何继续验证返回类型的地方,如果我尝试以下几行,我永远不会执行我想要的代码,在本例中是返回 VOID。我的问题是如何比较返回类型以根据结果执行特定操作。我还尝试过以下方法:

switch execResult.(type) {
    case DataType:
        if execResult.(DataType) == BREAK {

            return execResult
        }
}
登录后复制

在这种情况下,开关内的情况也不满足。我的问题基本上是如何确定从函数调用返回的接口{}值的类型。


正确答案


我认为@Charlie Tumahai 是对的:问题是价值观不匹配。我尝试了 Go Playground 上的一个小示例,它的工作原理与我们预期的一样:如果 DataType 是从 Visit 返回,然后与 DataType 的比较可以为 true。

返回的类型必须DataType 类型。 Visit2 方法演示了这一点:它返回一个 int64,它永远不会等于 BREAK

Go 编程语言规范中的比较运算符下对此进行了介绍:

package main

import "fmt"

type DataType int64

const (
    INT DataType = iota
    BREAK
    CONTINUE
)

func Visit() interface{} { return BREAK }
func Visit2() interface{} {return int64(BREAK) }

func main() {
    for _, x := range []interface{}{Visit(), Visit2()} {
        fmt.Printf("x = %v, T(x) = %T : ", x, x)
        if x == BREAK {
            fmt.Println("x is BREAK")
        } else {
            fmt.Println("Cannot identify x")
        }
    }

    // Output:
    // x = 1, T(x) = main.DataType : x is BREAK
    // x = 1, T(x) = int64 : Cannot identify x
}
登录后复制

以上是确定 Golang 中函数返回的接口 {} 值的类型的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!