1 // run 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 // Test interfaces on basic types. 8 9 package main 10 11 type myint int 12 type mystring string 13 type I0 interface{} 14 15 func f() { 16 var ia, ib I0 17 var i myint 18 var s mystring 19 20 if ia != ib { 21 panic("1") 22 } 23 24 i = 1 25 ia = i 26 ib = i 27 if ia != ib { 28 panic("2") 29 } 30 if ia == nil { 31 panic("3") 32 } 33 34 i = 2 35 ia = i 36 if ia == ib { 37 panic("4") 38 } 39 40 ia = nil 41 if ia == ib { 42 panic("5") 43 } 44 45 ib = nil 46 if ia != ib { 47 panic("6") 48 } 49 50 if ia != nil { 51 panic("7") 52 } 53 54 s = "abc" 55 ia = s 56 ib = nil 57 if ia == ib { 58 panic("8") 59 } 60 61 s = "def" 62 ib = s 63 if ia == ib { 64 panic("9") 65 } 66 67 s = "abc" 68 ib = s 69 if ia != ib { 70 panic("a") 71 } 72 } 73 74 func main() { 75 var ia [20]I0 76 var b bool 77 var s string 78 var i8 int8 79 var i16 int16 80 var i32 int32 81 var i64 int64 82 var u8 uint8 83 var u16 uint16 84 var u32 uint32 85 var u64 uint64 86 87 f() 88 89 ia[0] = "xxx" 90 ia[1] = 12345 91 ia[2] = true 92 93 s = "now is" 94 ia[3] = s 95 b = false 96 ia[4] = b 97 98 i8 = 29 99 ia[5] = i8 100 i16 = 994 101 ia[6] = i16 102 i32 = 3434 103 ia[7] = i32 104 i64 = 1234567 105 ia[8] = i64 106 107 u8 = 12 108 ia[9] = u8 109 u16 = 799 110 ia[10] = u16 111 u32 = 4455 112 ia[11] = u32 113 u64 = 765432 114 ia[12] = u64 115 116 s = ia[0].(string) 117 if s != "xxx" { 118 println(0, s) 119 panic("fail") 120 } 121 i32 = int32(ia[1].(int)) 122 if i32 != 12345 { 123 println(1, i32) 124 panic("fail") 125 } 126 b = ia[2].(bool) 127 if b != true { 128 println(2, b) 129 panic("fail") 130 } 131 132 s = ia[3].(string) 133 if s != "now is" { 134 println(3, s) 135 panic("fail") 136 } 137 b = ia[4].(bool) 138 if b != false { 139 println(4, b) 140 panic("fail") 141 } 142 143 i8 = ia[5].(int8) 144 if i8 != 29 { 145 println(5, i8) 146 panic("fail") 147 } 148 i16 = ia[6].(int16) 149 if i16 != 994 { 150 println(6, i16) 151 panic("fail") 152 } 153 i32 = ia[7].(int32) 154 if i32 != 3434 { 155 println(7, i32) 156 panic("fail") 157 } 158 i64 = ia[8].(int64) 159 if i64 != 1234567 { 160 println(8, i64) 161 panic("fail") 162 } 163 164 u8 = ia[9].(uint8) 165 if u8 != 12 { 166 println(5, u8) 167 panic("fail") 168 } 169 u16 = ia[10].(uint16) 170 if u16 != 799 { 171 println(6, u16) 172 panic("fail") 173 } 174 u32 = ia[11].(uint32) 175 if u32 != 4455 { 176 println(7, u32) 177 panic("fail") 178 } 179 u64 = ia[12].(uint64) 180 if u64 != 765432 { 181 println(8, u64) 182 panic("fail") 183 } 184 } 185