ChatGPT解决这个技术问题 Extra ChatGPT

Change values while iterating

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.

So you want some kind of Array.prototype.forEach in JavaScript?
That's an interesting idea and that could have been a solution but calling a function which would in turn call a function at each iteration looks heavy and wrong in a server side language. And the lacks of generics would make this feel even heavier.
Honestly, I don't think it's that heavy. Calling a function or two is very cheap, this is usually what compilers optimize the most. I'd try it and benchmark it to see if its fits the bill.
As Go lacks generics, I'm afraid the function passed to forEach would necessarily start with a type assertion. That's not really better than attr := &n.Attr[i].

n
nemo

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.


One way to think of this is that assigning a value causes a copy. If you saw val := x[1], it would be completely unsurprising that val was a copy of x[1]. Rather than thinking of the range as doing something special, remember that each iteration of a range begins by assigning the index and value variables, and that it is that assignment rather than the range that causes the copy.
Sorry I'm still a little bit confuse here. If the 2nd value of for loop is a[i], then what is the different between the a[i] from the for loop and the a[i] as we write? It looks like the same thing but it isn't, right?
@TiếnNguyễnHoàng range returns a[i] as its second return value. This operation, val = a[i], as done by range creates a copy of the value so any write operation to val is applied to a copy.
p
peterSO

You seem to be asking for something equivalent to this:

package main

import "fmt"

type Attribute struct {
    Key, Val string
}
type Node struct {
    Attr []Attribute
}

func main() {

    n := Node{
        []Attribute{
            {"key", "value"},
            {"href", "http://www.google.com"},
        },
    }
    fmt.Println(n)

    for i := 0; i < len(n.Attr); i++ {
        attr := &n.Attr[i]
        if attr.Key == "href" {
            attr.Val = "something"
        }
    }

    fmt.Println(n)
}

Output:

{[{key value} {href http://www.google.com}]}
{[{key value} {href something}]}

This avoids creating a--possibly large--copy of type Attribute values, at the expense of slice bounds checks. In your example, type Attribute is relatively small, two string slice references: 2 * 3 * 8 = 48 bytes on a 64-bit architecture machine.

You could also simply write:

for i := 0; i < len(n.Attr); i++ {
    if n.Attr[i].Key == "href" {
        n.Attr[i].Val = "something"
    }
}

But, the way to get an equivalent result with a range clause, which creates a copy but minimizes slice bounds checks, is:

for i, attr := range n.Attr {
    if attr.Key == "href" {
        n.Attr[i].Val = "something"
    }
}

It's a pity that value := &someMap[key] will not work if someMap is a map
peterSO in your first code snippet don't you have to deference attr to assign something to it? i.e. *attr.Val = "something"
worked for me, thanks
P
Paul Hankin

I'd adapt your last suggestion and use the index-only version of range.

for i := range n.Attr {
    if n.Attr[i].Key == "href" {
        n.Attr[i].Val = "something"
    }
}

It seems simpler to me to refer to n.Attr[i] explicitly in both the line that tests Key and the line that sets Val, rather than using attr for one and n.Attr[i] for the other.


z
zzzz

For example:

package main

import "fmt"

type Attribute struct {
        Key, Val string
}

type Node struct {
        Attr []*Attribute
}

func main() {
        n := Node{[]*Attribute{
                &Attribute{"foo", ""},
                &Attribute{"href", ""},
                &Attribute{"bar", ""},
        }}

        for _, attr := range n.Attr {
                if attr.Key == "href" {
                        attr.Val = "something"
                }
        }

        for _, v := range n.Attr {
                fmt.Printf("%#v\n", *v)
        }
}

Playground

Output

main.Attribute{Key:"foo", Val:""}
main.Attribute{Key:"href", Val:"something"}
main.Attribute{Key:"bar", Val:""}

Alternative approach:

package main

import "fmt"

type Attribute struct {
        Key, Val string
}

type Node struct {
        Attr []Attribute
}

func main() {
        n := Node{[]Attribute{
            {"foo", ""},
            {"href", ""},
            {"bar", ""},
        }}

        for i := range n.Attr {
                attr := &n.Attr[i]
                if attr.Key == "href" {
                        attr.Val = "something"
                }
        }

        for _, v := range n.Attr {
                fmt.Printf("%#v\n", v)
        }
}

Playground

Output:

main.Attribute{Key:"foo", Val:""}
main.Attribute{Key:"href", Val:"something"}
main.Attribute{Key:"bar", Val:""}

I thought it was obvious but I don't want to change the structures I get (they're from the go.net/html package)
@dystroy: The second approach above doesn't change the types (the "structures") wrt the OP.
Yes, I know, but it's not really bringing anything. I was expecting an idea that I could possibly have missed. I you feel confident that there is no simpler solution then that would be the answer.
@dystroy: It does bring something, it doesn't copy here and back the whole Attribute. And yes, I'm confident that taking the address of a slice element to avoid a double copy (r+w) update of the element is the optimal solution.