Anchal Sarraf
Anchal Sarraf

Reputation: 3909

How to create an array of struct in golang as we create in C

I want to create an array of struct in golang as we create in C. I am trying to create like this, but is not working.

type State struct{
    name string
    population string
}st[5]

Upvotes: 3

Views: 2609

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176342

Given a definition of a Struct

type State struct{
    name string
    population string
}

You have several ways. You can declare an array of 5 States

var states [5]State

Or you can assign (and autodeclare) in one line

var states = [5]State{}

or

states := State{}

You may want to start from the Go documentation.

Upvotes: 2

Related Questions