Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 1 | // $G $D/$F.go && $L $F.$A && ./$A.out |
| 2 | |
| 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | package main |
| 8 | |
| 9 | import "unsafe" |
| 10 | |
| 11 | func use(bool) { } |
| 12 | |
| 13 | func stringptr(s string) uintptr { |
| 14 | return *(&s).(unsafe.Pointer).(*uintptr); |
| 15 | } |
| 16 | |
| 17 | func isfalse(b bool) { |
| 18 | if b { panicln("wanted false, got true") } // stack will explain where |
| 19 | } |
| 20 | |
| 21 | func istrue(b bool) { |
| 22 | if !b { panicln("wanted true, got false") } // stack will explain where |
| 23 | } |
| 24 | |
| 25 | func main() |
| 26 | { |
| 27 | var a []int; |
| 28 | var b map[string]int; |
| 29 | |
| 30 | var c string = "hello"; |
| 31 | var d string = "hel"; // try to get different pointer |
| 32 | d = d + "lo"; |
| 33 | if stringptr(c) == stringptr(d) { |
| 34 | panic("compiler too smart -- got same string") |
| 35 | } |
| 36 | |
| 37 | var e = make(chan int); |
| 38 | |
| 39 | var ia interface{} = a; |
| 40 | var ib interface{} = b; |
| 41 | var ic interface{} = c; |
| 42 | var id interface{} = d; |
| 43 | var ie interface{} = e; |
| 44 | |
| 45 | // these comparisons are okay because |
| 46 | // string compare is okay and the others |
| 47 | // are comparisons where the types differ. |
| 48 | isfalse(ia == ib); |
| 49 | isfalse(ia == ic); |
| 50 | isfalse(ia == id); |
| 51 | isfalse(ib == ic); |
| 52 | isfalse(ib == id); |
| 53 | istrue(ic == id); |
| 54 | istrue(ie == ie); |
| 55 | |
| 56 | // map of interface should use == on interface values, |
| 57 | // not memory. |
| 58 | // TODO: should m[c], m[d] be valid here? |
| 59 | var m = make(map[interface{}] int); |
| 60 | m[ic] = 1; |
| 61 | m[id] = 2; |
| 62 | if m[ic] != 2 { |
| 63 | panic("m[ic] = ", m[ic]); |
| 64 | } |
| 65 | } |
| 66 | |