Answer the question
In order to leave comments, you need to log in
How to change two dimensional array in for?
I'm trying to change a two-dimensional array - add a value. But since everything is passed by value, naturally I can’t do this. Tried to write something like this:
for _, *innerArray := range &arr {
innerArray = append(innerArray, "ddddd")
}
package main
import "fmt"
func main() {
var arr = [][]string{{"el1"}, {"el1", "el2"}, {}}
for _, innerArray := range arr {
fmt.Printf("len: %d\n", len(innerArray))
}
fmt.Println("----------")
for _, innerArray := range arr {
innerArray = append(innerArray, "ddddd")
}
for _, innerArray := range arr {
fmt.Printf("len: %d\n", len(innerArray))
}
}
Answer the question
In order to leave comments, you need to log in
....
for i, _ := range arr {
arr[i] = append(arr[i], "ddddd")
}
....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question