而在golang迭代变化值 [英] Change values while iterating in golang
问题描述
让我们假设我有这几种:
键入属性结构{
键,缬氨酸串
}
Node类型结构{
ATTR []属性
}
和我想重复我的节点的属性进行更改。
我会喜欢能够做到
_为,ATTR:=范围n.Attr {
如果attr.Key ==HREF{
attr.Val =东西
}
}
但 ATTR
是不是一个指针,这是行不通的,我必须做的。
对于我,ATTR:=范围n.Attr {
如果attr.Key ==HREF{
n.Attr [I] .VAL =东西
}
}
有没有更简单或更快的方法?是否有可能直接获得的指针范围
?
显然,我不想改变结构,只为迭代和更详细的解决方案是无解
没有,你想要的缩写,是不可能的。
这样做的原因是,范围
复制从你遍历切片的值。
有关范围的规范说:
范围前pression第一个值第二个值(如果第二个变量是present)
数组或一个切片[N] E,* [N] E,或[]①股指I INT A [我们]
块引用>所以,使用范围
A [I]
为数组/片的第二值,这实际上意味着,在
值被复制,使得原来价值碰不得。此行为是由表现出以下code :
X:=令([] INT,3)X [0]中,x [1],X [2] = 1,2,3对于我,VAL:=范围X {
的println(安培; X [I],对,与放大器; VAL)
}该值从范围和实际的code打印您完全不同的内存位置
在切片值:0xf84000f010与0x7f095ed0bf68
0xf84000f014与0x7f095ed0bf68
0xf84000f018与0x7f095ed0bf68所以,你唯一可以做的事情就是要么使用指针或索引,它已经被jnml和peterSO建议。
Let's suppose I have these types :
type Attribute struct { Key, Val string } type Node struct { Attr []Attribute }
and that I want to iterate on my node's attributes to change them.
I would have loved to be able to do
for _, attr := range n.Attr { if attr.Key == "href" { attr.Val = "something" } }
but as
attr
isn't a pointer, this wouldn't work and I have to dofor i, attr := range n.Attr { if attr.Key == "href" { n.Attr[i].Val = "something" } }
Is there a simpler or faster way ? Is it possible to directly get pointers from
range
?Obviously I don't want to change the structures just for the iteration and more verbose solutions are no solutions
解决方案No, the abbreviation you want is not possible.
The reason for this is that
range
copies the values from the slice you're iterating over. The specification about range says:Range expression 1st value 2nd value (if 2nd variable is present) array or slice a [n]E, *[n]E, or []E index i int a[i] E
So, range uses
a[i]
as its second value for arrays/slices, which effectively means that the value is copied, making the original value untouchable.This behavior is demonstrated by the following code:
x := make([]int, 3) x[0], x[1], x[2] = 1, 2, 3 for i, val := range x { println(&x[i], "vs.", &val) }
The code prints you completely different memory locations for the value from range and the actual value in the slice:
0xf84000f010 vs. 0x7f095ed0bf68 0xf84000f014 vs. 0x7f095ed0bf68 0xf84000f018 vs. 0x7f095ed0bf68
So the only thing you can do is to either use pointers or the index, as already proposed by jnml and peterSO.
这篇关于而在golang迭代变化值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!