Reputation: 950
I know that we can do something more or less that looks like this:
type Struct1 struct {
someString string
someInt int
}
type Struct2 struct {
someString string
someStruct []Struct1
}
var s Struct2 = Struct2{"abc", []{Struct1{"def", 123}, Struct1{"ghi", 456}}}
But I would like to know if you can do something anonymous for data I won't need anywhere else than in a specific place. I would like to avoid writing redundant code, I am looking for something like this:
var s = struct {
someString string, someStructs[] struct {
x string, y int
}
} {
"ok", []{
{1, 2}, {3, 4}, {5, 6}
}
}
Upvotes: 0
Views: 995
Reputation: 51497
Yes, you can do that, but it is somewhat tedious if you have nested anonymous structs:
var s = struct {
someString string,
someStructs[] struct {
x string
y int
}
} {
someString: "ok",
someStructs: []struct{ x string, y int } {
{"1", 2}, {"3", 4}, {"5", 6}
}
}
Upvotes: 1