-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
45 lines (33 loc) · 1.4 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
package main;
import "fmt";
func main(){
slice1 :=[]int{1,2,3,4,5}
fmt.Println(slice1)
fmt.Println(cap(slice1))
fmt.Println(len(slice1))
// ========================== Create a Slice From an Array =========================
var myArray=[6]int{1,2,3,4,5,6}
mySlice :=myArray[1:4];
fmt.Printf("myslice = %v\n", mySlice)
fmt.Printf("length = %d\n", len(mySlice))
fmt.Printf("capacity = %d\n", cap(mySlice))
// =========================== Create a Slice With The make() Function ==============
// make(type,length, capacity)
// if capacity is not given then its value will be equal to the length
myslice1 := make([]bool, 5, 5)
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
// =========================== To access the value of slice =========================
fmt.Println(myslice1[0])
// =========================== To change the value of slice =========================
myslice1[0]=true;
fmt.Println(myslice1)
// =========================== Append element to the slice ==========================
fmt.Println("New---------------------------")
myslice1=append(myslice1,false,true)
fmt.Println(myslice1,len(myslice1),cap(myslice1))
// =========================== REMOVE ELEMENT FROM THE SLICE =========================
myslice1=append(myslice1[:2],myslice1[2+1:]...);
fmt.Println(myslice1,len(myslice1),cap(myslice1))
}