| 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 |
package main |
| 8 |
|
| 9 |
import "fmt" |
| 10 |
|
| 11 |
type Element interface { |
| 12 |
} |
| 13 |
|
| 14 |
type Vector struct { |
| 15 |
nelem int |
| 16 |
elem []Element |
| 17 |
} |
| 18 |
|
| 19 |
func New() *Vector { |
| 20 |
v := new(Vector) |
| 21 |
v.nelem = 0 |
| 22 |
v.elem = make([]Element, 10) |
| 23 |
return v |
| 24 |
} |
| 25 |
|
| 26 |
func (v *Vector) At(i int) Element { |
| 27 |
return v.elem[i] |
| 28 |
} |
| 29 |
|
| 30 |
func (v *Vector) Insert(e Element) { |
| 31 |
v.elem[v.nelem] = e |
| 32 |
v.nelem++ |
| 33 |
} |
| 34 |
|
| 35 |
func main() { |
| 36 |
type I struct{ val int } |
| 37 |
i0 := new(I) |
| 38 |
i0.val = 0 |
| 39 |
i1 := new(I) |
| 40 |
i1.val = 11 |
| 41 |
i2 := new(I) |
| 42 |
i2.val = 222 |
| 43 |
i3 := new(I) |
| 44 |
i3.val = 3333 |
| 45 |
i4 := new(I) |
| 46 |
i4.val = 44444 |
| 47 |
v := New() |
| 48 |
r := "hi\n" |
| 49 |
v.Insert(i4) |
| 50 |
v.Insert(i3) |
| 51 |
v.Insert(i2) |
| 52 |
v.Insert(i1) |
| 53 |
v.Insert(i0) |
| 54 |
for i := 0; i < v.nelem; i++ { |
| 55 |
var x *I |
| 56 |
x = v.At(i).(*I) |
| 57 |
r += fmt.Sprintln(i, x.val) // prints correct list |
| 58 |
} |
| 59 |
for i := 0; i < v.nelem; i++ { |
| 60 |
r += fmt.Sprintln(i, v.At(i).(*I).val) |
| 61 |
} |
| 62 |
expect := `hi |
| 63 |
0 44444 |
| 64 |
1 3333 |
| 65 |
2 222 |
| 66 |
3 11 |
| 67 |
4 0 |
| 68 |
0 44444 |
| 69 |
1 3333 |
| 70 |
2 222 |
| 71 |
3 11 |
| 72 |
4 0 |
| 73 |
` |
| 74 |
if r != expect { |
| 75 |
panic(r) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
bug027.go:50: illegal types for operand |
| 81 |
(<Element>I{}) CONV (<I>{}) |
| 82 |
bug027.go:50: illegal types for operand |
| 83 |
(<Element>I{}) CONV (<I>{}) |
| 84 |
*/ |