Golang编程语言知识介绍


  • 首页

  • 随想录

  • 生活

  • OS

  • lua

  • redis

  • Golang

  • C

  • OpenVPN

  • web

  • OpenWRT

  • 运维

  • Git

  • 鸟哥的私房菜

  • IT杂谈

  • About Me

  • 友情链接

  • 搜索
close

Copy a slice in Go

时间: 2022-09-29   |   分类: go     |   阅读: 384 字 ~1分钟

To duplicate a slice in Go, getting a deep copy of its contents, you need to either use the built-in copy() function, or create a new empty slice and add all the elements of the first slice to it using the append() function. Because of how slices are built in Go, assigning one slice to another only makes a shallow copy, and you should not use it if you want to clone the slice in a deep way.

Copy a slice using the copy() function

package main

import "fmt"

func main() {
    src := []string{"a", "b", "c"}
    dst := make([]string, len(src))

    copy(dst, src)

    fmt.Printf("source slice: %[1]v, address: %[1]p\n", src)
    fmt.Printf("source slice: %[1]v, address: %[1]p\n", dst)
}

Output:

Copy

source slice: [a b c], address: 0xc000098180
source slice: [a b c], address: 0xc0000981b0

As you can see, we got two different addresses of underlying arrays for the src and dst slices, which is evidence that we deeply cloned the src slice. The copy() function copies min(len(dst), len(src)) elements, so we need to create the dst slice of the same size as the src using make([]string, len(src)).

Copy a slice using the append() function

package main

import "fmt"

func main() {
    src := []string{"a", "b", "c"}
    var dst []string

    dst = append(dst, src...)

    fmt.Printf("source slice: %[1]v, address: %[1]p\n", src)
    fmt.Printf("source slice: %[1]v, address: %[1]p\n", dst)
}

Output:

source slice: [a b c], address: 0xc000098180
source slice: [a b c], address: 0xc0000981b0

Copying a slice using the append() function is really simple. You just need to define a new empty slice, and use the append() to add all elements of the src to the dst slice. In that way, you get a new slice with all the elements duplicated.

Shallow copy by assignment

package main

import "fmt"

func main() {
    src := []string{"a", "b", "c"}
    dst := src

    fmt.Printf("source slice: %[1]v, address: %[1]p\n", src)
    fmt.Printf("source slice: %[1]v, address: %[1]p\n", dst)
}

Output:

source slice: [a b c], address: 0xc000098180
source slice: [a b c], address: 0xc000098180

If you just assign the src slice to the new dst variable, you get a shallow copy that has the same underlying array. When you modify the contents of this copy, the original slice will also change.

以上内容转载自网友的blog

#go#
Compare And Swap(CAS)原理分析
golang 中 channel 的详细使用、使用注意事项及死锁分析
shankusu2017@gmail.com

shankusu2017@gmail.com

501 日志
34 分类
39 标签
GitHub
© 2009 - 2023
粤ICP备2021068940号-1 粤公网安备44011302003059
0%