URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [interface/] [convert.go] - Rev 700
Compare with Previous | Blame | View Log
// $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.// Check uses of all the different interface// conversion runtime functions.package maintype Stringer interface {String() string}type StringLengther interface {String() stringLength() int}type Empty interface{}type T stringfunc (t T) String() string {return string(t)}func (t T) Length() int {return len(t)}type U stringfunc (u U) String() string {return string(u)}var t = T("hello")var u = U("goodbye")var e Emptyvar s Stringer = tvar sl StringLengther = tvar i intvar ok boolfunc hello(s string) {if s != "hello" {println("not hello: ", s)panic("fail")}}func five(i int) {if i != 5 {println("not 5: ", i)panic("fail")}}func true(ok bool) {if !ok {panic("not true")}}func false(ok bool) {if ok {panic("not false")}}func main() {// T2Is = thello(s.String())// I2Tt = s.(T)hello(t.String())// T2Ee = t// E2Tt = e.(T)hello(t.String())// T2I againsl = thello(sl.String())five(sl.Length())// I2I statics = slhello(s.String())// I2I dynamicsl = s.(StringLengther)hello(sl.String())five(sl.Length())// I2E (and E2T)e = shello(e.(T).String())// E2Is = e.(Stringer)hello(s.String())// I2T2 truet, ok = s.(T)true(ok)hello(t.String())// I2T2 false_, ok = s.(U)false(ok)// I2I2 truesl, ok = s.(StringLengther)true(ok)hello(sl.String())five(sl.Length())// I2I2 false (and T2I)s = usl, ok = s.(StringLengther)false(ok)// E2T2 truet, ok = e.(T)true(ok)hello(t.String())// E2T2 falsei, ok = e.(int)false(ok)// E2I2 truesl, ok = e.(StringLengther)true(ok)hello(sl.String())five(sl.Length())// E2I2 false (and T2E)e = usl, ok = e.(StringLengther)false(ok)}
