| 
									
										
										
										
											2012-02-16 23:51:04 -05:00
										 |  |  | // run | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import "fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	a = iota | 
					
						
							|  |  |  | 	b | 
					
						
							|  |  |  | 	c | 
					
						
							|  |  |  | 	d | 
					
						
							|  |  |  | 	e | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | var x = []int{1, 2, 3} | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func f(x int, len *byte) { | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	*len = byte(x) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func whatis(x interface{}) string { | 
					
						
							|  |  |  | 	switch xx := x.(type) { | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("default ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case int, int8, int16, int32: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("signed ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case int64: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("signed64 ", int64(xx)) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case uint, uint8, uint16, uint32: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("unsigned ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case uint64: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("unsigned64 ", uint64(xx)) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case nil: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("nil ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	panic("not reached") | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func whatis1(x interface{}) string { | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	xx := x | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	switch xx.(type) { | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("default ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case int, int8, int16, int32: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("signed ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case int64: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("signed64 ", xx.(int64)) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case uint, uint8, uint16, uint32: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("unsigned ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case uint64: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("unsigned64 ", xx.(uint64)) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	case nil: | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		return fmt.Sprint("nil ", xx) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	panic("not reached") | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func check(x interface{}, s string) { | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	w := whatis(x) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	if w != s { | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		fmt.Println("whatis", x, "=>", w, "!=", s) | 
					
						
							|  |  |  | 		panic("fail") | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	w = whatis1(x) | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	if w != s { | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 		fmt.Println("whatis1", x, "=>", w, "!=", s) | 
					
						
							|  |  |  | 		panic("fail") | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2010-03-30 10:34:57 -07:00
										 |  |  | 	check(1, "signed 1") | 
					
						
							|  |  |  | 	check(uint(1), "unsigned 1") | 
					
						
							|  |  |  | 	check(int64(1), "signed64 1") | 
					
						
							|  |  |  | 	check(uint64(1), "unsigned64 1") | 
					
						
							|  |  |  | 	check(1.5, "default 1.5") | 
					
						
							|  |  |  | 	check(nil, "nil <nil>") | 
					
						
							| 
									
										
										
										
											2009-09-09 00:18:16 -07:00
										 |  |  | } |