| 
									
										
										
										
											2012-02-16 23:51:04 -05:00
										 |  |  | // run | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Copyright 2009 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-24 11:48:19 +11:00
										 |  |  | // Test switch statements. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-24 11:48:19 +11:00
										 |  |  | import "os" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | func assert(cond bool, msg string) { | 
					
						
							|  |  |  | 	if !cond { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		print("assertion fail: ", msg, "\n") | 
					
						
							|  |  |  | 		panic(1) | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	i5 := 5 | 
					
						
							|  |  |  | 	i7 := 7 | 
					
						
							|  |  |  | 	hello := "hello" | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch true { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case i5 < 5: | 
					
						
							|  |  |  | 		assert(false, "<") | 
					
						
							|  |  |  | 	case i5 == 5: | 
					
						
							|  |  |  | 		assert(true, "!") | 
					
						
							|  |  |  | 	case i5 > 5: | 
					
						
							|  |  |  | 		assert(false, ">") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case i5 < 5: | 
					
						
							|  |  |  | 		assert(false, "<") | 
					
						
							|  |  |  | 	case i5 == 5: | 
					
						
							|  |  |  | 		assert(true, "!") | 
					
						
							|  |  |  | 	case i5 > 5: | 
					
						
							|  |  |  | 		assert(false, ">") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch x := 5; true { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case i5 < x: | 
					
						
							|  |  |  | 		assert(false, "<") | 
					
						
							|  |  |  | 	case i5 == x: | 
					
						
							|  |  |  | 		assert(true, "!") | 
					
						
							|  |  |  | 	case i5 > x: | 
					
						
							|  |  |  | 		assert(false, ">") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-06 15:53:14 -07:00
										 |  |  | 	switch x := 5; true { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case i5 < x: | 
					
						
							|  |  |  | 		assert(false, "<") | 
					
						
							|  |  |  | 	case i5 == x: | 
					
						
							|  |  |  | 		assert(true, "!") | 
					
						
							|  |  |  | 	case i5 > x: | 
					
						
							|  |  |  | 		assert(false, ">") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch i5 { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case 0: | 
					
						
							|  |  |  | 		assert(false, "0") | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 		assert(false, "1") | 
					
						
							|  |  |  | 	case 2: | 
					
						
							|  |  |  | 		assert(false, "2") | 
					
						
							|  |  |  | 	case 3: | 
					
						
							|  |  |  | 		assert(false, "3") | 
					
						
							|  |  |  | 	case 4: | 
					
						
							|  |  |  | 		assert(false, "4") | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		assert(true, "5") | 
					
						
							|  |  |  | 	case 6: | 
					
						
							|  |  |  | 		assert(false, "6") | 
					
						
							|  |  |  | 	case 7: | 
					
						
							|  |  |  | 		assert(false, "7") | 
					
						
							|  |  |  | 	case 8: | 
					
						
							|  |  |  | 		assert(false, "8") | 
					
						
							|  |  |  | 	case 9: | 
					
						
							|  |  |  | 		assert(false, "9") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "default") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch i5 { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case 0, 1, 2, 3, 4: | 
					
						
							|  |  |  | 		assert(false, "4") | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		assert(true, "5") | 
					
						
							|  |  |  | 	case 6, 7, 8, 9: | 
					
						
							|  |  |  | 		assert(false, "9") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "default") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch i5 { | 
					
						
							|  |  |  | 	case 0: | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 	case 2: | 
					
						
							|  |  |  | 	case 3: | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case 4: | 
					
						
							|  |  |  | 		assert(false, "4") | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		assert(true, "5") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	case 6: | 
					
						
							|  |  |  | 	case 7: | 
					
						
							|  |  |  | 	case 8: | 
					
						
							| 
									
										
										
										
											2009-09-14 21:03:53 -07:00
										 |  |  | 	case 9: | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		assert(i5 == 5, "good") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch i5 { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case 0: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 2: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 3: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 4: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		assert(false, "4") | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 6: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 7: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 8: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 9: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		assert(i5 == 5, "good") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	fired := false | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	switch i5 { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case 0: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough // tests scoping of cases | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 2: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 3: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 4: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		assert(false, "4") | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 6: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 7: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 8: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 9: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		dummy := 0 | 
					
						
							|  |  |  | 		_ = dummy | 
					
						
							|  |  |  | 		fired = !fired | 
					
						
							|  |  |  | 		assert(i5 == 5, "good") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	assert(fired, "fired") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	count := 0 | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	switch i5 { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case 0: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 2: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 3: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 4: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		assert(false, "4") | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 6: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 7: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 8: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case 9: | 
					
						
							|  |  |  | 		count = count + 1 | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(i5 == count, "good") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	assert(fired, "fired") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-15 20:24:30 -07:00
										 |  |  | 	switch hello { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case "wowie": | 
					
						
							|  |  |  | 		assert(false, "wowie") | 
					
						
							|  |  |  | 	case "hello": | 
					
						
							|  |  |  | 		assert(true, "hello") | 
					
						
							|  |  |  | 	case "jumpn": | 
					
						
							|  |  |  | 		assert(false, "jumpn") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "default") | 
					
						
							| 
									
										
										
										
											2008-06-15 20:24:30 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	fired = false | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	switch i := i5 + 2; i { | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 	case i7: | 
					
						
							|  |  |  | 		fired = true | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "fail") | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	assert(fired, "var") | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// switch on nil-only comparison types | 
					
						
							|  |  |  | 	switch f := func() {}; f { | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		assert(false, "f should not be nil") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch m := make(map[int]int); m { | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		assert(false, "m should not be nil") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch a := make([]int, 1); a { | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		assert(false, "m should not be nil") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-02-24 11:48:19 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-03 21:47:26 +02:00
										 |  |  | 	// switch on interface. | 
					
						
							|  |  |  | 	switch i := interface{}("hello"); i { | 
					
						
							|  |  |  | 	case 42: | 
					
						
							|  |  |  | 		assert(false, `i should be "hello"`) | 
					
						
							|  |  |  | 	case "hello": | 
					
						
							|  |  |  | 		assert(true, "hello") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, `i should be "hello"`) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-17 21:29:10 +02:00
										 |  |  | 	// switch on implicit bool converted to interface | 
					
						
							|  |  |  | 	// was broken: see issue 3980 | 
					
						
							|  |  |  | 	switch i := interface{}(true); { | 
					
						
							|  |  |  | 	case i: | 
					
						
							|  |  |  | 		assert(true, "true") | 
					
						
							|  |  |  | 	case false: | 
					
						
							|  |  |  | 		assert(false, "i should be true") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "i should be true") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-26 00:45:43 +01:00
										 |  |  | 	// switch on interface with constant cases differing by type. | 
					
						
							|  |  |  | 	// was rejected by compiler: see issue 4781 | 
					
						
							|  |  |  | 	type T int | 
					
						
							|  |  |  | 	type B bool | 
					
						
							|  |  |  | 	type F float64 | 
					
						
							|  |  |  | 	type S string | 
					
						
							|  |  |  | 	switch i := interface{}(float64(1.0)); i { | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case (*int)(nil): | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case 1: | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case T(1): | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case F(1.0): | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case 1.0: | 
					
						
							|  |  |  | 		assert(true, "true") | 
					
						
							|  |  |  | 	case "hello": | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case S("hello"): | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case true, B(false): | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	case false, B(true): | 
					
						
							|  |  |  | 		assert(false, "i should be float64(1.0)") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-03 21:47:26 +02:00
										 |  |  | 	// switch on array. | 
					
						
							|  |  |  | 	switch ar := [3]int{1, 2, 3}; ar { | 
					
						
							| 
									
										
										
										
											2013-02-11 18:20:52 -05:00
										 |  |  | 	case [3]int{1, 2, 3}: | 
					
						
							| 
									
										
										
										
											2012-08-03 21:47:26 +02:00
										 |  |  | 		assert(true, "[1 2 3]") | 
					
						
							| 
									
										
										
										
											2013-02-11 18:20:52 -05:00
										 |  |  | 	case [3]int{4, 5, 6}: | 
					
						
							| 
									
										
										
										
											2012-08-03 21:47:26 +02:00
										 |  |  | 		assert(false, "ar should be [1 2 3]") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "ar should be [1 2 3]") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// switch on channel | 
					
						
							|  |  |  | 	switch c1, c2 := make(chan int), make(chan int); c1 { | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		assert(false, "c1 did not match itself") | 
					
						
							|  |  |  | 	case c2: | 
					
						
							|  |  |  | 		assert(false, "c1 did not match itself") | 
					
						
							|  |  |  | 	case c1: | 
					
						
							|  |  |  | 		assert(true, "chan") | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		assert(false, "c1 did not match itself") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-11 18:20:52 -05:00
										 |  |  | 	// empty switch | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// empty switch with default case. | 
					
						
							|  |  |  | 	fired = false | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		fired = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	assert(fired, "fail") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Default and fallthrough. | 
					
						
							|  |  |  | 	count = 0 | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		count++ | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	case false: | 
					
						
							|  |  |  | 		count++ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	assert(count == 2, "fail") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// fallthrough to default, which is not at end. | 
					
						
							|  |  |  | 	count = 0 | 
					
						
							|  |  |  | 	switch i5 { | 
					
						
							|  |  |  | 	case 5: | 
					
						
							|  |  |  | 		count++ | 
					
						
							|  |  |  | 		fallthrough | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		count++ | 
					
						
							|  |  |  | 	case 6: | 
					
						
							|  |  |  | 		count++ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	assert(count == 2, "fail") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-24 11:48:19 +11:00
										 |  |  | 	i := 0 | 
					
						
							|  |  |  | 	switch x := 5; { | 
					
						
							| 
									
										
										
										
											2013-02-11 18:20:52 -05:00
										 |  |  | 	case i < x: | 
					
						
							|  |  |  | 		os.Exit(0) | 
					
						
							|  |  |  | 	case i == x: | 
					
						
							|  |  |  | 	case i > x: | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							| 
									
										
										
										
											2012-02-24 11:48:19 +11:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-06-06 13:28:03 -07:00
										 |  |  | } |