OLD | NEW |
1 // $G $D/$F.go && $L $F.$A && ./$A.out | 1 // $G $D/$F.go && $L $F.$A && ./$A.out |
2 | 2 |
3 // Copyright 2009 The Go Authors. All rights reserved. | 3 // Copyright 2009 The Go Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file. | 5 // license that can be found in the LICENSE file. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 type S struct | 9 type S struct { |
10 { | |
11 a,b int; | 10 a,b int; |
12 } | 11 } |
13 | 12 |
14 type I1 interface | 13 type I1 interface { |
15 { | |
16 f ()int; | 14 f ()int; |
17 } | 15 } |
18 | 16 |
19 type I2 interface | 17 type I2 interface { |
20 { | |
21 g() int; | 18 g() int; |
22 f() int; | 19 f() int; |
23 } | 20 } |
24 | 21 |
25 func | 22 func (this *S) f()int { |
26 (this *S) f()int | |
27 { | |
28 return this.a; | 23 return this.a; |
29 } | 24 } |
30 | 25 |
31 func | 26 func (this *S) g()int { |
32 (this *S) g()int | |
33 { | |
34 return this.b; | 27 return this.b; |
35 } | 28 } |
36 | 29 |
37 func | 30 func |
38 main() | 31 main() { |
39 { | |
40 var i1 I1; | 32 var i1 I1; |
41 var i2 I2; | 33 var i2 I2; |
42 var g *S; | 34 var g *S; |
43 | 35 |
44 s := new(S); | 36 s := new(S); |
45 s.a = 5; | 37 s.a = 5; |
46 s.b = 6; | 38 s.b = 6; |
47 | 39 |
48 // call structure | 40 // call structure |
49 if s.f() != 5 { panic(11); } | 41 if s.f() != 5 { panic(11); } |
50 if s.g() != 6 { panic(12); } | 42 if s.g() != 6 { panic(12); } |
51 | 43 |
52 i1 = s; // convert S to I1 | 44 i1 = s; // convert S to I1 |
53 i2 = i1.(I2); // convert I1 to I2 | 45 i2 = i1.(I2); // convert I1 to I2 |
54 | 46 |
55 // call interface | 47 // call interface |
56 if i1.f() != 5 { panic(21); } | 48 if i1.f() != 5 { panic(21); } |
57 if i2.f() != 5 { panic(22); } | 49 if i2.f() != 5 { panic(22); } |
58 if i2.g() != 6 { panic(23); } | 50 if i2.g() != 6 { panic(23); } |
59 | 51 |
60 g = i1.(*S); // convert I1 to S | 52 g = i1.(*S); // convert I1 to S |
61 if g != s { panic(31); } | 53 if g != s { panic(31); } |
62 | 54 |
63 g = i2.(*S); // convert I2 to S | 55 g = i2.(*S); // convert I2 to S |
64 if g != s { panic(32); } | 56 if g != s { panic(32); } |
65 } | 57 } |
OLD | NEW |