-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
89 lines (64 loc) · 1.91 KB
/
types.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
package main
import (
"fmt"
)
// ****************** ARRAYS **********************
// create an array of 5 integers
var x [5]int
// populate it quickly:
// x := [5]float64{ 98, 93, 77, 82, 83 }
func main() {
x := [5]float64{98, 93, 77, 82, 83}
x[4] = 100 // set the fifth element to 100
}
// this is why we use the underscore:
// this function only grabs the index of the array and adds up the indices
func getIndex() {
x := []int{1, 2, 97} // I used a slice here cause I didn't know ahead of time how many elements my array would have
for index := range x {
total += index
}
fmt.Println(total) // 0 + 1 + 2 = 3
}
// if you want to actually grab the value at that index, use the undescore:
func getValue() {
x := []int{1, 2, 97}
for _, value := range x {
total += value
}
fmt.Println(total) // 1 + 2 + 97 = 100
}
// ****************** SLICES **********************
// have two built in methods:
func appendSlice() {
slice1 := []int{1, 2, 3}
slice2 := append(slice1, 4, 5) // [1,2,3,4,5]
}
// other method is copy
// use make() to initialize a slice:
var pow = make([]int, 10) // this allocates a zeroed array of length 10. To specify capacity, pass in a third argument
// make is one of two allocation primitives
// ************ MAPS/HASHES/DICTIONARIES *******************
// maps are unordered collections of key-value pairs
// map with key type string and value type int
// this declares the map but doesn't initialize it
var myMap map[string]int
// to initialize it:
func makeMap() {
myRealMap := make(map[string]int)
x["key"] = 10 // set the key "key" to value 10
fmt.Println(x["key"]) // use the same syntax as defining the key to access the key
//shorter way:
shortMap := {
"H": "Hydrogen",
"He": "Helium"
}
}
func checkMap() {
m := make(map[string]string)
m["name"] = "John"
// delete a key:
delete(m, "name")
// check that a key is present:
v, ok := m["name"] // ok is false
}