3

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]
1
  • The number of elements in an array goes before, not after, the element type. So that would be type State [5]struct{name string; population string}. Commented May 16, 2016 at 11:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.