-
Notifications
You must be signed in to change notification settings - Fork 4
/
05_enum_as_type.go
69 lines (60 loc) · 1.42 KB
/
05_enum_as_type.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
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Osmá část
// Tvorba balíčků a pokročilejší operace s kanály v jazyce Go
// https://www.root.cz/clanky/tvorba-balicku-a-pokrocilejsi-operace-s-kanaly-v-jazyce-go/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z osmé části:
// https://github.com/tisnik/go-root/blob/master/article_08/README.md
//
// Demonstrační příklad číslo 5:
// Výčet jako datový typ.
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_08/05_enum_as_type.html
package main
import "fmt"
// Enum je užitevatelsky definovaný datový typ mj. i pro dny v týdnu
type Enum int
// Konstanty reprezentující dny v týdnu
const (
Pondeli Enum = iota
Utery
Streda
Ctvrtek
Patek
Sobota
Nedele
)
// Den je uživatelsky definovaná datová struktura reprezentující jeden den v týdnu
type Den struct {
X Enum
}
func (day Enum) String() string {
days := []string{
"Pondeli",
"Utery",
"Streda",
"Ctvrtek",
"Patek",
"Sobota",
"Nedele"}
if day < Pondeli || day > Nedele {
return "Unknown day"
}
return days[day]
}
func reservation(day Den) {
fmt.Printf("Reservation for %s\n", day)
}
func main() {
reservation(Den{Pondeli})
reservation(Den{Sobota})
reservation(Den{Nedele})
day := Den{Pondeli}
reservation(day)
}