将地图嵌入到Go语言的结构体中

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

Embedding a map into a struct in the go language

问题

在这个答案这里中提到,在Go语言中无法将地图嵌入到结构体中。然而,我尝试了一下,并且得到了下面的代码,它确实可以工作,并且非常简单明了。

package main

import (
	"fmt"
	"runtime"
)

type record struct {
	m map[string]int
}

func main() {
	practice := record{m: make(map[string]int)}
	practice.m["foo"] = 2
	fmt.Println(practice.m)
	runtime.GC()
}

这将打印出 map[foo:2]

然而,我的问题是,使用这种在结构体中实现地图的方法是否存在任何负面影响,或者是否有更高效的方法来实现这一点?

英文:

So in this answerhere, it's stated that one cannot embed a map into a struct in go. However, I was fiddling around with it, and came up with this and it does actually work, and is pretty straight forward.

    package main

import (
	"fmt"
	"runtime"
)

type record struct {
	m map[string]int
}




func main() {
	practice := record{m:make(map[string]int)}
	practice.m["foo"] = 2
	fmt.Println(practice.m)
	runtime.GC()
}

this prints map[foo:2]

However, my question is that, are there any negative sides to using this implementation of maps in structs, or are is there more efficient ways to do this?

答案1

得分: 3

  1. 你可以这样做,完全没问题。
  2. 这并不是“嵌入”。嵌入意味着在结构体中将一个无名字段作为命名类型的一部分。你的map并不是嵌入的,它只是一个名为“m”的普通成员。
  3. 你提供的答案有点误导性:对于那个问题的答案(“我能否在没有MarshalJSON方法的情况下展开这个JSON输出”)确实是否定的,但实际上并不是说在结构体中嵌入一个map是被禁止的。如果你创建一个命名类型,它是一个map类型,你完全可以将它嵌入到结构体中。只是它在JSON输出中的方式不符合提问者的期望。
英文:
  1. You can do that, it's absolutely fine.

  2. That isn't "embedding". Embedding means something specific — including a nameless field of a named type in a struct. Your map isn't embedded, it's a regular member with the name "m".

  3. The answer that you linked is slightly misleading: the answer to the question there ("can I flatten this JSON output without a MarshalJSON method") is indeed no, but it's not actually true that embedding a map in a struct is forbidden. If you create a named type that is a map type, you can embed it in a struct just fine. It just doesn't output in JSON the way that the person asking that question would have liked.

答案2

得分: 0

没有任何固有的缺点。在结构体中嵌入地图是一种常见的做法。唯一的问题可能是在实现之外,比如在切片更好的情况下使用地图。然而,在这里这并不相关。选择使用哪种集合类型(切片 vs 数组 vs 地图)是一个不同的讨论,更多地基于你的数据访问模式。一般来说,使用任何Go的集合类型(包括地图)来组合结构体是正常和惯用的。

编辑:从hobbs的回答中注意到问题和我的回答中对嵌入术语的误用,正如他指出的,地图并没有被"嵌入",那是一种特定的语言特性,实际上这只是组合,我在上面应该这样称呼它。

英文:

There aren't any inherent draw backs. Embedding maps in structs is a common practice. The only issues with the implementation would be outside of that, like using a map in favor of a slice when a slice is better. However, that isn't relevant here. Choosing which collection type to use (slice vs array vs map say) is a different discussion and is more based on your data access patterns. In general, it is normal and idiomatic to compose structs with any of Go's collection types (map being one of those).

EDIT: noticed from hobbs answer the misuse of the term embedded in both the question and my answer, as he points out, the map is not 'embedded' that is a specific language feature, this is actually just composition and I should have referred to it as such above.

huangapple
  • 本文由 发表于 2016年4月29日 07:08:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/36926953.html
匿名

发表评论

匿名网友

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

确定