简体   繁体   中英

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]

Given a definition of a Struct

type State struct{
    name string
    population string
}

You have several ways. You can declare an array of 5 State s

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM