I was constrained to define a particular type for []string
because I implemented a custom yaml parser for the strSlice type.
Now I need to cast []strSlice
back into [][]string
but the go compiler 1.7.1 reject it as an error.
type strSlice []string
var x1 []strSlice
var x2 [][]string
...
x2 = [][]string(x1)
How can I perform the cast operation ?
You're using a named type of a string slice. You need to convert each entry in x1
back to a []string
first:
type strSlice []string
var x1 []strSlice
var x2 [][]string
...
for _, s := range x1 {
x2 = append(x2, []string(s))
}
https://play.golang.org/p/5iJT2Hsv1R
Unfortunately, there's no way to do this in one shot, because each of the indexes in x1
are a strSlice
type, and need to be converted to a []string
type to be stored in x2
. Go doesn't let you do this in a single operation because the developers didn't want to hide O(n) operations in syntactical sugar.