package main import "fmt" type stack struct { items []string } func (s stack) push(v string) stack { s.items = append(s.items, v) return s } func (s stack) pop() (stack, string) { last := len(s.items) - 1 top := s.items[last] s.items = s.items[:last] return s, top } func (s stack) peek() (string, bool) { if len(s.items) == 0 { return "", false } return s.items[len(s.items)-1], true } func (s stack) size() int { return len(s.items) } func main() { s := stack{} s = s.push("a") s = s.push("b") s = s.push("c") fmt.Println(s.size()) // 3 top, ok := s.peek() fmt.Println(top, ok) // c true s, top = s.pop() fmt.Println(top) // c s, top = s.pop() fmt.Println(top) // b fmt.Println(s.size()) // 1 }