Skip to content
Derk Norton edited this page Mar 16, 2023 · 2 revisions

A stack is a collection that supports last-in-first-out (LIFO) semantics. Each value in the stack is a component.

UML Diagram

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

package main

import (
	fmt "fmt"
	bal "github.com/bali-nebula/go-component-framework/v2/bali"
)

func main() {
	// Create a new stack from a string containing Bali Document Notation™.
	var stack = bal.Stack(`[
    $first
    $second
    $third
]($type: /bali/types/collections/Stack/v1)`)
	fmt.Println("The initial stack:", bal.FormatEntity(stack))

	// Peek at the value on top of the stack.
	var top = stack.GetTop()
	fmt.Println("The top of the stack:", bal.FormatComponent(top))

	// Remove the value from the top of the stack.
	var value = stack.RemoveTop()
	fmt.Println("The value from the top of the stack:", bal.FormatComponent(value))
	fmt.Println("The remaining stack:", bal.FormatEntity(stack))

	// Add another value to the top of the stack.
	stack.AddValue(bal.Component(`$fourth`))
	fmt.Println("The updated stack:", bal.FormatEntity(stack))

	// Remove the value from the top of the stack.
	value = stack.RemoveTop()
	fmt.Println("The value from the top of the stack:", bal.FormatComponent(value))
	fmt.Println("The remaining stack:", bal.FormatEntity(stack))

	// Remove all values from the stack.
	stack.RemoveAll()
	fmt.Println("The empty stack:", bal.FormatEntity(stack))
}