-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays-slices.go
187 lines (143 loc) · 3.28 KB
/
arrays-slices.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Author: Resul Emre AYGAN
*/
package main
import (
"fmt"
"strings"
)
var powArray = []int{1, 2, 4, 8, 16, 32, 64, 128}
func main() {
//Arrays
var a [2]string
a[0] = "Hello"
a[1] = "World"
fmt.Println(a[0], a[1])
fmt.Println(a)
primes := [6]int{2, 3, 5, 7, 11, 13}
fmt.Println(primes)
//Slices
var s []int = primes[1:4]
fmt.Println(s)
// An array has a fixed size.
// A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array.
//Slices are like references to arrays
names := [4]string{
"John",
"Paul",
"George",
"Ringo",
}
fmt.Println(names)
aNew := names[0:2]
bNew := names[1:3]
fmt.Println(aNew, bNew)
bNew[0] = "XXX"
fmt.Println(aNew, bNew)
fmt.Println(names)
// Changing the elements of a slice modifies the corresponding elements of its underlying array.
//Slice literals
q := []int{2, 3, 5, 7, 11, 13}
fmt.Println(q)
r := []bool{true, false, true, true, false, true}
fmt.Println(r)
sNew := []struct {
i int
b bool
}{
{2, true},
{3, false},
{5, true},
{7, true},
{11, false},
{13, true},
}
fmt.Println(sNew)
//Slice defaults
k := []int{2, 3, 5, 7, 11, 13}
k = k[1:4]
fmt.Println(k)
k = k[:2]
fmt.Println(k)
k = k[1:]
fmt.Println(k)
//Slice length and capacity
l := []int{2, 3, 5, 7, 11, 13}
printSlice(l)
// Slice the slice to give it zero length.
l = l[:0]
printSlice(l)
// Extend its length.
l = l[:4]
printSlice(l)
// Drop its first two values.
l = l[2:]
printSlice(l)
//Nil slices
var h []int
fmt.Println(h, len(h), cap(h))
if h == nil {
fmt.Println("nil!")
}
//Creating a slice with make
m := make([]int, 5)
printSliceNew("m", m)
// Slices can be created with the built-in make function; this is how you create dynamically-sized arrays.
// To specify a capacity, pass a third argument to make
n := make([]int, 0, 5)
printSliceNew("n", n)
g := n[:2]
printSliceNew("g", g)
j := g[2:5]
printSliceNew("j", j)
//Slices of slices
// Create a tic-tac-toe board.
board := [][]string{
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
}
// The players take turns.
board[0][0] = "X"
board[2][2] = "O"
board[1][2] = "X"
board[1][0] = "O"
board[0][2] = "X"
for i := 0; i < len(board); i++ {
fmt.Printf("%s\n", strings.Join(board[i], " "))
}
//Appending to a slice
var f []int
printSlice(f)
// append works on nil slices.
f = append(f, 0)
printSlice(f)
// The slice grows as needed.
f = append(f, 1)
printSlice(f)
// We can add more than one element at a time.
f = append(f, 2, 3, 4)
printSlice(f)
// If the backing array of f is too small to fit all the given values a bigger array will be allocated.
// The returned slice will point to the newly allocated array.
//Range
for i, v := range powArray {
fmt.Printf("2**%d = %d\n", i, v)
}
//Range continued
powNew2 := make([]int, 10)
// If you only want the index, you can omit the second variable.
for i := range powNew2 {
powNew2[i] = 1 << uint(i) // == 2**i
}
// You can skip the index or value by assigning to _.
for _, value := range powNew2 {
fmt.Printf("%d\n", value)
}
}
func printSlice(l []int) {
fmt.Printf("len=%d cap=%d %v\n", len(l), cap(l), l)
}
func printSliceNew(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n", s, len(s), cap(x), x)
}