mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-28 15:56:04 +03:00
41 lines
526 B
Go
41 lines
526 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 (s *stack) GetTop() *stackEntry {
|
|
return s.top
|
|
}
|
|
|
|
func main() {
|
|
s := &stack{}
|
|
s.Push("one")
|
|
s.Push("two")
|
|
s.Push("three")
|
|
fmt.Printf("%#v", s.GetTop().Next)
|
|
}
|