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
26 func squareJump(x int) (int, int) {
27
28
29
30 switch x {
31 case 1:
32 return 1, 1
33 case 2:
34 return 4, 2
35 case 3:
36 return 9, 3
37 case 4:
38 return 16, 4
39 case 5:
40 return 25, 5
41 case 6:
42 return 36, 6
43 case 7:
44 return 49, 7
45 case 8:
46 return 64, 8
47 default:
48 return x * x, x
49 }
50 }
51
52
53 func squareLookup(x int) int {
54
55
56
57 switch x {
58 case 1:
59 return 1
60 case 2:
61 return 4
62 case 3:
63 return 9
64 case 4:
65 return 16
66 case 5:
67 return 25
68 case 6:
69 return 36
70 case 7:
71 return 49
72 case 8:
73 return 64
74 default:
75 return x * x
76 }
77 }
78
79
80
81 func fallthroughLookup(x int) int {
82
83
84 switch x {
85 case 1:
86 return 1
87 case 2:
88 return 2
89 case 3:
90 fallthrough
91 case 4:
92 return 40
93 case 5:
94 return 5
95 case 6:
96 return 6
97 case 7:
98 return 7
99 case 8:
100 return 8
101 case 9:
102 fallthrough
103 default:
104 return x * x
105 }
106 }
107
108
109 func boolLookup(x int) bool {
110
111
112
113 switch x {
114 case 1:
115 return true
116 case 2:
117 return false
118 case 3:
119 return true
120 case 4:
121 return false
122 case 5:
123 return true
124 case 6:
125 return false
126 case 7:
127 return true
128 case 8:
129 return false
130 default:
131 return x > 0
132 }
133 }
134
135
136 func floatLookup(x int) float64 {
137
138
139
140 switch x {
141 case 1:
142 return 1.5
143 case 2:
144 return 2.5
145 case 3:
146 return 3.5
147 case 4:
148 return 4.5
149 case 5:
150 return 5.5
151 case 6:
152 return 6.5
153 case 7:
154 return 7.5
155 case 8:
156 return 8.5
157 default:
158 return 0.0
159 }
160 }
161
162
163 func stringLookup(x int) string {
164
165
166 switch x {
167 case 1:
168 return "a"
169 case 2:
170 return "b"
171 case 3:
172 return "c"
173 case 4:
174 return "d"
175 case 5:
176 return "e"
177 case 6:
178 return "f"
179 case 7:
180 return "g"
181 case 8:
182 return "h"
183 default:
184 return ""
185 }
186 }
187
188
189 func complexLookup(x int) complex128 {
190
191
192 switch x {
193 case 1:
194 return 1 + 2i
195 case 2:
196 return 3 + 4i
197 case 3:
198 return 5 + 6i
199 case 4:
200 return 7 + 8i
201 case 5:
202 return 9 + 10i
203 case 6:
204 return 11 + 12i
205 case 7:
206 return 13 + 14i
207 case 8:
208 return 15 + 16i
209 default:
210 return 0
211 }
212 }
213
214
215 func length(x string) int {
216
217
218
219 switch x {
220 case "a":
221 return 1
222 case "bb":
223 return 2
224 case "ccc":
225 return 3
226 case "dddd":
227 return 4
228 case "eeeee":
229 return 5
230 case "ffffff":
231 return 6
232 case "ggggggg":
233 return 7
234 case "hhhhhhhh":
235 return 8
236 default:
237 return len(x)
238 }
239 }
240
241
242
243 func mimetype(ext string) string {
244
245
246 switch ext {
247
248
249 case ".htm":
250 return "A"
251
252
253 case ".eot":
254 return "B"
255
256
257 case ".svg":
258 return "C"
259
260
261 case ".ttf":
262 return "D"
263 default:
264 return ""
265 }
266 }
267
268
269 func typeSwitch(x any) int {
270
271
272 switch x.(type) {
273 case int:
274 return 0
275 case int8:
276 return 1
277 case int16:
278 return 2
279 case int32:
280 return 3
281 case int64:
282 return 4
283 }
284 return 7
285 }
286
287 type I interface {
288 foo()
289 }
290 type J interface {
291 bar()
292 }
293 type IJ interface {
294 I
295 J
296 }
297 type K interface {
298 baz()
299 }
300
301
302 func interfaceSwitch(x any) int {
303
304
305 switch x.(type) {
306 case I:
307 return 1
308 case J:
309 return 2
310 default:
311 return 3
312 }
313 }
314
315 func interfaceSwitch2(x K) int {
316
317
318 switch x.(type) {
319 case I:
320 return 1
321 case J:
322 return 2
323 default:
324 return 3
325 }
326 }
327
328 func interfaceCast(x any) int {
329
330
331 if _, ok := x.(I); ok {
332 return 3
333 }
334 return 5
335 }
336
337 func interfaceCast2(x K) int {
338
339
340 if _, ok := x.(I); ok {
341 return 3
342 }
343 return 5
344 }
345
346 func interfaceConv(x IJ) I {
347
348
349 return x
350 }
351
352
353 func stringSwitchInlineable(s string) {
354 switch s {
355 case "foo", "bar", "baz", "goo":
356 default:
357 println("no")
358 }
359 }
360 func stringSwitch() {
361
362
363 stringSwitchInlineable("foo")
364 }
365
View as plain text