Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Rob Pike | a65cc2a | 2008-06-18 11:51:21 -0700 | [diff] [blame] | 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 | package main |
| 8 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 9 | type Element interface { |
Rob Pike | a65cc2a | 2008-06-18 11:51:21 -0700 | [diff] [blame] | 10 | } |
| 11 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 12 | type Vector struct { |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 13 | elem []Element; |
Rob Pike | a65cc2a | 2008-06-18 11:51:21 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | func (v *Vector) At(i int) Element { |
| 17 | return v.elem[i]; |
| 18 | } |
| 19 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 20 | type TStruct struct { |
Rob Pike | a65cc2a | 2008-06-18 11:51:21 -0700 | [diff] [blame] | 21 | name string; |
| 22 | fields *Vector; |
| 23 | } |
| 24 | |
Rob Pike | c137d6b | 2008-06-25 15:22:27 -0700 | [diff] [blame] | 25 | func (s *TStruct) field(i int) *TStruct { |
Russ Cox | 49e2087 | 2009-02-11 17:55:16 -0800 | [diff] [blame] | 26 | return s.fields.At(i).(*TStruct); |
Rob Pike | c137d6b | 2008-06-25 15:22:27 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | func main() { |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 30 | v := new(Vector); |
| 31 | v.elem = make([]Element, 10); |
| 32 | t := new(TStruct); |
Rob Pike | c137d6b | 2008-06-25 15:22:27 -0700 | [diff] [blame] | 33 | t.name = "hi"; |
| 34 | v.elem[0] = t; |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 35 | s := new(TStruct); |
Rob Pike | c137d6b | 2008-06-25 15:22:27 -0700 | [diff] [blame] | 36 | s.name = "foo"; |
| 37 | s.fields = v; |
| 38 | if s.field(0).name != "hi" { |
Rob Pike | bc2f5f1 | 2008-08-11 22:07:49 -0700 | [diff] [blame] | 39 | panic("bad name") |
Rob Pike | c137d6b | 2008-06-25 15:22:27 -0700 | [diff] [blame] | 40 | } |
Rob Pike | a65cc2a | 2008-06-18 11:51:21 -0700 | [diff] [blame] | 41 | } |