-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
54 lines (39 loc) · 953 Bytes
/
map.go
File metadata and controls
54 lines (39 loc) · 953 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
53
54
package main
/*
Maps são tipos de dados associativos padrões no Go (às vezes chamados de hashes ou
dicionários em outras linguagens).
*/
import "fmt"
type Aviao struct {
Ano int
}
func main() {
// criando um map de struct aviao
var avioes map[string]Aviao
avioes = make(map[string]Aviao)
avioes["novo"] = Aviao{1999}
avioes["antigo"] = Aviao{2000}
fmt.Println(avioes["novo"])
fmt.Println(avioes)
// apagando a chave "antigo"
delete(avioes, "antigo")
fmt.Println(avioes)
// verificando se existe, se existir a variavel "ok" ira retornar true
aviao, ok := avioes["antigo"]
fmt.Println(aviao, ok)
// ==========
var avioes2 = map[string]Aviao{
"luiz": {1956},
"filipy": {1890},
}
fmt.Println(avioes2)
// ==========
// chave string, valor int
nums := make(map[string]int)
nums["a"] = 12
nums["b"] = 15
fmt.Println(nums)
fmt.Println(nums["b"])
nums2 := map[string]int{"c": 23, "d": 34}
fmt.Println(nums2)
}