How to Implement Stack in Go: Simple Guide with Example
In Go, you can implement a stack using a slice and define methods like
Push, Pop, and Peek to add, remove, and view elements. This approach uses Go's built-in slice type to manage the stack efficiently.Syntax
A stack in Go can be implemented as a struct containing a slice. You define methods to manipulate the stack:
- Push: Add an element to the top.
- Pop: Remove and return the top element.
- Peek: View the top element without removing it.
This uses Go slices which grow dynamically.
go
type Stack struct { items []int } func (s *Stack) Push(item int) { s.items = append(s.items, item) } func (s *Stack) Pop() (int, bool) { if len(s.items) == 0 { return 0, false } lastIndex := len(s.items) - 1 item := s.items[lastIndex] s.items = s.items[:lastIndex] return item, true } func (s *Stack) Peek() (int, bool) { if len(s.items) == 0 { return 0, false } return s.items[len(s.items)-1], true }
Example
This example shows how to create a stack, push items, peek at the top, and pop items until empty.
go
package main import ( "fmt" ) type Stack struct { items []int } func (s *Stack) Push(item int) { s.items = append(s.items, item) } func (s *Stack) Pop() (int, bool) { if len(s.items) == 0 { return 0, false } lastIndex := len(s.items) - 1 item := s.items[lastIndex] s.items = s.items[:lastIndex] return item, true } func (s *Stack) Peek() (int, bool) { if len(s.items) == 0 { return 0, false } return s.items[len(s.items)-1], true } func main() { var stack Stack stack.Push(10) stack.Push(20) stack.Push(30) top, _ := stack.Peek() fmt.Println("Top item:", top) for { item, ok := stack.Pop() if !ok { break } fmt.Println("Popped:", item) } }
Output
Top item: 30
Popped: 30
Popped: 20
Popped: 10
Common Pitfalls
Common mistakes when implementing a stack in Go include:
- Not checking if the stack is empty before popping or peeking, which can cause runtime errors.
- Modifying the slice incorrectly, such as forgetting to update the slice after popping.
- Using a fixed-size array instead of a slice, which limits stack size and flexibility.
Always check for empty stack and use slices for dynamic size.
go
/* Wrong: popping without checking empty stack */ func (s *Stack) Pop() int { lastIndex := len(s.items) - 1 item := s.items[lastIndex] // panic if empty s.items = s.items[:lastIndex] return item } /* Right: check empty before popping */ func (s *Stack) Pop() (int, bool) { if len(s.items) == 0 { return 0, false } lastIndex := len(s.items) - 1 item := s.items[lastIndex] s.items = s.items[:lastIndex] return item, true }
Quick Reference
| Operation | Description | Go Implementation |
|---|---|---|
| Push | Add element to top | s.items = append(s.items, item) |
| Pop | Remove and return top element | item := s.items[len(s.items)-1]; s.items = s.items[:len(s.items)-1] |
| Peek | View top element without removing | item := s.items[len(s.items)-1] |
| IsEmpty | Check if stack is empty | len(s.items) == 0 |
Key Takeaways
Use a slice inside a struct to implement a stack in Go for dynamic sizing.
Always check if the stack is empty before popping or peeking to avoid errors.
Use methods Push, Pop, and Peek to manage stack operations cleanly.
Slices make stack implementation simple and efficient in Go.
Avoid fixed-size arrays to keep your stack flexible.