Search code examples
go

Looking for reasonable stack implementation in golang?


So far my naive approach is

type stack []int

func (s *stack) Push(v int) {
    *s = append(*s, v)
}

func (s *stack) Pop() int {
    res:=(*s)[len(*s)-1]
    *s=(*s)[:len(*s)-1]
    return res
}

it works - playground, but looks ugly and has too much dereferencing. Can I do better?


Solution

  • It's a matter of style and personal taste, your code is fine (apart from not being thread safe and panicking if you pop from an empty stack). To simplify it a bit you can work with value methods and return the stack itself, it's slightly more elegant to some tastes. i.e.

    type stack []int
    
    func (s stack) Push(v int) stack {
        return append(s, v)
    }
    
    func (s stack) Pop() (stack, int) {
        // FIXME: What do we do if the stack is empty, though?
    
        l := len(s)
        return  s[:l-1], s[l-1]
    }
    
    
    func main(){
        s := make(stack,0)
        s = s.Push(1)
        s = s.Push(2)
        s = s.Push(3)
    
        s, p := s.Pop()
        fmt.Println(p)
    
    }
    

    Another approach is to wrap it in a struct, so you can also easily add a mutex to avoid race conditions, etc. something like:

    type stack struct {
         lock sync.Mutex // you don't have to do this if you don't want thread safety
         s []int
    }
    
    func NewStack() *stack {
        return &stack {sync.Mutex{}, make([]int,0), }
    }
    
    func (s *stack) Push(v int) {
        s.lock.Lock()
        defer s.lock.Unlock()
    
        s.s = append(s.s, v)
    }
    
    func (s *stack) Pop() (int, error) {
        s.lock.Lock()
        defer s.lock.Unlock()
    
    
        l := len(s.s)
        if l == 0 {
            return 0, errors.New("Empty Stack")
        }
    
        res := s.s[l-1]
        s.s = s.s[:l-1]
        return res, nil
    }
    
    
    func main(){
        s := NewStack()
        s.Push(1)
        s.Push(2)
        s.Push(3)
        fmt.Println(s.Pop())
        fmt.Println(s.Pop())
        fmt.Println(s.Pop())
    }