Go Struct And Stack Data Structure

Minh-Phuc Tran on Nov 14, 2020

go

programming

beginners

datastructure

Stack

Stack is a LIFO (last-in-first-out) data structure. It's usually used to convert a recursive solution into an iterative solution.

ℹī¸ It is asked a lot during technical interviews at big tech companies.

image.png


Golang implementation

Interestingly enough, there's no built-in implementation of Stack in Go, you'll have to implement it based on a linked list.

import "container/list" // import a package in Golang

type Stack struct { // how a struct is defined
    list *list.List // lowercase var/function is private
}

func (s *Stack) Push(val int) { // upperccase var/function is public
    s.list.PushBack(val)
}

func (s *Stack) Pop() int { // how a struct's function is defined
    item := s.list.Back()
    val := item.Value.(int)
    s.list.Remove(item)
    return val
}

func (s *Stack) Clean() {
    for s.list.Len() > 0 { // there's no 'while' in Golang, only 'for'
        s.list.Remove(s.list.Back())
    }
}

Subscribe to the newsletter

Get emails from me about software development, SaaS, and early access to new articles.