package
Version:
v1.16.1
Opens a new window with list of versions in this module.
Published: Jul 9, 2025
License: MIT
Opens a new window with license information.
Imports: 1
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
Stack
This package provides a generic stack data structure for Go, supporting any type.
Features
- Generic: Works with any type (
Stack[T any]).
- Push: Add an element to the top of the stack.
- Pop: Remove and return the top element (returns zero value and
false if empty).
- Peek: View the top element without removing it.
- IsEmpty: Check if the stack is empty.
- Size: Get the number of elements in the stack.
Example
s := stack.New[int]()
s.Push(10)
s.Push(20)
val, ok := s.Pop() // val == 20, ok == true
Documentation
¶
type Stack[T any] struct {
}
Stack is a generic thread-safe stack data structure.
IsEmpty returns true if the stack is empty.
Peek returns the top element without removing it.
Returns the zero value and false if the stack is empty.
Pop removes and returns the top element of the stack.
Returns the zero value and false if the stack is empty.
func (s *Stack[T]) Push(value T)
Push adds an element to the top of the stack.
Size returns the number of elements in the stack.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.