-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
43 lines (35 loc) · 965 Bytes
/
main.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
package main
import (
"fmt"
"github.com/phakornkiong/go-pattern-match/pattern"
)
func FoodSorter(input string) (output string) {
switch input {
case "apple", "strawberry", "orange":
output = "fruit"
case "carrot", "pok-choy", "cabbage":
output = "vegetable"
default:
output = "unknown"
}
return output
}
func FoodSorterWithPattern(input string) (output string) {
output = pattern.NewMatcher[string](input).
WithPattern(
pattern.Union("apple", "strawberry", "orange"),
func() string { return "fruit" },
).
WithPattern(
pattern.Union("carrot", "pok-choy", "cabbage"),
func() string { return "vegetable" },
).
Otherwise(func() string { return "unknown" })
return output
}
func main() {
fmt.Println(FoodSorterWithPattern("apple")) // "fruit"
fmt.Println(FoodSorterWithPattern("orange")) // "fruit"
fmt.Println(FoodSorterWithPattern("carrot")) // "vegetable"
fmt.Println(FoodSorterWithPattern("candy")) // "unknown"
}