英文:
Golang - Comparison operator not defined on interface converted to float64
问题
如下所示,我正在对interface{}
进行一些类型转换,以便进行正确的类型比较。我试图将interface{}
转换为float64
或string
,然后将其与一个值进行比较。然而,当在float64
上使用除==
以外的比较运算符时,Go在构建时会报错。
Go报错如下所示:
invalid operation: (interface {})(val.(float64)) > subval.Value (接口上未定义操作符>)
invalid operation: (interface {})(val.(float64)) >= subval.Value (接口上未定义操作符>=)
invalid operation: (interface {})(val.(float64)) < subval.Value (接口上未定义操作符<)
invalid operation: (interface {})(val.(float64)) <= subval.Value (接口上未定义操作符<=)
...这是我的代码:
val := s.FieldByName(subval.Metric).Interface()
switch val.(type) {
case float64, float32, int:
switch subval.Type {
case "greater_than":
if val.(float64) > subval.Value {
runAction(subval.Action, Config)
}
case "greater_than_or_equal_to":
if val.(float64) >= subval.Value {
runAction(subval.Action, Config)
}
case "equal_to":
if val.(float64) == subval.Value {
runAction(subval.Action, Config)
}
case "less_than":
if val.(float64) < subval.Value {
runAction(subval.Action, Config)
}
case "less_than_or_equal_to":
if val.(float64) <= subval.Value {
runAction(subval.Action, Config)
}
}
case string:
if subval.Type == "equal_to" {
if val.(string) == subval.Value {
runAction(subval.Action, Config)
}
}
}
英文:
As shown in the code below, I'm doing some type switching on an interface{}
in order to do the correct type switching - I'm trying to convert the interface{}
to either a float64
or a string
and then comparing it to a value, however, when using a comparison operator other than ==
on the float64
's, Go complains about it when building.
The errors Go spits out are as follows :-
invalid operation: (interface {})(val.(float64)) > subval.Value (operator > not defined on interface)
invalid operation: (interface {})(val.(float64)) >= subval.Value (operator >= not defined on interface)
invalid operation: (interface {})(val.(float64)) < subval.Value (operator < not defined on interface)
invalid operation: (interface {})(val.(float64)) <= subval.Value (operator <= not defined on interface)
...and this is my code:-
val:= s.FieldByName(subval.Metric).Interface()
switch val(type) {
case float64, float32, int:
switch subval.Type {
case "greater_than":
if val.(float64) > subval.Value {
runAction(subval.Action, Config)
}
case "greater_than_or_equal_to":
if val.(float64) >= subval.Value {
runAction(subval.Action, Config)
}
case "equal_to":
if val.(float64) == subval.Value {
runAction(subval.Action, Config)
}
case "less_than":
if val.(float64) < subval.Value {
runAction(subval.Action, Config)
}
case "less_than_or_equal_to":
if val.(float64) <= subval.Value {
runAction(subval.Action, Config)
}
}
case string:
if subval.Type == "equal_to" {
if val.(string) == subval.Value {
runAction(subval.Action, Config)
}
}
}
答案1
得分: 4
你的问题可能是subval.Value
是一个interface{}
,所以比较运算符不可用。我认为当使用接口进行相等比较时,Go会按字节比较底层类型,因此==
不会报错。
英文:
Your problem is probably that subval.Value
is an interface{}
, so the comparison operator isn't available. I think that when doing an equality comparison with an interface, go compare the underlying types byte-wise, hence the ==
not giving an error.
答案2
得分: 0
如果有人像我一样遇到了这个问题,那么我会这样描述我的情况。让它在这里。
我遇到了类似的问题。需要比较List容器返回的两个int类型的接口。
我是这样解决的:
- 初始化变量。
- 使用fmt.Sprint()函数将返回的值转换为字符串,并将其赋值给变量。
- 然后将字符串转换为所需的类型。
- 这样我就能够比较这些值了。
例如:
list1.Front().Value > list2.Front().Value // 我无法直接比较它们,因为这个方法返回的是interface{}
value1 := fmt.Sprint(list1.Front().Value) // 所以如果你这样做
value2 := fmt.Sprint(list2.Front().Value)
// ...使用func Atoi()将其转换为int类型
// 你可以比较这两个值
isItTrue := value1 > value2
英文:
If someone got here like me, then for him I will describe my situation like this. Let it be here
I had a similar problem. It was necessary to compare the two ints that the List container returns as interfaces.
I solved it this way:
- Initialized the variable
- Assigned it a value that I return using fmt.Sprint() - this function returns the passed value as a string to your variable.
- Then I converted the string to the type I needed
- And so I was able to compare these values
For example:
list1.Front().Value > list2.Front().Value // i cant compare it because this
// method returns inerfase{}
value1 := fmt.Sprint(list1.Front().Value) // so if you do it this way
value2 := fmt.Sprint(list2.Front().Value)
// ...CONVERTING TO int's USING func Atoi()
// you can compare these two values
isItTrue := value1 > value2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论