I have this code, which is giving me errors:
package main
import (
"fmt"
)
func main() {
var cnt = make([][]string,0,10)
for i := 0; i < 5; i++ {
var tmp = make([]string,0,8)
for c := 0 ; c < 5 ; c++ {
tmp = append(tmp,"Matias")
}
cnt= append(cnt,tmp...)
}
fmt.Println(cnt)
}
It's giving me an error. Basically what I need is to have the slice to be as dinámic as possible. I don't know what the final length will be in any of the two dimensions.
The compiler error is actually misleading - it should quote you are using tmp...
which is of a variadic of strings - instead it quotes tmp
which is of the correct type []string
which one could use to append to cnt
:
main.go:14:15:cannot use tmp (type []string) as type [][]string in append
Anyway, using tmp...
, go is turning tmp
from a []string into individual string parameters. Effectively:
cnt = append(cnt, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4])
And go can't append string
to a [][]string
type.
Change the line to:
cnt = append(cnt, tmp)