Implementing Stacks In Go

April 2, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

👋 Welcome Gophers, in this tutorial, we are going to be looking at how we can effectively implement the Stack data structure in Go. We’ll look closely at how we can implement the key methods Push, Pop, and Peek as well as the helper methods that give us information such as the size of the stack and if it’s empty.

Stack Implementation

Let’s dive in to VS Code and start implementing our Stack data structure.

We’ll start off with the definition of our Stack struct which will contain a slice of type int:

package main

type Stack struct {
  Elements []int

Again, this is going to be very similar in nature to our Queue implementation, the only differences are how we access the data and the LIFO pattern that the Stack data structure adheres to.


Now that we have our Stack struct defined, let’s set about implementing the Push function which will handle pushing new elements on to the top of our stack.

func (s *Stack) Push(elem int) {
  s.Elements = append(s.Elements, elem)

Perfect, we now have a method in place that will allow us to append new elements on to the top of our stack.


Let’s now have a look at how we can consume or pop elements off the top of our stack. We’ll be using the slice notation in order to retrieve the last element from the stack and to also update our element slice to remove the popped-off element:

func (s *Stack) Pop(elem int) (int, error) {
  if len(s.Elements) == 0 {
    return 0, errors.New("Stack is Empty")
  } else {
    lastElemIndex := len(s.Elements) - 1
    lastElement, s.Elements = s.Elements[lastElemIndex], s.Elements[:lastElemIndex]
    return lastElement, nil

Cool, so we have been able to first check to see if the stack is empty, if it is then we return an error. If it has elements on the stack then we calculate the index of the last element in the slice before then using this index to retrieve the last element and to also update the slice to remove this last element.

Awesome, we now have a very simple stack implementation up and running!


Our stack implementation wouldn’t be complete without first tackling some of the helper methods that our consumers may wish to use. Let’s sort this out now by implementing the Peek, Length and IsEmpty methods now:

We’ll start with the Peek method which is going to return the element on the top of our stack but not update the underlying Elements slice:

func (s *Stack) Peek() (int, error) {
  if len(s.Elements) == 0 {
    return 0, errors.New("Stack is Empty")
  } else {
    return s.Elements[len(s.Elements)-1], nil

You’ll notice this is a far more condensed implementation of the Pop method that we’ve previously defined. We need to do the check to see if the stack is empty first, if it isn’t empty then we can calculate and fetch the last element within our return statement.


Two more methods to implement. Let’s now take a look at implementing the Length method which will return the length of the slice within our stack. Effectively we are counting how many pancakes we have left to eat to continue with the analogy from the previous video;

func (s *Stack) Length() int {
  return len(s.Elements)


And finally, let’s implement the IsEmpty method which will return a bool depending on whether or not our stack has any pancakes left!

func (s *Stack) IsEmpty() bool {
  return len(s.Elements) == 0

Perfect, we now have all the auxilliary methods defined for our own simple Stack implementation in Go!


Validate your knowledge with this mini-quiz:


Perfect, so in this video, we looked at how we could effectively implement the Stack data structure in Go! We’ve covered how to implement all the necessary methods that a Stack implementation needs and how the underlying slice should be updated when we perform actions such as poping and pushing.

In the next section of this course, we are going to be looking at how we can implement the linked-list data structure in Go.