我想在 golang 中创建一个 struct
数组,就像我们在 C 中创建的那样。我正在尝试这样创建,但没有用。
type State struct{
name string
population string
}st[5]
最佳答案
给定一个Struct
的定义
type State struct{
name string
population string
}
你有几种方法。您可以声明一个包含 5 个 State
var states [5]State
或者您可以在一行中分配(和自动声明)
var states = [5]State{}
或
states := State{}
您可能想从 Go documentation 开始.
关于arrays - 如何像我们在 C 中创建的那样在 golang 中创建一个结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37252565/