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 ¶
- func ArraySize[V any](array []V) uint
- func ArraysAreEqual[V comparable](first []V, second []V) bool
- func CardinalToRelative(cardinal int, size uint) int
- func CombineArrays[V any](first []V, second []V) []V
- func CombineMaps[K comparable, V any](first map[K]V, second map[K]V) map[K]V
- func CopyArray[V any](array []V) []V
- func CopyMap[K comparable, V any](map_ map[K]V) map[K]V
- func Format(value any) string
- func HomeDirectory() string
- func ImplementsInterface(value any, pointer any) bool
- func IsDefined(value any) bool
- func IsUndefined(value any) bool
- func MakeAllCaps(mixedCase string) string
- func MakeDirectory(directory string)
- func MakeLowerCase(mixedCase string) string
- func MakePlural(mixedCase string) string
- func MakeSnakeCase(mixedCase string) string
- func MakeUpperCase(mixedCase string) string
- func MapSize[K comparable, V any](map_ map[K]V) uint
- func MapsAreEqual[K comparable, V comparable](first map[K]V, second map[K]V) bool
- func PathExists(path string) bool
- func ReadDirectory(directory string) []string
- func ReadFile(filename string) string
- func RelativeToCardinal(relative int, size uint) int
- func RemakeDirectory(directory string)
- func RemovePath(path string)
- func ReplaceAll(template string, name string, value string) string
- func WriteFile(filename string, source string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 ¶
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 ¶
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 ¶
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 ¶
IsUndefined checks whether or not the specified value is undefined. Empty strings and nil pointers are considered as being undefined.
func MakeAllCaps ¶
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 ¶
MakeLowerCase modifies the specified mixed case string into a corresponding string starting with a lowercase letter. All other letters remain unchanged.
func MakePlural ¶
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 ¶
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 ¶
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 ¶
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
ReadDirectory returns an array containing the filenames of the files in the specified directory.
func ReadFile ¶
ReadFile returns the contents of the specified file from the file system as a string.
func RelativeToCardinal ¶ added in v7.14.0
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 ¶
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.
Types ¶
This section is empty.