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

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

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对地图进行编码和解码,将其存储在一个新的变量中。

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

package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"log"
)

func main() {
	ori := map[string]int{
		"key":  3,
		"clef": 5,
	}

	var mod bytes.Buffer
	enc := gob.NewEncoder(&mod)
	dec := gob.NewDecoder(&mod)

	fmt.Println("ori:", ori) // key:3 clef:5
	err := enc.Encode(ori)
	if err != nil {
		log.Fatal("encode error:", err)
	}

	var cpy map[string]int
	err = dec.Decode(&cpy)
	if err != nil {
		log.Fatal("decode error:", err)
	}

	fmt.Println("cpy:", cpy) // key:3 clef:5
	cpy["key"] = 2
	fmt.Println("cpy:", cpy) // key:2 clef:5
	fmt.Println("ori:", ori) // key:3 clef:5
}

如果你想了解更多关于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.

package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"log"
)

func main() {
	ori := map[string]int{
		"key":  3,
		"clef": 5,
	}

	var mod bytes.Buffer
	enc := gob.NewEncoder(&mod)
	dec := gob.NewDecoder(&mod)

	fmt.Println("ori:", ori) // key:3 clef:5
	err := enc.Encode(ori)
	if err != nil {
		log.Fatal("encode error:", err)
	}

	var cpy map[string]int
	err = dec.Decode(&cpy)
	if err != nil {
		log.Fatal("decode error:", err)
	}

	fmt.Println("cpy:", cpy) // key:3 clef:5
	cpy["key"] = 2
	fmt.Println("cpy:", cpy) // key:2 clef:5
	fmt.Println("ori:", ori) // key:3 clef:5
}

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

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

original := map[string]int{
    "Hello": 4,
    "World": 123,
}

copy := map[string]int{}
for k, v := range original {
    copy[k] = v
}
英文:

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!

original := map[string]int{
    "Hello": 4,
    "World": 123,
}

copy := map[string]int{}
for k, v := range original {
    copy[k] = v
}

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:

确定