module

package module
v7.17.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 9 Imported by: 39

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
  • Composites (arrays, slices and maps)
  • Strings
  • Reflection

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArraySize added in v7.16.0

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 CardinalToRelative added in v7.14.0

func CardinalToRelative(
	cardinal int,
	size uint,
) int

func CombineArrays added in v7.7.0

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 added in v7.7.0

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 added in v7.7.0

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

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 added in v7.16.0

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 ReadDirectory added in v7.12.0

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 added in v7.14.0

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 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

This section is empty.

Jump to

Keyboard shortcuts

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