blob: 73d1b0660bd7666e610c3c1ef61c1bce21afc271 [file] [log] [blame]
Rob Pikeab34d152008-06-06 14:27:34 -07001// $G $D/$F.go && $L $F.$A && ./$A.out
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
7package main
8
Robert Griesemer581530e2009-12-10 12:53:23 -08009type Iputs interface {
Ian Lance Taylor5e77b002012-01-18 16:12:24 -080010 puts (s string) string;
Rob Pikeab34d152008-06-06 14:27:34 -070011}
12
13// ---------
14
Robert Griesemer581530e2009-12-10 12:53:23 -080015type Print struct {
Rob Pikeab34d152008-06-06 14:27:34 -070016 whoami int;
17 put Iputs;
18}
19
Ian Lance Taylor5e77b002012-01-18 16:12:24 -080020func (p *Print) dop() string {
21 r := " print " + string(p.whoami + '0')
22 return r + p.put.puts("abc");
Rob Pikeab34d152008-06-06 14:27:34 -070023}
24
25// ---------
26
Robert Griesemer581530e2009-12-10 12:53:23 -080027type Bio struct {
Rob Pikeab34d152008-06-06 14:27:34 -070028 whoami int;
29 put Iputs;
30}
31
Ian Lance Taylor5e77b002012-01-18 16:12:24 -080032func (b *Bio) puts(s string) string {
33 r := " bio " + string(b.whoami + '0')
34 return r + b.put.puts(s);
Rob Pikeab34d152008-06-06 14:27:34 -070035}
36
37// ---------
38
Robert Griesemer581530e2009-12-10 12:53:23 -080039type File struct {
Rob Pikeab34d152008-06-06 14:27:34 -070040 whoami int;
41 put Iputs;
42}
43
Ian Lance Taylor5e77b002012-01-18 16:12:24 -080044func (f *File) puts(s string) string {
45 return " file " + string(f.whoami + '0') + " -- " + s
Rob Pikeab34d152008-06-06 14:27:34 -070046}
47
48func
Robert Griesemer581530e2009-12-10 12:53:23 -080049main() {
Russ Cox55645042009-01-06 15:19:02 -080050 p := new(Print);
51 b := new(Bio);
52 f := new(File);
Rob Pikeab34d152008-06-06 14:27:34 -070053
54 p.whoami = 1;
55 p.put = b;
56
57 b.whoami = 2;
58 b.put = f;
59
60 f.whoami = 3;
61
Ian Lance Taylor5e77b002012-01-18 16:12:24 -080062 r := p.dop();
63 expected := " print 1 bio 2 file 3 -- abc"
64 if r != expected {
65 panic(r + " != " + expected)
66 }
Rob Pikeab34d152008-06-06 14:27:34 -070067}