How would you do it in golang: Stacks

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
This is a series of articles of how we can use golang to code the most commonly used programs, data structures and more.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array.
Lets start with a simple structure definition and a method to initialize the stack.
type stack struct {
items []string
index int
}
// NewStack - function to create a new stack object
func NewStack() *stack {
return &stack{
items: []string{},
index: -1,
}
}
stack is the struct itself, we define two variables - items which stores the items and index which stores the position of the topmost item. index will store the actual array position (starting with 0)
Now, we create the methods - Push, Pop and Top. To keep things simple, we will only give out an empty string in case there is no item in the stack.
func (s *stack) Push(item string) {
s.items = append(s.items, item)
s.index += 1
}
func (s *stack) Pop() string {
if s.index > -1 {
item := s.items[s.index]
s.items = s.items[:len(s.items)-1]
s.index -= 1
return item
}
return ""
}
func (s *stack) Top() string {
if s.index > -1 {
return s.items[s.index]
}
return ""
}
Push function is very simple, we take the item and append it at the last and increase the index. In case of Pop function, we first check if the stack is not empty, then we get the last item, however to remove an item from the slice - we need to get all but the last item s.items[:len(s.items)-1], then we decrease the index and return the item. In case of Top we just return top item if its present.
The complete code (with test run) -
package main
import (
"fmt"
)
type stack struct {
items []string
index int
}
func NewStack() *stack {
return &stack{
items: []string{},
index: -1,
}
}
func (s *stack) Push(item string) {
s.items = append(s.items, item)
s.index += 1
}
func (s *stack) Pop() string {
if s.index > -1 {
item := s.items[s.index]
s.items = s.items[:len(s.items)-1]
s.index -= 1
return item
}
return ""
}
func (s *stack) Top() string {
if s.index > -1 {
return s.items[s.index]
}
return ""
}
func main() {
st := NewStack()
//push hello
st.Push("h")
st.Push("e")
st.Push("l")
st.Push("l")
st.Push("o")
// pop until empty
for {
if item := st.Pop(); item != "" {
fmt.Printf("%s", item)
} else {
break
}
}
}
This article is part of the "How would you do it in golang?" series and happens to be the first post.