mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-23 05:06:01 +03:00
37 lines
456 B
Go
37 lines
456 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type stackEntry struct {
|
|
next *stackEntry
|
|
value interface{}
|
|
}
|
|
|
|
type stack struct {
|
|
top *stackEntry
|
|
}
|
|
|
|
func (s *stack) Push(v interface{}) {
|
|
var e stackEntry
|
|
e.value = v
|
|
e.next = s.top
|
|
s.top = &e
|
|
}
|
|
|
|
func (s *stack) Pop() interface{} {
|
|
if s.top == nil {
|
|
return nil
|
|
}
|
|
v := s.top.value
|
|
s.top = s.top.next
|
|
return v
|
|
}
|
|
|
|
func main() {
|
|
s := &stack{}
|
|
s.Push("one")
|
|
s.Push("two")
|
|
s.Push("three")
|
|
fmt.Printf("%#v", s)
|
|
}
|