Range references instead values
The short & direct answer: no, use the array index instead of the value
So the above code becomes:
package main
import "fmt"
type MyType struct {
field string
}
func main() {
var array [10]MyType
for idx, _ := range array {
array[idx].field = "foo"
}
for _, e := range array {
fmt.Println(e.field)
fmt.Println("--")
}
}
To combine @Dave C and @Sam Toliman's comments
package main
import "fmt"
type MyType struct {
field string
}
func main() {
var array [10]MyType
for idx := range array {
e := &array[idx]
e.field = "foo"
}
for _, e := range array {
fmt.Println(e.field)
fmt.Println("--")
}
}
https://play.golang.org/p/knKfziB1nce