-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearStory2.go
executable file
·73 lines (53 loc) · 1.38 KB
/
linearStory2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// add a function to insert new page after a page
// delete a page from the list
package main
import (
"fmt"
)
type storyPage struct {
text string
nextPage *storyPage
}
func playStory(page *storyPage) {
for page != nil {
fmt.Println(page.text)
page = page.nextPage
}
}
func insertPage(page *storyPage, beforePage *storyPage) {
if beforePage.nextPage != nil {
page.nextPage = beforePage.nextPage
}
beforePage.nextPage = page
}
func (page *storyPage) addToEnd(text string) {
pageToAdd := &storyPage{text, nil}
for page.nextPage != nil {
page = page.nextPage
}
page.nextPage = pageToAdd
}
func (page *storyPage) addAfter(text string) {
newPage := &storyPage{text, page.nextPage}
page.nextPage = newPage
}
// Delete
// delete one page from the book and keep the coherency
func deletePage(page *storyPage) {
if page.nextPage != nil {
*page = *page.nextPage
return
}
// just set the value to the zeroth value object
*page = storyPage{}
}
func main() {
page1 := storyPage{"It was a dark and stormy night.", nil}
page1.addToEnd("You are alone, and you need to find the sacred helment before the bad guys do.")
page1.addToEnd("You see a troll ahead")
page1.addAfter("Testing")
playStory(&page1)
// Functions - has a return value - may also execute
// Procedures - has no return value, just executes
// Methods - functions attached to a struct/object/etc
}