-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_09_2018CopyFunctionExample.go
50 lines (43 loc) · 1.77 KB
/
11_09_2018CopyFunctionExample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import "fmt"
func main() {
srcArray := []int{10, 20, 30, 40, 50, 60, 70, 80, 90} //src array is larger than dst array
dstArray := []int{100, 200, 300, 400}
fmt.Println(srcArray)
fmt.Println(dstArray)
var total int = copy(dstArray, srcArray) //Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).
fmt.Println(total)
fmt.Println(srcArray)
fmt.Println(dstArray)
srcArray = []int{10, 20} //src array is smaller than dst array
dstArray = []int{100, 200, 300, 400, 500}
fmt.Println(srcArray)
fmt.Println(dstArray)
total = copy(dstArray, srcArray) //Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).
fmt.Println(total)
fmt.Println(srcArray)
fmt.Println(dstArray)
str := "Tathagat Ajay Khanorkar" // it will also copy bytes from a string to a slice of bytes.
fmt.Println(str)
byteArray := make([]byte, 10)
total = copy(byteArray, str) //Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).
fmt.Printf("%s \n", byteArray)
fmt.Println(byteArray)
fmt.Println(str)
fmt.Println(total)
srcArray = []int{10,20,30,40,50,60}//The source and destination may overlap.
fmt.Println(srcArray)
total = copy(srcArray,srcArray[:])//copy() works correctly even if the destination is a slice which shares the same underlying array as the source slice, and the part of the array designated by source and destination has common parts
fmt.Println(total)
fmt.Println(srcArray)
srcArray = []int{10,20,30,40,50,60}
fmt.Println(srcArray)
total = copy(srcArray,srcArray[1:])
fmt.Println(total)
fmt.Println(srcArray)
srcArray = []int{10,20,30,40,50,60}
fmt.Println(srcArray)
total = copy(srcArray,srcArray[1:6])
fmt.Println(total)
fmt.Println(srcArray)
}