1 package main
2
3 import "fmt"
4
5 func main() {
6 /*
7 1.switch的标注写法:
8 switch 变量{
9 case 数值1:分支1
10 case 数值2:分支2
11 。。。
12 default:
13 最后一个分支
14 }
15 2.省略switch后的变量,相当于直接作用在true上
16 switch{//true
17 case true:
18 case false:
19 }
20
21 3.case后可以同时跟随多个数值
22 switch 变量{
23 case 数值1,数值2,数值3:
24
25 case 数值4,数值5:
26
27 }
28
29 4.switch后可以多一条初始化语句
30 switch 初始化语句;变量{
31 }
32 */
33 switch {
34 case true:
35 fmt.Println("true..")
36 case false:
37 fmt.Println("false...")
38 }
39 /*
40 成绩:
41 [0-59],不及格
42 [60,69],及格
43 [70,79],中
44 [80,89],良好
45 [90,100],优秀
46 */
47 score := 88
48 switch {
49 case score >= 0 && score < 60:
50 fmt.Println(score, "不及格")
51 case score >= 60 && score < 70:
52 fmt.Println(score, "及格")
53 case score >= 70 && score < 80:
54 fmt.Println(score, "中等")
55 case score >= 80 && score < 90:
56 fmt.Println(score, "良好")
57 case score >= 90 && score <= 100:
58 fmt.Println(score, "优秀")
59 default:
60 fmt.Println("成绩有误。。。")
61
62 }
63
64 fmt.Println("---------------------")
65 letter := ""
66 switch letter {
67 case "A", "E", "I", "O", "U":
68 fmt.Println(letter, "是元音。。")
69 case "M", "N":
70 fmt.Println("M或N。。")
71 default:
72 fmt.Println("其他。。")
73 }
74 /*
75 一个月的天数
76 1,3,5,7,8,10,12
77 31
78 4,6,9,11
79 30
80 2:29/28
81 */
82 month := 9
83 day := 0
84 year := 2019
85 switch month {
86 case 1, 3, 5, 7, 8, 10, 12:
87 day = 31
88
89 case 4, 6, 9, 11:
90 day = 30
91 case 2:
92 if year%400 == 0 || year%4 == 0 && year%100 != 0 {
93 day = 29
94 } else {
95 day = 28
96 }
97 default:
98 fmt.Println("月份有误。。")
99 }
100 fmt.Printf("%d 年 %d 月 的天数是:%d\n", year, month, day)
101 fmt.Println("--------------------------")
102
103 switch language := "golang"; language {
104 case "golang":
105 fmt.Println("Go语言。。")
106 case "java":
107 fmt.Println("Java语言。。")
108 case "python":
109 fmt.Println("Python语言。。")
110 }
111 //fmt.Println(language) //undefined: language
112 }