Source file
test/codegen/switch.go
1
2
3
4
5
6
7
8
9 package codegen
10
11
12 func f(x string) int {
13
14 switch x {
15 case "":
16 return -1
17 case "1", "2", "3":
18 return -2
19 default:
20 return -3
21 }
22 }
23
24
25 func square(x int) int {
26
27
28 switch x {
29 case 1:
30 return 1
31 case 2:
32 return 4
33 case 3:
34 return 9
35 case 4:
36 return 16
37 case 5:
38 return 25
39 case 6:
40 return 36
41 case 7:
42 return 49
43 case 8:
44 return 64
45 default:
46 return x * x
47 }
48 }
49
50
51 func length(x string) int {
52
53
54 switch x {
55 case "a":
56 return 1
57 case "bb":
58 return 2
59 case "ccc":
60 return 3
61 case "dddd":
62 return 4
63 case "eeeee":
64 return 5
65 case "ffffff":
66 return 6
67 case "ggggggg":
68 return 7
69 case "hhhhhhhh":
70 return 8
71 default:
72 return len(x)
73 }
74 }
75
76
77
78 func mimetype(ext string) string {
79
80
81 switch ext {
82
83
84 case ".htm":
85 return "A"
86
87
88 case ".eot":
89 return "B"
90
91
92 case ".svg":
93 return "C"
94
95
96 case ".ttf":
97 return "D"
98 default:
99 return ""
100 }
101 }
102
103
104 func typeSwitch(x any) int {
105
106
107 switch x.(type) {
108 case int:
109 return 0
110 case int8:
111 return 1
112 case int16:
113 return 2
114 case int32:
115 return 3
116 case int64:
117 return 4
118 }
119 return 7
120 }
121
122 type I interface {
123 foo()
124 }
125 type J interface {
126 bar()
127 }
128 type IJ interface {
129 I
130 J
131 }
132 type K interface {
133 baz()
134 }
135
136
137 func interfaceSwitch(x any) int {
138
139
140 switch x.(type) {
141 case I:
142 return 1
143 case J:
144 return 2
145 default:
146 return 3
147 }
148 }
149
150 func interfaceSwitch2(x K) int {
151
152
153 switch x.(type) {
154 case I:
155 return 1
156 case J:
157 return 2
158 default:
159 return 3
160 }
161 }
162
163 func interfaceCast(x any) int {
164
165
166 if _, ok := x.(I); ok {
167 return 3
168 }
169 return 5
170 }
171
172 func interfaceCast2(x K) int {
173
174
175 if _, ok := x.(I); ok {
176 return 3
177 }
178 return 5
179 }
180
181 func interfaceConv(x IJ) I {
182
183
184 return x
185 }
186
View as plain text