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 do:
for 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: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:
The code prints you completely different memory locations for the value from range and the actual value in the slice:
So the only thing you can do is to either use pointers or the index, as already proposed by jnml and peterSO.
I'd adapt your last suggestion and use the index-only version of range.
It seems simpler to me to refer to
n.Attr[i]
explicitly in both the line that testsKey
and the line that setsVal
, rather than usingattr
for one andn.Attr[i]
for the other.For example:
Playground
Output
Alternative approach:
Playground
Output:
You seem to be asking for something equivalent to this:
Output:
This avoids creating a--possibly large--copy of type
Attribute
values, at the expense of slice bounds checks. In your example, typeAttribute
is relatively small, twostring
slice references: 2 * 3 * 8 = 48 bytes on a 64-bit architecture machine.You could also simply write:
But, the way to get an equivalent result with a
range
clause, which creates a copy but minimizes slice bounds checks, is: