我知道我们可以或多或少地执行以下操作:
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}}}
但是我想知道您是否可以为不需要在特定地方使用的数据进行匿名处理。我想避免编写多余的代码,我正在寻找这样的东西:
var s = struct {
someString string, someStructs[] struct {
x string, y int
}
} {
"ok", []{
{1, 2}, {3, 4}, {5, 6}
}
}
答案 0 :(得分:1)
是的,您可以这样做,但是如果您嵌套了匿名结构,那会有些乏味:
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}
}
}