Stack Using Golang Generics

 


With the recent introduction of generics in Golang, they have proved themselves not only elegant and simple, but also very efficient, check out the performance characteristics here. Now it is time to demonstrate their power in creating flexible data structures in this post. As promised in a later post, we are going to implement a stack in this post. The rest of this post is organized as follows:

  1. what is a stack
  2. stack methods
  3. implementation in Golang
  4. testing our stack



What is a Stack

Stacks are simple data structures that powers a great deal of modern computational systems like function calls in compilers themselves, LRU policy of memory block replacements, bracket matching problems and many many more. Stacks were first introduced in 1964 by Alan M. Turing and they were gaining popularity ever since.






A stack acts as a first in last out data structure most commonly known as LIFO. In layman terms this means it is a data store where you can insert an element at a time, but you can retrieve these elements back in reverse order. For example, if we insert these integers in order to a stack (Push them) 1,2,3,4,5 we can retrieve (Pull) them in reverse order i.e. 5,4,3,2,1. In other words, a stack act as a pile of books, you can always add books to the top of this book stack, but you can access the most recent that you but in.  If you need to find more, click here.


Stack Methods

Stacks usually poses four methods, these methods are:
  1. Init
  2. Pop
  3. Push
  4. Peak

The "Init" method usually used to instantiate the stack i.e allocate memory to be used in the stack. In strongly typed languages like Golang, it is required to define the type of elements that goes into the stack. In addition to the memory allocations, it is required for the stack to be thread safe, if it is going to run in a multi-threading environment. 


The "Pop" method simply removes the last element of the stack and return it to its caller. Thus achieving the purpose of LIFO.

The "Push" method simply appends an item to the top of the stack, to be retrieved later using the "Pop" method. 

The "Peak" method acts very similarly to the "Pop" method but without actually removing an item from the stack. 

Stack Implementation in Golang

Here goes the interesting stuff. First we create a struct that will represent our stack. This struct will contain a slice that will represent our data, a locking mechanism for safe threading. It's required to keep these struct members private so no one miss with them from outside our package. These Items will be manipulated by our receiver functions only. So our struct will look something like this:


So in order to instantiate this struct and use it, we might provide a method to do that. This method will return a pointer to a stack so the caller can invoke the struct methods on them. The Other Three methods required by a stack to function probably are displayed below. Notice here that we do not allow simulations reads/writes to our data structure.


Testing our Stack

To test our stack, we first insert (Push) the items 0,1,2,3,4 and use the Pull method to retrieve them back. Here is the code snippet:

Happy Generic Stacks :)

Comments

Popular posts from this blog

Golang Generics Performance Evaluation and Implications

Generics In Golang