goutil

package module
v7.7.2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 11 Imported by: 3

README

Go Utility Methods

donation link

A simple utility package for golang.

This module simply adds a variety of useful functions in an easy to use way.

Installation

  go get github.com/AspieSoft/goutil/v7

Usage


import (
  "github.com/AspieSoft/goutil/v7"

  // other optional utility files

  // filesystem
  "github.com/AspieSoft/goutil/v7/fs"

  // encryption
  "github.com/AspieSoft/goutil/v7/crypt"

  // compression
  "github.com/AspieSoft/goutil/v7/compress/gzip"
  "github.com/AspieSoft/goutil/v7/compress/brotli"
  "github.com/AspieSoft/goutil/v7/compress/smaz"

  // other
  "github.com/AspieSoft/goutil/v7/compress/bash"
  "github.com/AspieSoft/goutil/v7/compress/cache"
  "github.com/AspieSoft/goutil/v7/compress/syncmap"
  "github.com/AspieSoft/goutil/v7/compress/cputemp"
)

func main(){
  fs.JoinPath("root", "file") // a safer way to join 2 file paths without backtracking

  goutil.Contains([]any, any) // checks if an array contains a value


  // simple AES-CFB Encryption
  encrypted := crypt.CFB.Encrypt([]byte("my message"), []byte("password"))
  crypt.CFB.Decrypt(encrypted, []byte("password"))


  // simple gzip compression for strings
  // (also supports brotli and smaz)
  compressed := gzip.Zip([]byte("my long string"))
  gzip.UnZip(compressed)


  // convert any type to something else
  MyStr := goutil.ToType[string](MyByteArray)
  MyInt := goutil.ToType[int]("1") // this will return `MyInt == 1`
  MyUInt := goutil.ToType[uint32]([]byte{'2'}) // this will return `MyUInt == 2`
  MyBool := goutil.ToType[bool](1) // this will return `MyBool == true`


  // watch a directory recursively
  watcher := fs.FileWatcher()

  watcher.OnFileChange = func(path, op string) {
    // do something when a file changes
    path // the file path the change happened to
    op // the change operation
  }

  watcher.OnDirAdd = func(path, op string) {
    // do something when a directory is added
    // return false to prevent that directory from being watched
    return true
  }

  watcher.OnRemove = func(path, op string) {
    // do something when a file or directory is removed
    // return false to prevent that directory from no longer being watched
    return true
  }

  watcher.OnAny = func(path, op string) {
    // do something every time something happenes
  }

  // add a directory to the watch list
  watcher.WatchDir("my/folder")

  // close a specific watcher
  watcher.CloseWatcher("my/folder")

  // close all watchers
  watcher.CloseWatcher("*")

  // wait for all watchers to finish closing
  watcher.Wait()


  // loading config files
  type Config struct{}
  config = Config{}

  // this method will automatically search for [.yml, .yaml, .json, etc...] files
  // it allows the user to decide what compatible file type they want to use for their config
  fs.ReadConfig("path/to/config.yml", &config)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Clean *clean = &clean{}
View Source
var Conv *typeConv = &typeConv{}
View Source
var HTML *encodeHtml = &encodeHtml{}
View Source
var JSON *encodeJson = &encodeJson{}
View Source
var VarType map[string]reflect.Type

Functions

func ArrayEqual

func ArrayEqual[T any](arr1 []T, arr2 []T, ignoreLength ...bool) bool

ArrayEqual returns true if 2 arrays are equal and of the same length (even if they are in a different order)

func CloneBytes added in v7.5.3

func CloneBytes(b []byte) []byte

CloneBytes is a simple method for copying a stuborn []byte that wants to be a reference

golang default:
	buf := make([]byte, 5)
	buf = []byte{'t', 'e', 's', 't', '1'}
	newBuf := buf
	newBuf[4] = 2
	string(buf) == string(newBuf)

using this method:
	buf := make([]byte, 5)
	buf = []byte{'t', 'e', 's', 't', '1'}
	newBuf := goutil.CloneBytes(buf)
	newBuf[4] = 2
	string(buf) != string(newBuf)

func Contains

func Contains[T any](search []T, value T) bool

Contains returns true if an array contains a value

func ContainsMap

func ContainsMap[T Hashable, J any](search map[T]J, value J) bool

ContainsMap returns true if a map contains a value

func ContainsMapKey

func ContainsMapKey[T Hashable, J any](search map[T]J, key T) bool

ContainsMapKey returns true if a map contains a key

func FormatMemoryUsage

func FormatMemoryUsage(b uint64) float64

FormatMemoryUsage converts bytes to megabytes

func IndexOf

func IndexOf[T any](search []T, value T) (int, error)

IndexOf returns the index of a value in an array

returns -1 and an error if the value is not found

func IndexOfMap

func IndexOfMap[T Hashable, J any](search map[T]J, value J) (T, error)

IndexOfMap returns the index of a value in a map

returns an error if the value is not found

func IsZeroOfUnderlyingType

func IsZeroOfUnderlyingType(x interface{}) bool

IsZeroOfUnderlyingType can be used to determine if an interface{} in null or empty

func MapArgs

func MapArgs(args ...[]string) map[string]string

MapArgs will convert a bash argument array ([]string) into a map (map[string]string)

When @args is left blank with no values, it will default to os.Args[1:]

-- Arg Convertions:

"--Key=value" will convert to "key:value"

"--boolKey" will convert to "boolKey:true"

"-flags" will convert to "f:true, l:true, a:true, g:true, s:true" (only if its alphanumeric [A-Za-z0-9]) if -flags is not alphanumeric (example: "-test.paniconexit0" "-test.timeout=10m0s") it will be treated as a --flag (--key=value --boolKey)

keys that match a number ("--1" or "-1") will start with a "-" ("--1=value" -> "-1:value", "-1" -> -1:true) this prevents a number key from conflicting with an index key

everything else is given a number value index starting with 0

this method will not allow --args to have their values modified after they have already been set

func MapArgsByte

func MapArgsByte(args ...[][]byte) map[string][]byte

MapArgs is just like MapArgs, but it excepts and outputs using []byte instead of string

func MapEqual

func MapEqual[T Hashable, J any](map1 map[T]J, map2 map[T]J, ignoreLength ...bool) bool

MapEqual returns true if 2 maps are equal and of the same length (even if they are in a different order)

func SysFreeMemory added in v7.5.1

func SysFreeMemory() float64

SysFreeMemory returns the amount of memory available in megabytes

func ToType

func ToType[T SupportedType](val interface{}) T

ToType attempts to converts an interface{} from the many possible types in golang, to a specific type of your choice

if it fails to convert, it will return a nil/zero value for the appropriate type

func ToVarType

func ToVarType[T SupportedType](val interface{}, ref T) T

ToVarType grabs the type from another var as a reference, and runs the `ToType` with the ref type

func ToVarTypeInterface

func ToVarTypeInterface(val interface{}, ref interface{}) interface{}

ToVarTypeInterface attempts to convert an interface to match the unknown type of another interface

this method is similar to the ToType method, but it simply returns nil if it cannot find the proper var type

func TrimRepeats

func TrimRepeats(b []byte, chars []byte) []byte

TrimRepeats trims repeating adjacent characters and reduces them to one character

@b: byte array to trim

@chars: list of bytes to trim repeats of

func TypeEqual

func TypeEqual(val1 interface{}, val2 interface{}) bool

ToVarTypeInterface attempts to convert an interface to match the unknown type of another interface

this method is similar to the ToType method, but it simply returns nil if it cannot find the proper var type

Types

type Hashable

type Hashable interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | string | complex64 | complex128
}

type NullType

type NullType[T any] struct {
	Null T
}

type SupportedType

type SupportedType interface {
	string | []byte | byte | bool |
		int | int64 | int32 | int16 | int8 |
		uint | uint64 | uint32 | uint16 | uintptr |
		float64 | float32 |
		[]interface{} | []string | [][]byte | []bool |
		[]int | []int64 | []int32 | []int16 | []int8 |
		[]uint | []uint64 | []uint32 | []uint16 | []uintptr |
		[]float64 | []float32 |
		map[string]interface{} | map[byte]interface{} |
		map[int]interface{} | map[int64]interface{} | map[int32]interface{} | map[int16]interface{} | map[int8]interface{} |
		map[uint]interface{} | map[uint64]interface{} | map[uint32]interface{} | map[uint16]interface{} | map[uintptr]interface{} |
		map[float64]interface{} | map[float32]interface{}
}

SupportedType is an interface containing the types which are supported by the ToType method

type ToInterface

type ToInterface struct {
	Val interface{}
}

Jump to

Keyboard shortcuts

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