...
1
2
3 package main
4
5
6
7
8
9
10
11
12
13
14
15 func SimpleSwitch(x, y int) {
16
17
18
19
20
21
22
23 switch x {
24 case 1:
25 print(1)
26 case 2, 3:
27 print(23)
28 fallthrough
29 case 4:
30 print(3)
31 default:
32 print(4)
33 case y:
34 print(5)
35 }
36 print(6)
37 }
38
39 func four() int { return 4 }
40
41
42
43 func SwitchWithNonConstantCase(x int) {
44
45
46
47
48
49
50
51
52
53
54
55
56 switch x {
57 case 1:
58 print(1)
59 case 2, 3:
60 print(23)
61 case four():
62 print(3)
63 case 5:
64 print(5)
65 case 6:
66 print(6)
67 }
68 print("done")
69 }
70
71
72
73
74 func ImplicitSwitches(x, y int) {
75
76
77
78
79
80 if x == 1 || 2 == x || x < 5 {
81 print(12)
82 }
83
84
85
86
87
88
89 if x == 3 || 4 == x || x == y {
90 print(34)
91 }
92
93
94 if x == 5 || y == 6 {
95 print(56)
96 }
97
98
99 if x == 7 || x == y {
100 print(78)
101 }
102 }
103
104 func IfElseBasedSwitch(x int) {
105
106
107
108
109
110 if x == 1 {
111 print(1)
112 } else if x == 2 {
113 print(2)
114 } else {
115 print("else")
116 }
117 }
118
119 func GotoBasedSwitch(x int) {
120
121
122
123
124
125 if x == 1 {
126 goto L1
127 }
128 if x == 2 {
129 goto L2
130 }
131 print("else")
132 L1:
133 print(1)
134 goto end
135 L2:
136 print(2)
137 end:
138 }
139
140 func SwitchInAForLoop(x int) {
141
142
143
144
145
146 loop:
147 for {
148 print("head")
149 switch x {
150 case 1:
151 print(1)
152 break loop
153 case 2:
154 print(2)
155 break loop
156 }
157 }
158 }
159
160
161
162
163 func SwitchInAForLoopUsingGoto(x int) {
164
165
166
167
168
169 loop:
170 print("head")
171 if x == 1 {
172 goto L1
173 }
174 if x == 2 {
175 goto L2
176 }
177 goto loop
178 L1:
179 print(1)
180 goto end
181 L2:
182 print(2)
183 end:
184 }
185
186 func UnstructuredSwitchInAForLoop(x int) {
187
188
189
190
191
192 for {
193 if x == 1 {
194 print(1)
195 return
196 }
197 if x == 2 {
198 continue
199 }
200 break
201 }
202 print("end")
203 }
204
205 func CaseWithMultiplePreds(x int) {
206 for {
207 if x == 1 {
208 print(1)
209 return
210 }
211 loop:
212
213
214 if x == 2 {
215 goto loop
216 }
217 break
218 }
219 print("end")
220 }
221
222 func DuplicateConstantsAreNotEliminated(x int) {
223
224
225
226
227
228
229 if x == 1 {
230 print(1)
231 } else if x == 1 {
232 print("1a")
233 } else if x == 2 {
234 print(2)
235 }
236 }
237
238
239
240 func MakeInterfaceIsNotAConstant(x interface{}) {
241 if x == "foo" {
242 print("foo")
243 } else if x == 1 {
244 print(1)
245 }
246 }
247
248 func ZeroInitializedVarsAreConstants(x int) {
249
250
251
252
253
254 var zero int
255 if x == zero {
256 print(1)
257 } else if x == 2 {
258 print(2)
259 }
260 print("end")
261 }
262
263
264
265
266 func SelectDesugarsToSwitch(ch chan int) {
267
268
269
270
271
272
273 select {
274 case x := <-ch:
275 println(x)
276 case <-ch:
277 println(0)
278 case ch <- 1:
279 println(1)
280 default:
281 println("default")
282 }
283 }
284
285
286 func NonblockingSelectDefaultCasePanics(ch chan int) {
287
288
289
290
291
292
293 select {
294 case x := <-ch:
295 println(x)
296 case <-ch:
297 println(0)
298 case ch <- 1:
299 println(1)
300 }
301 }
302
303
304
305
306 func SimpleTypeSwitch(x interface{}) {
307
308
309
310
311
312
313 switch y := x.(type) {
314 case nil:
315 println(y)
316 case int, bool:
317 println(y)
318 case string:
319 println(y)
320 default:
321 println(y)
322 }
323 }
324
325
326 func DuplicateTypesAreNotEliminated(x interface{}) {
327
328
329
330
331
332
333 switch y := x.(type) {
334 case string:
335 println(1)
336 case interface{}:
337 println(y)
338 case int:
339 println(3)
340 }
341 }
342
343
344 func AdHocTypeSwitch(x interface{}) {
345
346
347
348
349
350 if i, ok := x.(int); ok {
351 println(i)
352 } else if s, ok := x.(string); ok {
353 println(s)
354 } else {
355 print("default")
356 }
357 }
358
View as plain text