I have a function that returns a value from an enum. The enum definition is as follows:
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
}
The signature for the Visit method is as follows
Visit(tree antlr.ParseTree) interface{}
After calling the method I receive a value of type DataType, and I'm printing the type and return value in the following lines.
fmt.Printf("TYPE -> %T\n", execResult) // Prints exec.DataType (the type)
fmt.Printf("VALUE -> %v\n", execResult) // Prints 5 (the enum value)
And the output is the following:
TYPE -> exec.DataType
VALUE -> 5
Up to this point everything is fine, however I need to make a comparison and that is where I have a problem and that is that there is something that I am not understanding well about Golang. I have the following:
if execResult == BREAK { // This never executes
fmt.Println("It's a break")
return VOID
} else { // This executes
fmt.Println("It's another thing")
}
And this is where I don't know how to continue with the verification of the return type, if I try the following lines I never execute the code that I want, which in this case is to return VOID. My problem is how to compare the return type to perform a specific action depending on the result. I have also tried the following:
switch execResult.(type) {
case DataType:
if execResult.(DataType) == BREAK {
return execResult
}
}
In this case, the case within the switch is not fulfilled either. My question is basically how can I determine the type of an interface {} value returned from a function call.
I think @Charlie Tumahai is right: the problem is a value mismatch. I tried a small example on the Go Playground and it works like we expect: if a
DataTypeis returned fromVisitthen a comparison to aDataTypecan be true.The returned type must be of type
DataType. This is demonstrated by theVisit2method: it returns anint64which will never be equal toBREAK.This is covered in The Go Programming Language Specification under Comparison Operators: