Golang – 接口转换为float64后未定义比较运算符。

huangapple go评论80阅读模式
英文:

Golang - Comparison operator not defined on interface converted to float64

问题

如下所示,我正在对interface{}进行一些类型转换,以便进行正确的类型比较。我试图将interface{}转换为float64string,然后将其与一个值进行比较。然而,当在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)) &gt; subval.Value (operator &gt; not defined on interface)
invalid operation: (interface {})(val.(float64)) &gt;= subval.Value (operator &gt;= not defined on interface)
invalid operation: (interface {})(val.(float64)) &lt; subval.Value (operator &lt; not defined on interface)
invalid operation: (interface {})(val.(float64)) &lt;= subval.Value (operator &lt;= 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 &quot;greater_than&quot;:
		if val.(float64) &gt; subval.Value {
			runAction(subval.Action, Config)
		}
	case &quot;greater_than_or_equal_to&quot;:
		if val.(float64) &gt;= subval.Value {
			runAction(subval.Action, Config)
		}
	case &quot;equal_to&quot;:
		if val.(float64) == subval.Value {
			runAction(subval.Action, Config)
		}
	case &quot;less_than&quot;:
		if val.(float64) &lt; subval.Value {
			runAction(subval.Action, Config)
		}
	case &quot;less_than_or_equal_to&quot;:
		if val.(float64) &lt;= subval.Value {
			runAction(subval.Action, Config)
		}
	}
case string:
	if subval.Type == &quot;equal_to&quot; {
		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类型的接口。

我是这样解决的:

  1. 初始化变量。
  2. 使用fmt.Sprint()函数将返回的值转换为字符串,并将其赋值给变量。
  3. 然后将字符串转换为所需的类型。
  4. 这样我就能够比较这些值了。

例如:

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:

  1. Initialized the variable
  2. Assigned it a value that I return using fmt.Sprint() - this function returns the passed value as a string to your variable.
  3. Then I converted the string to the type I needed
  4. And so I was able to compare these values

For example:

list1.Front().Value &gt; 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&#39;s USING func Atoi()
// you can compare these two values
isItTrue := value1 &gt; value2

huangapple
  • 本文由 发表于 2014年7月3日 00:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/24536598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定