2 回答
TA贡献1841条经验 获得超3个赞
像下面这样
package main
import "fmt"
func main() {
type NeoCoverage struct {
Name string
Number string
}
type Coverage struct {
neoCoverage []NeoCoverage
}
coverage := Coverage {
neoCoverage: []NeoCoverage {
NeoCoverage{ Name: "xyz", Number: "xyz123" },
NeoCoverage{ Name: "abc", Number: "abc123" },
},
}
fmt.Println(coverage)
}
输出
{[{xyz xyz123} {abc abc123}]}
这是简要说明;neoCoverage: []NeoCoverage{}代表切片,NeoCoverage该切片应填充NeoCoverage{}元素,如NeoCoverage{ Name: "xyz", Number: "xyz123" }, ...
TA贡献1811条经验 获得超4个赞
您还可以省略数组中多余的 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}]}
- 2 回答
- 0 关注
- 399 浏览
添加回答
举报