Tsallaka zuwa abun ciki
Justice Tech School

copy and Sharing

Slices share memory. copy makes an independent copy.

Example
package main

import "fmt"

func main() {
	a := []int{1, 2, 3}
	b := a
	b[0] = 99
	c := make([]int, len(a))
	copy(c, a)
	c[1] = 7
	fmt.Println(a, b, c)
}

Result

[99 2 3] [99 2 3] [99 7 3]

Exercise

Copy slices.

(dst, src)

Quiz Test

Do slices share memory?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.