我有像
这样的结构type Coverage struct {
neoCoverage []NeoCoverage
ApocCoverage []ApocCoverage
ApocConfigCoverage []ApocConfigCoverage
}
和
type NeoCoverage struct {
Name string
Number string
}
那么我应该如何填写coverage结构?
这是我的尝试方式。
coverage = Coverage{
[]neoCoverage: NeoCoverage{
Name: "xyz",
Number: "xyz123",
},
}
答案 0 :(得分:3)
您还可以省略数组中的冗余Type声明。以Alper的代码库为例。
package main
import "fmt"
func main() {
type NeoCoverage struct {
Name string
Number string
}
type Coverage struct {
neoCoverage []NeoCoverage
}
coverage := Coverage {
neoCoverage: []NeoCoverage {
{ Name: "xyz", Number: "xyz123" },
{ Name: "abc", Number: "abc123" },
},
}
fmt.Println(coverage)
}
输出:
{[{xyz xyz123} {abc abc123}]}