| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | // $G $D/$F.go && $L $F.$A && ./$A.out | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | type T struct { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	a float64 | 
					
						
							|  |  |  | 	b int64 | 
					
						
							|  |  |  | 	c string | 
					
						
							|  |  |  | 	d byte | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-03 08:39:12 -08:00
										 |  |  | var a = []int{ 1, 2, 3 } | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | var NIL []int | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func arraycmptest() { | 
					
						
							|  |  |  | 	if NIL != nil { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail1:", NIL, "!= nil") | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if nil != NIL { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail2: nil !=", NIL) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if a == nil || nil == a { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail3:", a, "== nil") | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | func SameArray(a, b []int) bool { | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if len(a) != len(b) || cap(a) != cap(b) { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return false | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if len(a) > 0 && &a[0] != &b[0] { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return false | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	return true | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-03 08:39:12 -08:00
										 |  |  | var t = T{1.5, 123, "hello", 255} | 
					
						
							| 
									
										
										
										
											2009-01-06 15:19:02 -08:00
										 |  |  | var mt = make(map[int]T) | 
					
						
							|  |  |  | var ma = make(map[int][]int) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func maptest() { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	mt[0] = t | 
					
						
							|  |  |  | 	t1 := mt[0] | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail: map val struct", t1.a, t1.b, t1.c, t1.d) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	ma[1] = a | 
					
						
							|  |  |  | 	a1 := ma[1] | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if !SameArray(a, a1) { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail: map val array", a, a1) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-06 15:19:02 -08:00
										 |  |  | var ct = make(chan T) | 
					
						
							|  |  |  | var ca = make(chan []int) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func send() { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	ct <- t | 
					
						
							|  |  |  | 	ca <- a | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func chantest() { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	go send() | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	t1 := <-ct | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail: map val struct", t1.a, t1.b, t1.c, t1.d) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	a1 := <-ca | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if !SameArray(a, a1) { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("fail: map val array", a, a1) | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-20 14:40:40 -08:00
										 |  |  | type E struct { } | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | var e E | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func interfacetest() { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	var i interface{} | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	i = a | 
					
						
							|  |  |  | 	a1 := i.([]int) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if !SameArray(a, a1) { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("interface <-> []int", a, a1) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	pa := new([]int) | 
					
						
							|  |  |  | 	*pa = a | 
					
						
							|  |  |  | 	i = pa | 
					
						
							|  |  |  | 	a1 = *i.(*[]int) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if !SameArray(a, a1) { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("interface <-> *[]int", a, a1) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	i = t | 
					
						
							|  |  |  | 	t1 := i.(T) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println("interface <-> struct", t1.a, t1.b, t1.c, t1.d) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	i = e | 
					
						
							|  |  |  | 	e1 := i.(E) | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | 	// nothing to check; just verify it doesn't crash | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	_ = e1 | 
					
						
							| 
									
										
										
										
											2008-12-19 17:11:54 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	arraycmptest() | 
					
						
							|  |  |  | 	maptest() | 
					
						
							|  |  |  | 	chantest() | 
					
						
							|  |  |  | 	interfacetest() | 
					
						
							| 
									
										
										
										
											2008-12-19 12:05:22 -08:00
										 |  |  | } |