简体   繁体   English

如何在我们用C创建的golang中创建一个struct数组

[英]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. 我想在golang中创建一个struct数组,就像我们在C中创建的那样。我试图像这样创建,但是没有用。

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

Given a definition of a Struct 给出Struct的定义

type State struct{
    name string
    population string
}

You have several ways. 你有几种方法。 You can declare an array of 5 State s 您可以声明一个包含5个State的数组

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 . 您可能想从Go文档开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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