A stack is a linearlyordered data structure in which the addition of new elements — or the deletion of an existing element — always takes place at the same end. This means that the last element inserted would be the first one treated before moving on to the next element. This specific order is called LIFO (Last in, First out). In relation to this definition, the term “linear” refers to data elements being traversed sequentially; and the term “ordered” refers to sequence counts being inserted.

In this article, I will show you how to implement a stack in Golang using slice.

A stack data structure has its own operations and properties as follows:

  • Push operation: this adds a new element to the stack
  • Pop operation: this removes the latest pushed element to the top of the stack.
  • peek or top property: this is the element at the top of the stack

This situation can be compared to a stack of plates in a cafeteria where every new plate added to the stack is added at the top. Similarly, every new plate taken off the stack is also taken from the top of the stack.

#golang #concurrency #slice #stack #data-structures

Slice Based Stack Implementation in Golang
5.00 GEEK