Repository navigation
Expand file tree
/
Copy pathmaps.odin
More file actions
96 lines (74 loc) · 2.09 KB
/
Copy pathmaps.odin
File metadata and controls
96 lines (74 loc) · 2.09 KB
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
package main
import "core:fmt"
main :: proc() {
name_to_age := make(map[string]int)
name_to_age["john"] = 30
name_to_age["nick"] = 50
name_to_age["diego"] = 45
fmt.println(name_to_age)
if "diego" in name_to_age {
fmt.println("diego found")
}
if value, existed := name_to_age["andy"]; existed {
fmt.println("andy existed")
fmt.println(value)
} else {
fmt.println("andy does not existed")
}
the_age := name_to_age["z"] or_else 80
fmt.println(the_age)
// Create a map with string keys and int values.
scores := make(map[string]int)
// Add entries.
scores["alice"] = 95
scores["bob"] = 87
fmt.println("alice:", scores["alice"])
fmt.println("bob:", scores["bob"])
// Accessing a missing key returns the zero value.
fmt.println("zero value:", scores["charlie"])
// Comma-ok idiom: ok is true if the key exists.
_, ok := scores["charlie"]
fmt.println("charlie present:", ok)
// The "in" operator also checks for key existence.
if "alice" in scores {
fmt.println("alice is in the map")
}
fmt.println("len:", len(scores))
// Remove a key.
delete_key(&scores, "bob")
fmt.println("len:", len(scores))
scores["charlie"] = 92
fmt.println("len:", len(scores))
// Iterate over keys and values. Order is not
// specified.
for key, value in scores {
fmt.println(key, value)
}
// Create a string from a literal.
s := "Hellope!"
fmt.println(s)
// len returns the number of bytes.
fmt.println("len:", len(s))
// Indexing returns a byte (u8).
fmt.println("first byte:", s[0])
// + concatenates constant strings at compile time.
fmt.println("Hello" + "pe!")
// For variables, use strings.concatenate.
greeting := strings.concatenate({s, ", world!"})
fmt.println(greeting)
// Compare strings with == and !=.
fmt.println("equal:", s == "Hellope!")
// Iterate over runes. Odin decodes UTF-8
// automatically.
for r, i in s {
fmt.println(i, r)
}
// UTF-8: len counts bytes, not characters.
word := "café"
fmt.println("bytes:", len(word))
// Indexing gives raw bytes. The é character
// uses two bytes in UTF-8.
for i in 0 ..< len(word) {
fmt.println(i, word[i])
}
}