how do i define a struct where the keys are ascending integers without a defined limit?

huangapple go评论94阅读模式
英文:

how do i define a struct where the keys are ascending integers without a defined limit?

问题

这是我之前遇到的一个问题,我不确定如何干净地解决它。

我有一个看起来像这样的json_data:

{
"data": [{
"0": {
"first_name": "hello",
"last_name": "world"
},
"1": {
"first_name": "hello",
"last_name": "world"
}
}]
}

问题是,这个列表中的项目数量是不确定的。键总是连续的,而且顺序很重要,这就是为什么它们存在的原因。

我该如何将其表示为一个结构体?在Typescript中,我可以将其类型定义为[key: str]: {...},但我不知道如何在Go中实现。

英文:

this is an issue I have run into before and i am not sure how to solve this cleanly.

i have json_data that looks like this:

{
	"data": [{
		"0": {
			"first_name": "hello",
			"last_name": "world"
		},
		"1": {
			"first_name": "hello",
			"last_name": "world"
		}
	}]
}

the thing is, the amount of items in this list is undefined. The keys are always uninterrupted and the order matters which is why they are there.

how would i represent this as a struct? in Typescript i mihght type it as [key: str]: {...} but I dont know how to do it in go.

答案1

得分: 2

这是一个常见的问题,对于在使用解释型语言(如TypeScript或Python)之后学习Go的人来说,这是一个容易出错的地方,所以最好尽早解决。在这些其他语言中,用户定义的对象由相同的底层类型表示,其中包含哈希映射,用于动态关联属性和方法名与底层值和函数。因此,属性可以在任何时候添加到对象中,因为它实际上只是一个动态集合。

在Go中,结构体字段是静态的。字段在编译时在源代码中定义,并且应该通过名称使用点运算符访问。字段名称是符号,而不是变量

在Go中,你永远不会像动态定义的键/值对集合一样处理结构体。只有当代码中有一组明确定义的键可以合理地转换为符号时,结构体才有意义。

你的JSON数据结构看起来有点奇怪(如果键是递增整数,那只是一个列表),但假设这不是你可以控制的,我建议你建模如下:

type name struct {
	First_name, Last_name string
}
type fulldata struct {
	Data []map[string]name
}

这里有一个工作示例:https://go.dev/play/p/gBtrRoceeTL

英文:

> how would i represent this as a struct? in Typescript ...

This is a common stumbling block for those learning go after exeperience with interpreted languages such as typescript or python, so a good thing to get ironed out right away.

In those other languages, user defined objects are represented by the same underlying type containing hash maps to dynamically associate attribute and method names with underlying values and functions. Thus an attribute can be added to an object at any time as it is really just a dynamic collection anyway.

In Go, struct fields are static. The fields are defined within the source code at compile time, and should be accessed with the dot operator by name. Field names are symbols, not variables.

In Go, you never treat structs like dynamically defined sets of key/value pairs. Structs only make sense when there is a well defined set of keys that can reasonably be turned into symbols in the code.

Your data structure in json is a little silly looking (if the keys are ascending integers, that's just a list) but assuming that's not up to you, I'd suggest you model something like this.

type name struct {
	First_name, Last_name string
}
type fulldata struct {
	Data []map[string]name
}

Working example here: https://go.dev/play/p/gBtrRoceeTL

huangapple
  • 本文由 发表于 2023年5月9日 20:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76209141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定