Golang how to initialise an array struct

Issue

I got a struct like this:

type Eoor []struct {
    Name string
}

I can initialise an empty struct by: &Eoor{}
But I want to initialise some values for it, but how?
Thanks

Solution

Since the struct is a slice, Eoor{} gives the slice, and you have to initialize individual slice elements:

x := Eoor{{Name: "a"},{Name:"b"}}

Answered By – Burak Serdar

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.