diff options
Diffstat (limited to 'test/ken/rob1.go')
-rw-r--r-- | test/ken/rob1.go | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/test/ken/rob1.go b/test/ken/rob1.go index 03350662a..a5854b93e 100644 --- a/test/ken/rob1.go +++ b/test/ken/rob1.go @@ -7,61 +7,64 @@ package main type Item interface { - Print(); + Print() string } type ListItem struct { - item Item; - next *ListItem; + item Item + next *ListItem } type List struct { - head *ListItem; + head *ListItem } func (list *List) Init() { - list.head = nil; + list.head = nil } func (list *List) Insert(i Item) { - item := new(ListItem); - item.item = i; - item.next = list.head; - list.head = item; + item := new(ListItem) + item.item = i + item.next = list.head + list.head = item } -func (list *List) Print() { - i := list.head; +func (list *List) Print() string { + r := "" + i := list.head for i != nil { - i.item.Print(); - i = i.next; + r += i.item.Print() + i = i.next } + return r } // Something to put in a list type Integer struct { - val int; + val int } func (this *Integer) Init(i int) *Integer { - this.val = i; - return this; + this.val = i + return this } -func (this *Integer) Print() { - print(this.val); +func (this *Integer) Print() string { + return string(this.val + '0') } -func -main() { - list := new(List); - list.Init(); +func main() { + list := new(List) + list.Init() for i := 0; i < 10; i = i + 1 { - integer := new(Integer); - integer.Init(i); - list.Insert(integer); + integer := new(Integer) + integer.Init(i) + list.Insert(integer) } - list.Print(); - print("\n"); + r := list.Print() + if r != "9876543210" { + panic(r) + } } |