gpt4 book ai didi

arrays - 如何像我们在 C 中创建的那样在 golang 中创建一个结构数组

转载 作者:IT王子 更新时间:2023-10-29 01:09:58 24 4
gpt4 key购买 nike

我想在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com