如何用反射附加将切片作为地图值?
假设
- 我正在使用GO1.17不是1.18的
- ,因此GO 1.18中的答案可能会帮助他人,但对我没有帮助。我搜索并尝试了很多事情,但是这种情况从未解决。
问题
import (
"fmt"
"reflect"
)
func main() {
l := map[string][]interface{}{"a": {}}
appendData(l["a"])
fmt.Println(l["a"])
}
func appendData(k interface{}) {
lValue := reflect.ValueOf(k)
lValue.Set(reflect.Append(lValue, reflect.ValueOf(1)))
lValue.Set(reflect.Append(lValue, reflect.ValueOf(2)))
lValue.Set(reflect.Append(lValue, reflect.ValueOf(3)))
}
我将场景简化为此代码。
我只需要能够更改从appenddata
function。 请不要给我这条代码l [“ a”] = appenddata(l [a a'])。([]接口{})
。<
我知道这有效,但由于某种原因我无法在代码中实现它。(我在做一些BFS工作,而我做不到,我必须在当时更改一些值)
我想要什么?
我只想看到这个输出:
[1, 2, 3]
有可能吗?
还有其他其他方法可以从代码中的其他某个地方更改这些数据吗?
感谢您的帮助。
Assumptions
- I'm using go1.17 not 1.18 so answers in go 1.18 may help others but not me.
- I searched and tried many things but this scenario never solved.
Problem
import (
"fmt"
"reflect"
)
func main() {
l := map[string][]interface{}{"a": {}}
appendData(l["a"])
fmt.Println(l["a"])
}
func appendData(k interface{}) {
lValue := reflect.ValueOf(k)
lValue.Set(reflect.Append(lValue, reflect.ValueOf(1)))
lValue.Set(reflect.Append(lValue, reflect.ValueOf(2)))
lValue.Set(reflect.Append(lValue, reflect.ValueOf(3)))
}
I simplified the scenario into this piece of code.
I just need to have the ability to change elements of that passed slice of interfaces([]interface{}
) from appendData
function.
Please do not send me this line of code l["a"] = appendData(l["a"]).([]interface{})
.
I know that this works but I can't implement that in my code for some reason.(I'm doing some BFS stuff and I can't do this, I have to change some values at the time)
What I Want?
I just wanna see this output:
[1, 2, 3]
Is it possible?
Are there any other alternative ways that I can change those data from somewhere else in my code?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将永远不会看到
[1,2,3]
打印,无论appenddata()
仅通过片段,就可以。2件事:您无法更改存储在地图中的值。如果必须更改该值,则必须重新分配新值。有关详细信息,请参见
第二:您也无法更改存储在接口中的值。有关详细信息,请参见一种主张的接口切片
正确的方法:如果您想更改某些东西,请始终将指针传递给它,并修改指向值。当然,在您的情况下,您无法传递指向地图中存储的值的指针(“指向地图”),因此这是不可能的。地图索引表达式无法寻址,有关详细信息,请参见不能接受地图元素的地址。如果您必须使用地图,则必须在地图中存储一个指针,并且可以通过该指针。
另一种方法是返回新的,修改的切片并将新切片分配给呼叫者,确切的/如何/hredin
append()
做(append()
返回您期望分配/存储的新切片)。如果沿着此路线沿着此路线,则可以将非分子切片存储在地图中,因为您可以将修改后的切片重新分配到同一地图键。You will never see
[1, 2, 3]
printed, no matter whatappendData()
does if only the slice is passed.2 things: You can't change values stored in maps. If the value must be changed, you have to reassign the new value. For details see How to update map values in Go
Second: you also can't change values stored in interfaces. For details, see Removing an element from a type asserted Slice of interfaces
The right approach: if you want to change something, always pass a pointer to it, and modify the pointed value. Of course in your case you can't pass a pointer that points to a value stored in a map (that "points into the map"), so that's not possible. Map index expressions are not addressable, for details, see Cannot take the address of map element. If you must use a map, you must store a pointer in the map, and you may pass that pointer.
Another approach is to return the new, modified slice and assign the new slice at the caller, exactly what / how the builtin
append()
does it (append()
returns a new slice which you're expected to assign / store). If you go down this route, you may store non-pointer slices in the map, since you can reassign the modified slice to the same map key.