英文:
Set a parameter as an interface or a list of interfaces
问题
我对Go还不太熟悉,还有一些细微之处我还不太理解。
例如,我有一个函数可能会这样调用:
myVar.InitOperation("foo", Operator.EQUAL, "bar")
myVar.InitOperation("foo", Operator.INCLUDE, []interface{}{"baz", 1, "boo"})
myVar.InitOperation("foo", Operator.GREATER_THAN, 1)
所以我将这个函数声明为:
func InitOperation(attr string, operator Operator, value interface{}) {
if operator.String() == "BAR" {
doSomething(attr, operator, value)
} else if operator.String() == "INCLUDE" {
doSomethingElse(attr, operator, value)
}
// 根据情况继续处理
}
问题是,当我传递一个字符串或整数时,一切都正常,但当我传递一个数组时,它被解析为单个元素。
在我的函数doSomethingElse
中,我尝试迭代values
,正如你可能猜到的,我遇到了错误。
好吧,我只需将values
设置为[]interface{}
而不是interface{}
。在这里,一切都正常,但当我调用doSomething
时,它被解析为[[myValue]]
,这是合理的,但不是我期望的。
我的问题是,有没有办法根据情况传递[]interface{}
或interface{}
,以便根据情况将其解析为值或值数组?
谢谢!
英文:
I'm pretty new to Go and there are some subtilities that I don't get yet
For instance, I have a function who may be called like this :
myVar.InitOperation("foo",Operator.EQUAL,"bar")
myVar.InitOperation("foo",Operator.INCLUDE,[]interface{}{"baz",1,"boo"})
myVar.InitOperation("foo",Operator.GREATER_THAN,1)
So I declared this function as :
func InitOperation(attr string, operator Operator, value interface{}){
if operator.String() == "BAR"{
doSomething(attr,operator,value)
} else if (operator.String() == "INCLUDE" {
doSomethingElse(attr,operator,value)
}
// And so on regarding to the case
}
The thing is that when I pass a string or an integer, it goes well but when I pass an array, it is parsed as a single element.
In my function doSomethingElse
, I'm trying to iterate over values
, and as you may guess, I have an error.
Fine, I'll just set values
as []interface{}
and not interface{}
. Here, everything goes well but when I'm calling doSomething
, it is parsed as [[myValue]]
which is logical but not what I expect.
My question is, is there a way to pass either an []interface{}
or interface{}
who can be red as a value or an array of values regarding to the case ?
Thanks !
答案1
得分: 2
你需要进行类型断言:
func InitOperation(attr string, operator Operator, value interface{}) {
if operator.String() == "BAR" {
doSomething(attr, operator, value)
} else if operator.String() == "INCLUDE" {
doSomethingElse(attr, operator, value.([]interface{}))
}
// 根据情况继续处理
}
func doSomethingElse(attr string, operator Operator, value []interface{}) {
for _, v := range value {
fmt.Println(v)
}
}
在 InitOperation
函数中,根据 operator
的不同值,调用不同的函数进行处理。当 operator
的值为 "BAR" 时,调用 doSomething
函数;当 operator
的值为 "INCLUDE" 时,调用 doSomethingElse
函数,并将 value
进行类型断言为 []interface{}
类型。在 doSomethingElse
函数中,遍历 value
数组并打印每个元素。
英文:
You need a type assertion:
func InitOperation(attr string, operator Operator, value interface{}){
if operator.String() == "BAR"{
doSomething(attr,operator,value)
} else if (operator.String() == "INCLUDE" {
doSomethingElse(attr, operator, value.([]interface{}))
}
// And so on regarding to the case
}
func doSomethingElse(attr string, operator Operator, value []interface{}) {
for _, v := range value {
fmt.Println(v)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论