Is there a built in function in go for making copies of arbitrary maps?

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

Is there a built in function in go for making copies of arbitrary maps?

问题

在Go语言中,没有内置的函数用于复制任意映射(maps)。你可以手动编写一个复制函数,但是我之前在查找类似问题时发现,当我想要进行深度比较映射时,已经有一个内置函数可以实现!所以,类似地,我想知道是否有一个内置的函数、库或包可以在Go语言中进行映射的深度复制。我相信我不是第一个想要复制映射的人。

通过复制,我指的是可以创建两个不同的变量,它们在内容上相同但在内存中引用不同的映射。

英文:

Is there a built in function in go for making copies of arbitrary maps?

I would be able to write one by hand but I found out earlier I was looking a similar question when I wanted to make a deep comparison of maps and there seemed to be a function already built in for that! So similarly, maybe I was wondering if there was an built in or some library or package for making deep copies of maps in golang. I am sure I am not the first person to want to make copies of maps in go.

By copy I mean you can create two different variables that reference a different map in memory even though they are the same content wise.

答案1

得分: 8

更一般的方法是,你可以使用encoding/gob对地图进行编码和解码,将其存储在一个新的变量中。

这种方法的优点是,它甚至可以处理更复杂的数据结构,比如包含地图切片的结构切片。

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/gob"
  5. "fmt"
  6. "log"
  7. )
  8. func main() {
  9. ori := map[string]int{
  10. "key": 3,
  11. "clef": 5,
  12. }
  13. var mod bytes.Buffer
  14. enc := gob.NewEncoder(&mod)
  15. dec := gob.NewDecoder(&mod)
  16. fmt.Println("ori:", ori) // key:3 clef:5
  17. err := enc.Encode(ori)
  18. if err != nil {
  19. log.Fatal("encode error:", err)
  20. }
  21. var cpy map[string]int
  22. err = dec.Decode(&cpy)
  23. if err != nil {
  24. log.Fatal("decode error:", err)
  25. }
  26. fmt.Println("cpy:", cpy) // key:3 clef:5
  27. cpy["key"] = 2
  28. fmt.Println("cpy:", cpy) // key:2 clef:5
  29. fmt.Println("ori:", ori) // key:3 clef:5
  30. }

如果你想了解更多关于gob的信息,可以参考Go博客文章

英文:

For a more general answer, you can encode your map and decode it in a new variable with encoding/gob.

The advantages of this way is that it'll even work on more complex data structure, like a slice of struct containing a slice of maps.

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/gob"
  5. "fmt"
  6. "log"
  7. )
  8. func main() {
  9. ori := map[string]int{
  10. "key": 3,
  11. "clef": 5,
  12. }
  13. var mod bytes.Buffer
  14. enc := gob.NewEncoder(&mod)
  15. dec := gob.NewDecoder(&mod)
  16. fmt.Println("ori:", ori) // key:3 clef:5
  17. err := enc.Encode(ori)
  18. if err != nil {
  19. log.Fatal("encode error:", err)
  20. }
  21. var cpy map[string]int
  22. err = dec.Decode(&cpy)
  23. if err != nil {
  24. log.Fatal("decode error:", err)
  25. }
  26. fmt.Println("cpy:", cpy) // key:3 clef:5
  27. cpy["key"] = 2
  28. fmt.Println("cpy:", cpy) // key:2 clef:5
  29. fmt.Println("ori:", ori) // key:3 clef:5
  30. }

If you want to know more about gobs, there is a go blog post about it.

答案2

得分: 2

不,没有内置的一行代码来进行深拷贝映射。

然而,有一种迭代解决方案,以及一个用于深拷贝的通用包

英文:

No, there is no built-in one-liner for map deep copy.

However, there is an iterative solution as well as a generic package for deep copy.

答案3

得分: -1

复制一个映射只需要几行简单的代码。只需编写代码,放下每个简单代码都必须在库中的想法,然后享受它!

  1. original := map[string]int{
  2. "Hello": 4,
  3. "World": 123,
  4. }
  5. copy := map[string]int{}
  6. for k, v := range original {
  7. copy[k] = v
  8. }
英文:

Copying a map is a few, trivial lines of code. Just write the code and enjoy letting go of the idea that every simple piece of code has to be in a library!

  1. original := map[string]int{
  2. "Hello": 4,
  3. "World": 123,
  4. }
  5. copy := map[string]int{}
  6. for k, v := range original {
  7. copy[k] = v
  8. }

huangapple
  • 本文由 发表于 2014年4月13日 00:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/23033143.html
匿名

发表评论

匿名网友

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

确定