LIFO container in Go

go

Solution

There is no built-in stack type in Go or the standard libraries. However, you can add Push and Pop methods to a slice pretty simply (not unlike the existing methods on lists in python).

type Stack []YourType

func (s *Stack) Push(v YourType) {
    *s = append(*s, v)
}

func (s *Stack) Pop() YourType {
    ret := (*s)[len(*s)-1]
    *s = (*s)[0 : len(*s)-1]

    return ret
}

Pretty straightforward

Problem

I need to use LIFO stack container with push and pop operations, but `container` package doesn't have one. Is it supposed to be writen ad-hoc by every programmer, or there is a way to use other data structure as stack (like list in python)?

Original source

Related problems