module

package module
v8.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 14 Imported by: 33

Documentation

Overview

Package "module" defines the global functions provided by this module. The functions fill is some gaps in the Go language and native libraries. They make it easy to perform the things that should be simple in Go but aren't for various reasons. The functions cover the following areas:

  • File System
  • Sequences (arrays, slices and maps)
  • Controllers (finite state machines)
  • Strings
  • Codex
  • Random
  • Reflection

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArraySize

func ArraySize[V any](
	array []V,
) uint

ArraySize[V any] returns the current size of the specified array.

func ArraysAreEqual

func ArraysAreEqual[V comparable](
	first []V,
	second []V,
) bool

ArraysAreEqual[V comparable] determines whether or not the specified arrays have the same elements.

func Base16Decode

func Base16Decode(
	encoded string,
) []byte

func Base16Encode

func Base16Encode(
	bytes []byte,
) string

func Base32Decode

func Base32Decode(
	encoded string,
) []byte

func Base32Encode

func Base32Encode(
	bytes []byte,
) string

func Base64Decode

func Base64Decode(
	encoded string,
) []byte

func Base64Encode

func Base64Encode(
	bytes []byte,
) string

func CardinalToRelative

func CardinalToRelative(
	cardinal int,
	size uint,
) int

func CombineArrays

func CombineArrays[V any](
	first []V,
	second []V,
) []V

CombineArrays[V any] returns a new array containing the concatenation of the specified arrays.

func CombineMaps

func CombineMaps[K comparable, V any](
	first map[K]V,
	second map[K]V,
) map[K]V

CombineMaps[K comparable, V any] returns a new map containing the concatenation of the specified maps.

func CopyArray

func CopyArray[V any](
	array []V,
) []V

CopyArray[V any] returns a copy of the specified array with the same size and elements as the specified array. The result is not a deep copy.

func CopyMap

func CopyMap[K comparable, V any](
	map_ map[K]V,
) map[K]V

CopyMap[K comparable, V any] returns a copy of the specified map with the same size and key-value pairs as the specified map. The result is not a deep copy.

func Format

func Format(
	value any,
) string

Format returns a canonical string describing any value in Go. It takes into account the nesting depth of all compound values (i.e. arrays, maps and structs) and indents each four spaces per nesting level. This function does not call the Go "Stringer" interface on any of the values even if the value supports it since this the "Stringer" interface does not take into account the nesting depth.

That said, the Go "Stringer" interface can be safely implemented using the Format function as follows:

 func (v *MyClass) String() string {
	 return uti.Format(v)
 }

There should be no risk of infinite recursion from Format() calling String() calling Format() calling String()...

func HomeDirectory

func HomeDirectory() string

HomeDirectory returns the home directory path for the current user.

func ImplementsInterface

func ImplementsInterface(
	value any,
	pointer any,
) bool

ImplementsInterface checks whether or not the specified value implements the specified interface. It can be used as follows:

type MyInterface interface {
	DoSomething()
}

type MyStruct struct{}

func (v *MyStruct) DoSomething() {}

func main() {
	var myValue any = &MyStruct{}
	var myInterface *MyInterface
	if ImplementsInterface(myValue, myInterface) {
		var actual MyInterface = myValue.(MyInterface)
		fmt.Println("myValue implements MyInterface:", actual)
	}
}

NOTE: The interface argument that gets passed into the ImplementsInterface() call must be a pointer to the interface since the argument is of type any.

func IsDefined

func IsDefined(
	value any,
) bool

IsDefined checks whether or not the specified value is defined in a meaningful way. Empty strings and nil pointers are considered as being undefined.

func IsUndefined

func IsUndefined(
	value any,
) bool

IsUndefined checks whether or not the specified value is undefined. Empty strings and nil pointers are considered as being undefined.

func MakeAllCaps

func MakeAllCaps(
	mixedCase string,
) string

// Strings

/* MakeAllCaps modifies the specified mixed case string into a corresponding all uppercase string using "_"s to separate the words found in the mixed case string.

func MakeDirectory

func MakeDirectory(
	directory string,
)

MakeDirectory creates all directories in the specified file system directory path.

func MakeLowerCase

func MakeLowerCase(
	mixedCase string,
) string

MakeLowerCase modifies the specified mixed case string into a corresponding string starting with a lowercase letter. All other letters remain unchanged.

func MakePlural

func MakePlural(
	mixedCase string,
) string

MakePlural attempts to modify the specified mixed case string to make it plural. It does not use much intelligence to attempt this but gets most cases correct.

func MakeSnakeCase

func MakeSnakeCase(
	mixedCase string,
) string

MakeSnakeCase modifies the specified mixed case string into a corresponding all lowercase string using "-"s to separate the words found in the mixed case string.

func MakeUpperCase

func MakeUpperCase(
	mixedCase string,
) string

MakeUpperCase modifies the specified mixed case string into a corresponding string starting with an uppercase letter. All other letters remain unchanged.

func MapSize

func MapSize[K comparable, V any](
	map_ map[K]V,
) uint

MapSize[K comparable, V any] returns the current size of the specified map.

func MapsAreEqual

func MapsAreEqual[K comparable, V comparable](
	first map[K]V,
	second map[K]V,
) bool

MapsAreEqual[K comparable, V comparable] determines whether or not the specified maps have the same key-value pairs. This function is deterministic even though Go maps are not.

func PathExists

func PathExists(
	path string,
) bool

PathExists checks whether or not the specified file system path is defined. An empty string or a nil pointer is considered to be undefined.

func RandomBoolean

func RandomBoolean() bool

func RandomBytes

func RandomBytes(
	size uint,
) []byte

func RandomOrdinal

func RandomOrdinal(
	maximum uint,
) uint

func RandomProbability

func RandomProbability() float64

func ReadDirectory

func ReadDirectory(
	directory string,
) []string

ReadDirectory returns an array containing the filenames of the files in the specified directory.

func ReadFile

func ReadFile(
	filename string,
) string

ReadFile returns the contents of the specified file from the file system as a string.

func RelativeToCardinal

func RelativeToCardinal(
	relative int,
	size uint,
) int

Relative indexing allows an index to be a relative positive (or negative) ordinal index of a value in a sequence. The indices are ordinal rather than cardinal (zero-based) which never really made sense except for pointer offsets. What is the "zeroth value" in a sequence anyway? It's the "first value", right? So we start a fresh...

The relative indexing approach allows for positive indices starting at the beginning of a sequence—and negative indices starting at the end of the sequence, as follows:

    1           2           3             N
[value 1] . [value 2] . [value 3] ... [value N]
   -N        -(N-1)      -(N-2)          -1

Notice that because the indices are ordinal based, the positive and negative indices are symmetrical. A relative index can NEVER be zero.

RelativeToCardinal transforms a relative (ordinal-based) index into the corresponding zero-based index. The following transformation is performed:

[-size..-1] or [1..size] => [0..size)

Notice that the specified relative index cannot be zero since zero is NOT an ordinal number.

func RemakeDirectory

func RemakeDirectory(
	directory string,
)

RemakeDirectory recursively removes all files and subdirectories from the specified file system directory path.

func RemovePath

func RemovePath(
	path string,
)

RemovePath recursively removes all directories and files found in the specified file system path.

func RenamePath

func RenamePath(
	oldPath string,
	newPath string,
)

RenamePath renames an old file system path to a new one.

func ReplaceAll

func ReplaceAll(
	template string,
	name string,
	value string,
) string

ReplaceAll replaces each instance of the specified name embedded in angle brackets (i.e. "<" and ">") with the specified value throughout the specified template string. The way the name is shown in the brackets determines what transformations are done on the value prior to the substitution as follows:

  • <anyCaseName> -> value {leave value as is}
  • <lowerCaseName_> -> lowerCaseValue[_] {convert value to unique ⃰lower case}
  • <~lowerCaseName> -> lowerCaseValue {convert value to lower case}
  • <~snake-case-name> -> snake-case-value {convert value to snake case}
  • <~UpperCaseName> -> UpperCaseValue {convert value to upper case}
  • <~ALL_CAPS_NAME> -> ALL_CAPS_VALUE {convert value to all caps with _'s}

⃰A trailing underscore "_" is added if the value collides with a Go keyword.

func WriteFile

func WriteFile(
	filename string,
	source string,
)

WriteFile writes the specified source string as the contents of the specified file in the file system.

Types

type Event

type Event string

Event is a constrained type representing an event type in a state machine. Using a string type for an event makes it easier to print out in a human readable way.

type Ratcheted added in v8.2.0

type Ratcheted[V any] interface {
	ToStart()
	ToEnd()
	IsEmpty() bool
	GetSize() uint
	GetSlot() uint
	SetSlot(
		slot uint,
	)
	HasPrevious() bool
	GetPrevious() V
	HasNext() bool
	GetNext() V
}

Ratcheted[V any] is an interface that declares the complete set of methods that must be supported by each iterator.

func Iterator

func Iterator[V any](
	array []V,
) Ratcheted[V]

Iterator[V any] returns an iterator over the specified array. The iterator moves forwards or backwards over the array landing in the slots between the items in the array. From a given slot the previous and next items are accessible to the iterator.

type State

type State string

State is a constrained type representing a state in a state machine. Using a string type for a state makes it easier to print out in a human readable way.

type Stateful added in v8.2.0

type Stateful interface {
	ProcessEvent(
		event Event,
	) State
	GetState() State
	SetState(
		state State,
	)
	GetEvents() []Event
	GetTransitions() map[State]Transitions
}

Stateful is an interface that declares the complete set of principal, attribute and aspect methods that must be supported by each controller.

A controller implements a finite state machine with possible event types. It enforces the possible states of the state machine and allowed transitions between states given a finite set of possible event types. It implements a finite state machine with the following table structure:

        -----------------------------------
events: | [event1,  event2,  ... eventM ] |
        -----------------------------------
state1: | [invalid, state2,  ... invalid] |
state2: | [state3,  stateN,  ... invalid] |
        |                ...              |
stateN: | [state1,  invalid, ... state3 ] |
        -----------------------------------

The first row of the state machine defines the possible events that can occur. Each subsequent row defines a state and the possible transitions from that state to the next state for each possible event. Transitions marked as "invalid" cannot occur. The state machine always starts in the first state of the finite state machine (e.g. state1).

func Controller

func Controller(
	events []Event,
	transitions map[State]Transitions,
	initialState State,
) Stateful

Controller returns a state machine that can be used to control an automaton.

type Transitions

type Transitions []State

Transitions is a constrained type representing a row of states in a state machine.

Jump to

Keyboard shortcuts

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