to be honest, if i need a stack, i usually implement it for the type
in question, or inline, as it's only a very small number of lines:
type stack []T
func (s *stack) push(t T) {
*s = append(*s, t)
}
func (s *stack) pop() T {
n := len(*s)
t := (*s)[n-1]
*s = (*s)[:n-1]
return t
}
an alternative is to use container/list.
the same applies to queues - container/list works well for a queue;
i'd usually just inline the below implementation.
type queue list.List
func newQueue() *queue {
return (*queue)(list.New())
}
func (q *queue) push(t T) {
l := (*list.List)(q)
l.PushBack(t)
}
func (q *queue) pop() T {
l := (*list.List)(q)
return l.Remove(l.Front()).(T)
}
func (q *queue) len() int {
return (*list.List)(q).Len()
}
yes, interface{} has an overhead, but
it's not too bad for most purposes. if you find it's a bottleneck
in a particular case, it's usually ok to copy
the code and specialise the types.