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// Test general operation using a list implementation.
8
9package main
10
11type Item interface {
12	Print() string
13}
14
15type ListItem struct {
16	item Item
17	next *ListItem
18}
19
20type List struct {
21	head *ListItem
22}
23
24func (list *List) Init() {
25	list.head = nil
26}
27
28func (list *List) Insert(i Item) {
29	item := new(ListItem)
30	item.item = i
31	item.next = list.head
32	list.head = item
33}
34
35func (list *List) Print() string {
36	r := ""
37	i := list.head
38	for i != nil {
39		r += i.item.Print()
40		i = i.next
41	}
42	return r
43}
44
45// Something to put in a list
46type Integer struct {
47	val int
48}
49
50func (this *Integer) Init(i int) *Integer {
51	this.val = i
52	return this
53}
54
55func (this *Integer) Print() string {
56	return string(this.val + '0')
57}
58
59func main() {
60	list := new(List)
61	list.Init()
62	for i := 0; i < 10; i = i + 1 {
63		integer := new(Integer)
64		integer.Init(i)
65		list.Insert(integer)
66	}
67
68	r := list.Print()
69	if r != "9876543210" {
70		panic(r)
71	}
72}
73