Use a slice as a stack:
var stack []int
// Push
stack = append(stack, 1)
stack = append(stack, 2)
// Pop
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
Append pushes. Reslicing pops. This is efficient because append is amortized O().
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Push and pop
Use a slice as a stack:
var stack []int
// Push
stack = append(stack, 1)
stack = append(stack, 2)
// Pop
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
Append pushes. Reslicing pops. This is efficient because append is amortized O().