package main
import "fmt"
func main(){
a:=[]int{1,2,3,4,5} //slice of int
b:=[]struct{ //another slice of struct
i int
j string
}{
{1,"精"},
{2 ,"コバや歌詞"},
{3,"新一"},
{4,"武士"},
}
c:=[]struct{ //slice of slices
d []struct
e []int
}{
{a[:],b[:]},
}
fmt.Println(a,b,c)
}
The errors are:-
- ./slices.go:16:3: syntax error: unexpected e, expecting {
- ./slices.go:17:3: syntax error: unexpected {, expecting semicolon or newline or }
- ./slices.go:18:7: syntax error: unexpected ] at end of statement
You are creating c struct wrong. It should contain slice of int for first field and slice of struct of b type as second field.
c:=[]struct{ //slice of slices
d []int
e []struct{ //another slice of struct
i int
j string
}
}{
{a[:],b[:]},
}
Below code is working
package main
import "fmt"
func main(){
a:=[]int{1,2,3,4,5} //slice of int
b:=[]struct{ //another slice of struct
i int
j string
}{
{1,"精"},
{2 ,"コバや歌詞"},
{3,"新一"},
{4,"武士"},
}
c:=[]struct{ //slice of slices
d []int
e []struct{ //another slice of struct
i int
j string
}
}{
{a[:],b[:]},
}
fmt.Println(a,b,c)
}
In case you wonder how to improve on your code:
package main
import "fmt"
type someType struct { // struct
i int
j string
}
type composite struct { // struct with slices
e []int
d []someType
}
func main() {
a := []int{1, 2, 3, 4, 5} //slice of int
b := []someType{ //another slice of struct
{1, "精"},
{2, "コバや歌詞"},
{3, "新一"},
{4, "武士"},
}
c := []composite{ //slice of struct with slices
{a, b},
}
fmt.Println(a, b, c)
}
It looks a lot cleaner and while instantiating it is a a lot easier to keep an the overview.
Now it is still readable if you instantiate c with all content:
c := []composite{
{
e: []int{1, 2, 3, 4, 5},
d: []someType{
{1, "精"},
{2, "コバや歌詞"},
{3, "新一"},
{4, "武士"},
},
},
}