Go/Lists: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
package main | package main | ||
import "container/list" | import ( | ||
"fmt" | |||
"container/list" | |||
) | |||
func main() { | func main() { | ||
| Line 16: | Line 19: | ||
e4 := l.PushBack(4) | e4 := l.PushBack(4) | ||
e1 := l.PushFront(1) | e1 := l.PushFront(1) | ||
// State of list is [1, 4] | |||
l.InsertBefore(3, e4) | l.InsertBefore(3, e4) | ||
l.InsertAfter(2, e1) | l.InsertAfter(2, e1) | ||
// State of list is [1, 2, 3, 4] | |||
// Iterate over list | // Iterate over list | ||
for e := l.Front(); e != nil; e = e.Next() { | for e := l.Front(); e != nil; e = e.Next() { | ||
fmt.Println(e.Value) | |||
} | } | ||
} | } | ||
Revision as of 14:32, 13 December 2018
To use lists in Go, use the "container/lists" package. This will import a linked list object. import "container/list"
Link: https://golang.org/pkg/container/list/
package main
import (
"fmt"
"container/list"
)
func main() {
// Initialize list
l := list.New()
// Populate list
e4 := l.PushBack(4)
e1 := l.PushFront(1)
// State of list is [1, 4]
l.InsertBefore(3, e4)
l.InsertAfter(2, e1)
// State of list is [1, 2, 3, 4]
// Iterate over list
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
Flags
| Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|