我有结构像

type Coverage struct {
  neoCoverage        []NeoCoverage
  ApocCoverage       []ApocCoverage
  ApocConfigCoverage []ApocConfigCoverage
}


type NeoCoverage struct {
  Name   string
  Number string
}

那么我应该如何填充覆盖结构?

在这里,我是如何尝试的。
coverage = Coverage{
 []neoCoverage: NeoCoverage{
     Name:   "xyz",
     Number: "xyz123",
   },
 }

最佳答案

像下面这样

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 的 slice ,该 slice 应该像 NeoCoverage{} 一样填充 NeoCoverage{ Name: "xyz", Number: "xyz123" }, ... 元素

关于go - 在golang中填充结构数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28001040/

10-11 19:45