| 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 and methods. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
type S struct { |
| 12 |
a,b int; |
| 13 |
} |
| 14 |
|
| 15 |
type I1 interface { |
| 16 |
f ()int; |
| 17 |
} |
| 18 |
|
| 19 |
type I2 interface { |
| 20 |
g() int; |
| 21 |
f() int; |
| 22 |
} |
| 23 |
|
| 24 |
func (this *S) f()int { |
| 25 |
return this.a; |
| 26 |
} |
| 27 |
|
| 28 |
func (this *S) g()int { |
| 29 |
return this.b; |
| 30 |
} |
| 31 |
|
| 32 |
func |
| 33 |
main() { |
| 34 |
var i1 I1; |
| 35 |
var i2 I2; |
| 36 |
var g *S; |
| 37 |
|
| 38 |
s := new(S); |
| 39 |
s.a = 5; |
| 40 |
s.b = 6; |
| 41 |
|
| 42 |
// call structure |
| 43 |
if s.f() != 5 { panic(11); } |
| 44 |
if s.g() != 6 { panic(12); } |
| 45 |
|
| 46 |
i1 = s; // convert S to I1 |
| 47 |
i2 = i1.(I2); // convert I1 to I2 |
| 48 |
|
| 49 |
// call interface |
| 50 |
if i1.f() != 5 { panic(21); } |
| 51 |
if i2.f() != 5 { panic(22); } |
| 52 |
if i2.g() != 6 { panic(23); } |
| 53 |
|
| 54 |
g = i1.(*S); // convert I1 to S |
| 55 |
if g != s { panic(31); } |
| 56 |
|
| 57 |
g = i2.(*S); // convert I2 to S |
| 58 |
if g != s { panic(32); } |
| 59 |
} |