https://github.com/lossdev/stack
Lightweight, Simple, Quick, Thread-Safe Golang Stack Implementation
https://github.com/lossdev/stack
golang stack
Last synced: 3 months ago
JSON representation
Lightweight, Simple, Quick, Thread-Safe Golang Stack Implementation
- Host: GitHub
- URL: https://github.com/lossdev/stack
- Owner: lossdev
- License: bsd-3-clause
- Created: 2020-05-19T14:52:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-04-11T20:45:47.000Z (about 5 years ago)
- Last Synced: 2024-06-21T15:19:46.496Z (about 2 years ago)
- Topics: golang, stack
- Language: Go
- Size: 36.1 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stack
[](http://godoc.org/github.com/lossdev/stack)
[](https://goreportcard.com/report/github.com/lossdev/stack)
[](https://codecov.io/github/lossdev/stack/)
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
``` bash
go get -d -v github.com/lossdev/stack
```
## Example
``` Go
package main
import (
"github.com/lossdev/stack"
"log"
"fmt"
)
type foo struct {
bar string
baz bool
}
func main() {
// declare a new Stack 's' with int type (stack.Int)
s := stack.NewStack(stack.Int)
if err := s.Push(1); err != nil {
log.Println(err)
}
if recv, err := stack.ToInt(s.Peek()); err != nil {
log.Println(err)
} else {
fmt.Println(recv)
}
// Adding a member of a different type than what s is declared as will error
if err := s.Push("Hello, World!"); err != nil {
log.Println(err)
}
gs := stack.NewGenericStack()
f := foo{"Hello, World!", true}
gs.Push(f)
if recv, err := gs.Peek(); err != nil {
log.Println(err)
} else {
// type assertion needed
frecv := recv.(foo)
fmt.Printf("GenericStack: {%s, %t}\n", frecv.bar, frecv.baz)
}
}
```
``` bash
$ go run example.go
1
2021/04/07 13:31:52 Push(): expected: [int]; received: [string]
GenericStack: {Hello, World!, true}
```