-
Notifications
You must be signed in to change notification settings - Fork 87
/
switch.go
65 lines (57 loc) · 1.17 KB
/
switch.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
package main
import (
"fmt"
"os"
"regexp"
"strconv"
)
func main() {
arguments := os.Args
if len(arguments) < 2 {
fmt.Println("usage: switch number.")
os.Exit(1)
}
number, err := strconv.Atoi(arguments[1])
if err != nil {
fmt.Println("This value is not an integer:", number)
} else {
switch {
case number < 0:
fmt.Println("Less than zero!")
case number > 0:
fmt.Println("Bigger than zero!")
default:
fmt.Println("Zero!")
}
}
asString := arguments[1]
switch asString {
case "5":
fmt.Println("Five!")
case "0":
fmt.Println("Zero!")
default:
fmt.Println("Do not care!")
}
var negative = regexp.MustCompile(`-`)
var floatingPoint = regexp.MustCompile(`\d?\.\d`)
var email = regexp.MustCompile(`^[^@]+@[^@.]+\.[^@.]+`)
switch {
case negative.MatchString(asString):
fmt.Println("Negative number")
case floatingPoint.MatchString(asString):
fmt.Println("Floating point!")
case email.MatchString(asString):
fmt.Println("It is an email!")
fallthrough
default:
fmt.Println("Something else!")
}
var aType error = nil
switch aType.(type) {
case nil:
fmt.Println("It is nil interface!")
default:
fmt.Println("Not nil interface!")
}
}