Skip to content

Instantly share code, notes, and snippets.

@hyper0x
Last active August 29, 2015 14:21
Show Gist options
  • Save hyper0x/e334df027683d1c414c6 to your computer and use it in GitHub Desktop.
Save hyper0x/e334df027683d1c414c6 to your computer and use it in GitHub Desktop.
No deep copy
package main
import "fmt"
type S1 struct {
SS []int
N int
}
func main() {
a1 := []int{1, 2}
b1 := []int{3, 4}
copy(b1, a1)
a1[1] = 20
fmt.Printf("%v, %v\n", b1, a1)
a2 := []S1{S1{[]int{1, 2, 3}, 1}, S1{[]int{4, 5, 6}, 2}}
b2 := make([]S1, 2)
copy(b2, a2)
a2[0].SS[1] = 20
fmt.Printf("%v, %v\n", b2, a2)
a3 := [2]int{1, 2}
ch1 := make(chan [2]int, 1)
ch1 <- a3
b3 := <-ch1
a3[1] = 20
fmt.Printf("%v, %v\n", b3, a3)
a4 := []int{1, 2}
ch2 := make(chan []int, 1)
ch2 <- a4
b4 := <-ch2
a4[1] = 20
fmt.Printf("%v, %v\n", b4, a4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment