codekingpro/portable-devtools
114k
1// run2 3// Copyright 2009 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7// Test all the different interface conversion runtime functions.8 9package main10 11type Stringer interface {12 String() string13}14type StringLengther interface {15 String() string16 Length() int17}18type Empty interface{}19 20type T string21 22func (t T) String() string {23 return string(t)24}25func (t T) Length() int {26 return len(t)27}28 29type U string30 31func (u U) String() string {32 return string(u)33}34 35var t = T("hello")36var u = U("goodbye")37var e Empty38var s Stringer = t39var sl StringLengther = t40var i int41var ok bool42 43func hello(s string) {44 if s != "hello" {45 println("not hello: ", s)46 panic("fail")47 }48}49 50func five(i int) {51 if i != 5 {52 println("not 5: ", i)53 panic("fail")54 }55}56 57func true(ok bool) {58 if !ok {59 panic("not true")60 }61}62 63func false(ok bool) {64 if ok {65 panic("not false")66 }67}68 69func main() {70 // T2I71 s = t72 hello(s.String())73 74 // I2T75 t = s.(T)76 hello(t.String())77 78 // T2E79 e = t80 81 // E2T82 t = e.(T)83 hello(t.String())84 85 // T2I again86 sl = t87 hello(sl.String())88 five(sl.Length())89 90 // I2I static91 s = sl92 hello(s.String())93 94 // I2I dynamic95 sl = s.(StringLengther)96 hello(sl.String())97 five(sl.Length())98 99 // I2E (and E2T)100 e = s101 hello(e.(T).String())102 103 // E2I104 s = e.(Stringer)105 hello(s.String())106 107 // I2T2 true108 t, ok = s.(T)109 true(ok)110 hello(t.String())111 112 // I2T2 false113 _, ok = s.(U)114 false(ok)115 116 // I2I2 true117 sl, ok = s.(StringLengther)118 true(ok)119 hello(sl.String())120 five(sl.Length())121 122 // I2I2 false (and T2I)123 s = u124 sl, ok = s.(StringLengther)125 false(ok)126 127 // E2T2 true128 t, ok = e.(T)129 true(ok)130 hello(t.String())131 132 // E2T2 false133 i, ok = e.(int)134 false(ok)135 136 // E2I2 true137 sl, ok = e.(StringLengther)138 true(ok)139 hello(sl.String())140 five(sl.Length())141 142 // E2I2 false (and T2E)143 e = u144 sl, ok = e.(StringLengther)145 false(ok)146}147 