14
type A struct {
B struct {
Some string
Len int
}
}
簡單的問題。如何初始化這個結構?我願做這樣的事情:複合文字中缺失的類型
a := &A{B:{Some: "xxx", Len: 3}}
預期地我發現了一個錯誤:
missing type in composite literal
當然,我可以創造一個分開的結構B和初始化這樣說:
type Btype struct {
Some string
Len int
}
type A struct {
B Btype
}
a := &A{B:Btype{Some: "xxx", Len: 3}}
但它不如第一種方式有用。有初始化匿名結構的捷徑嗎?