stack

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2021 License: BSD-3-Clause Imports: 2 Imported by: 1

README

stack

GoDoc Go Report Card Go Coverage

Lightweight, Simple, Quick, Thread-Safe Golang Stack Implementation

Purpose

Provide a fast, thread safe, and generic Golang Stack API with minimal external linkage and maximum performance and usability.

Installation

go get -d -v github.com/lossdev/stack

Example

package main

import (
	"fmt"
	"log"
	"strconv"
	"github.com/lossdev/stack"
)

func main() {
	s := stack.NewStack()
	s.Push("foo")
	s.Push("bar")
	s.Push("baz")
	fmt.Println("Size: " + strconv.Itoa(s.Size()))
	val, err := s.Pop()
	if err != nil {
		log.Println(err)
	}
	fmt.Println("Pop: " + val.(string))
	val, err = s.Pop()
	if err != nil {
		log.Println(err)
	}
	fmt.Println("Pop: " + val.(string))
	val, err = s.Peek()
	if err != nil {
		log.Println(err)
	}
	fmt.Println("Peek: " + val.(string))
	val, err = s.Pop()
	if err != nil {
		log.Println(err)
	}
	fmt.Println("Pop: " + val.(string))
	fmt.Println("Size: " + strconv.Itoa(s.Size()))
	s.Push("foo")
	s.Push("bar")
	s.Push("baz")
	s.Drain()
	fmt.Println("Size: " + strconv.Itoa(s.Size()))
	s.Push("foo")
	fmt.Println("Size: " + strconv.Itoa(s.Size()))
	val, err = s.Peek()
	if err != nil {
		log.Println(err)
	}
	fmt.Println("Peek: " + val.(string))
	s.Drain()
	_, err = s.Peek()
	if err != nil {
		log.Println(err)
	}
}
$ go run stackTest.go
Size: 3
Pop: baz
Pop: bar
Peek: foo
Pop: foo
Size: 0
Size: 0
Size: 1
Peek: foo
2020/05/19 10:42:17 Peek from Empty Stack

Documentation

Overview

Package stack aims to provide a fast, simple, thread safe, and generic golang stack library to the public. It uses as few linking libraries as possible, and the ones it does are common standard libraries for a smaller code footprint and greater performance.

Each Stack is generically compatible, meaning that any data can be added and retrieved to/from a Stack. Locks are also used on Stack objects when modifying the Stack through using the Pop or Push methods, so that Stack objects are thread safe.

Package stack aims to provide a fast, simple, thread safe, and generic golang stack library to the public. It uses as few linking libraries as possible, and the ones it does are common standard libraries for a smaller code footprint and greater performance. Each Stack is generically compatible, meaning that any data can be added and retrieved to/from a Stack. Locks are also used on Stack objects when modifying the Stack through using the Pop or Push methods, so that Stack objects are thread safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack stores the lock and data members of the stack.

func NewStack

func NewStack() *Stack

NewStack is the constructor method that initializes a new, empty stack and returns it.

func (*Stack) Drain

func (s *Stack) Drain()

Drain removes all elements that are currently in the stack.

func (*Stack) Peek

func (s *Stack) Peek() (interface{}, error)

Peek looks at, but does not remove, the top element of the stack. It returns an error if the stack is empty, or the value of the top element if it isn't.

func (*Stack) Pop

func (s *Stack) Pop() (interface{}, error)

Pop attempts to remove a value from the top of the stack. It will return an error if the stack is empty, or the value of the top element if it isn't.

func (*Stack) Push

func (s *Stack) Push(val interface{})

Push will add a new value to the top of the stack.

func (*Stack) Size

func (s *Stack) Size() int

Size returns the current size of the stack. If the stack is empty, it will simply return 0, not an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL