doujiao3016 2015-01-17 15:33
浏览 195
已采纳

在Golang中填充结构数组

I have struct like

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

And

type NeoCoverage struct {
  Name   string
  Number string
}

So how should i fill coverage struct?

Here how I am Trying.

coverage = Coverage{
 []neoCoverage: NeoCoverage{
     Name:   "xyz",
     Number: "xyz123",
   },
 }
  • 写回答

2条回答 默认 最新

  • duanluan8390 2015-01-17 16:07
    关注

    Something like below

    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)
    }
    

    Output

    {[{xyz xyz123} {abc abc123}]}
    

    Here is the brief explanation; neoCoverage: []NeoCoverage{} stands for the slice of NeoCoverage and that slice should be populated with NeoCoverage{} elements as in NeoCoverage{ Name: "xyz", Number: "xyz123" }, ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?