Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling struct array in golang

Tags:

go

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",
   },
 }
like image 592
Akash Shinde Avatar asked Dec 19 '22 07:12

Akash Shinde


1 Answers

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" }, ...

like image 95
Alper Avatar answered Jan 28 '23 14:01

Alper