Reputation: 79
What is the main difference between these structs?
type foo struct {
Name string `json:"name"`
}
and
type foo []struct {
Name string `json:"name"`
}
Upvotes: 0
Views: 244
Reputation: 40321
The first is a struct type which is named. It can be initialized in the usual way:
// Initialize a foo
f1 := foo{"name1"}
// Initialize a foo slice
f1s := []foo {
foo{"name2"},
foo{"name3"},
}
The second is a named slice type whose elements are anonymous structs with the given fields. It can be initialized like this:
f2 := foo {
{"name4"},
{"name5"},
}
Upvotes: 0
Reputation: 2093
type foo1 struct {
Name string `json:"name"`
}
type foo2 []struct {
Name string `json:"name"`
}
Simple understand as
type foo2 []foo1
Upvotes: 1