-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.go
63 lines (48 loc) · 1.63 KB
/
arrays.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
package main
import (
"fmt"
)
func main() {
var arr1 = [3]int{1, 2, 3} // With var
arr2 := []int{4, 5, 6, 7, 8} // Using := and without using length
fmt.Println(arr1)
fmt.Println(arr2, "\n")
// String Arrays
var cars = [4]string{"Volvo", "BMW", "Ford", "Mazda"}
fmt.Print(cars, "\n")
// Accessing and changing elements of Arrays
prices := [3]int{10, 20, 30}
prices[2] = 50
fmt.Println(prices[2])
// Initialize only specific elements
arr5 := [5]int{1: 10, 2: 40}
fmt.Println(arr5)
// Slicing
arr10 := [6]int{10, 11, 12, 13, 14, 15}
myslice := arr10[2:4]
fmt.Printf("myslice = %v\n", myslice)
fmt.Printf("length = %d\n", len(myslice))
fmt.Printf("capacity = %d\n", cap(myslice))
// Appending elements to the end of the slice
myslice1 := []int{1, 2, 3, 4, 5, 6}
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 = append(myslice1, 20, 21)
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
// Creating a copy for memory efficency
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
// Original slice
fmt.Printf("numbers = %v\n", numbers)
fmt.Printf("length = %d\n", len(numbers))
fmt.Printf("capacity = %d\n", cap(numbers))
// Create copy with only needed numbers
neededNumbers := numbers[:len(numbers)-10]
numbersCopy := make([]int, len(neededNumbers))
copy(numbersCopy, neededNumbers)
fmt.Printf("numbersCopy = %v\n", numbersCopy)
fmt.Printf("length = %d\n", len(numbersCopy))
fmt.Printf("capacity = %d\n", cap(numbersCopy))
}