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

后端 未结 4 2019
心在旅途
心在旅途 2021-01-04 04:19

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

4条回答
  •  无人及你
    2021-01-04 04:48

    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
    }
    

提交回复
热议问题