Stacks - craterdog/go-component-framework GitHub Wiki

Overview

A stack is a collection that provides last-in-first-out (LIFO) semantics. They are often used during recursive processing and therefore allow a maximum capacity to be set.

UML Diagram

A Quick Example

To whet your appetite, here is some short example code that demonstrates the use of stacks.

package main

import (
	fmt "fmt"
	fra "github.com/craterdog/go-component-framework/v7"
)

func main() {
	// Create a new empty stack using the module constructor shortcut.
	var stack = fra.Stack[string]()
	fmt.Println(stack)

	// Add some values to it.
	stack.AddValue("Jack")
	fmt.Println(stack)
	stack.AddValue("King")
	fmt.Println(stack)
	stack.AddValue("Queen")
	fmt.Println(stack)

	// Remove the top value from the stack.
	var top = stack.RemoveLast()
	fmt.Println("Top:", top)
	fmt.Println(stack)

	// Remove all values from the stack.
	stack.RemoveAll()
	fmt.Println(stack)
}

Try it out in the Go Playground...