-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharrays.go
More file actions
52 lines (40 loc) · 922 Bytes
/
arrays.go
File metadata and controls
52 lines (40 loc) · 922 Bytes
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
package main
import (
"fmt"
"sort"
)
//searching element
//go doesnt provide any package for searching
func indexOf(data []int, ele int) int {
for index, value := range data {
if ele == value {
return index
}
}
return -1
}
func main() {
//making dynamic array
//initialize dynamic arr
arr := []int{}
arr = append(arr, 2, 3, 1, 100, 5)
fmt.Println(arr)
//print a slice of array
fmt.Println(arr[0:2])
//making a fixed size array of size 4 with all initialized by 0
new_arr := make([]int, 4)
new_arr = append(new_arr, 5)
fmt.Println(new_arr)
//arr operations ->
//get an element at a particular location
fmt.Println(arr[2])
// sort an array
sort.Ints(arr)
fmt.Println(arr)
//remove an element
//by index
fmt.Println(append(arr[:1], arr[2:len(arr)-1]...))
//by value
fmt.Println(indexOf(arr, 3))
fmt.Println(append(arr[:indexOf(arr, 3)], arr[indexOf(arr, 3)+1:len(arr)-1]...))
}