-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
69 lines (48 loc) · 1.26 KB
/
slice.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
)
func main() {
names := [...]string{"didi", "lesmana", "budi", "joko", "eko", "kurniawan"}
slices1 := names[4:6]
fmt.Print(slices1)
slices2 := names[:3]
fmt.Println(slices2)
slices3 := names[3:]
fmt.Println(slices3)
slices4 := names[:]
fmt.Println(slices4)
days := [...]string{"senin", "selasa", "rabu", "kamis", "jumat", "sabtu", "minggu"}
daySlice1 := days[5:]
fmt.Println(daySlice1)
daySlice1[0] = "sabtu baru"
daySlice1[1] = "minggu baru"
fmt.Println(daySlice1)
fmt.Println(days)
daySlice2 := append(daySlice1, "sabtu lama")
fmt.Println(daySlice1)
fmt.Print(daySlice2)
fmt.Print(days)
var newSlice []string = make([]string, 2, 5)
newSlice[0] = "didi"
newSlice[1] = "didi"
fmt.Println(newSlice)
fmt.Println(len(newSlice))
fmt.Println(cap(newSlice))
newSlice2 := append(newSlice, "lesmnana")
fmt.Println(newSlice2)
fmt.Println(len(newSlice2))
fmt.Println(cap(newSlice2))
newSlice2[0] = "budi"
fmt.Println(newSlice2)
fmt.Print(newSlice)
fromSlice := days[:]
toSlice := make([]string, len(fromSlice), cap(fromSlice))
copy(toSlice, fromSlice)
fmt.Println(fromSlice)
fmt.Println(toSlice)
iniArray := [...]int{1, 2, 3, 4, 5}
iniSlice := []int{1, 2, 3, 4, 5}
fmt.Println(iniArray)
fmt.Println(iniSlice)
}