'Is it equivalent use "copy" or "append" slice type in golang
a := []byte{1, 2, 3}
// Method: 1
b := make([]byte, len(a))
copy(b, a)
// Method: 2
c := append([]byte(nil), a...)
Q: Is the method 2 more concise and efficient than method 1?
Q: Whether mode 2 and mode 1 are equivalent, both are deep copy?
thank you for your help
Solution 1:[1]
Method 1 is more precise - as it allocates exactly the slice size it needs & fills it.
Method 2's append will allocate a slice of capacity (depending probably on your architecture) in units of 8. So those 3 initial items will be copied to a backing array of size 8:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
