Golang Pitfall

Go does not have references.

Go does not have references.

In C++11 one might do the following:

struct Foo {
    int Value;
};

void f() {
    std::vector<Foo> foovec;
    //....
    for (auto& foo : foovec) {
        if (foo.Value > 10) {
            foo.Value = 10;
        }
    }
    //....
}

However, in Go, the following contains a bug:

type Foo struct {
    Value int
}   

func f() {
    var fooslice []Foo
    //....
    for _, foo := range fooslice {
        if foo.Value > 10 {
            foo.Value = 10
        }
    }
}

What I wanted to do was the following:

    for idx, foo := range fooslice {
        if foo.Value > 10 {
            foo.Value = 10
            fooslice[idx] = foo
        }
    }