Documentation
¶
Overview ¶
Package utl is a library of very common housekeeping functions. Many of these functions seem very simple and almost unnecessary, but they can make code much easier to read and digest.
Index ¶
- Variables
- func Bool(x interface{}) bool
- func BytesToYamlObject(yamlBytes []byte) (yamlObject interface{}, err error)
- func ConvertDateFormat(dateString, srcFormat, dstFormat string) (string, error)
- func DateStringToEpocInt64(dateString, dateFormat string) (int64, error)
- func Die(format string, args ...interface{})
- func EpocInt64ToTime(epocInt int64) time.Time
- func EpocStringToTime(epocString string) (time.Time, error)
- func FileAge(filePath string) int64
- func FileExist(filePath string) (e bool)
- func FileModTime(filePath string) int
- func FileNotExist(filePath string) (e bool)
- func FileSize(filePath string) int64
- func FileUsable(filePath string) bool
- func FirstN(s string, n int) string
- func GenerateRandomString(length int) string
- func GetDateInDays(days string) time.Time
- func GetDaysBetween(date1, date2 string) int64
- func GetDaysSinceOrTo(date1 string) int64
- func GetType(v interface{}) string
- func Int2StrWithCommas(num interface{}) string
- func Int64(x interface{}) int64
- func Int64Abs(x int64) int64
- func Int64ToString(i int64) string
- func IntAbs(x int) int
- func IsAlpha(c rune) bool
- func IsDigit(c rune) bool
- func IsHexDigit(c rune) bool
- func IsInternetAvailable() bool
- func IsInternetNotAvailable() bool
- func IsIpPortStrReachable(ipPortStr string) bool
- func IsLeapYear(year int64) bool
- func ItemInList(arg string, argList []string) bool
- func JsonBytesReindent(jsonBytes []byte, indent int) (jsonBytes2 []byte, err error)
- func JsonBytesToJsonObj(jsonBytes []byte) (jsonObject interface{}, err error)
- func JsonToBytes(jsonObject interface{}) (jsonBytes []byte, err error)
- func JsonToBytesIndent(jsonObject interface{}, indent int) (jsonBytes []byte, err error)
- func LastElem(s, splitter string) string
- func LoadFileJson(filePath string, compressed bool) (jsonObject interface{}, err error)
- func LoadFileText(filePath string) (rawBytes []byte, err error)
- func LoadFileYaml(filePath string) (yamlObject interface{}, err error)
- func LoadFileYamlBytes(filePath string) (yamlBytes []byte, err error)
- func MergeJsonObjects(a, b map[string]interface{}) map[string]interface{}
- func MergeMaps(m1, m2 map[string]string) (result map[string]string)
- func MergeObjects(a, b map[string]interface{}) map[string]interface{}
- func PadSpaces(targetWidth, stringWidth int) string
- func PostSpc(value interface{}, width int) string
- func PreSpc(value interface{}, width int) string
- func Prettify(jsonObject interface{}) (pretty string, err error)
- func PrintColorSamples()
- func PrintDays(days int64)
- func PrintJson(jsonObject interface{})
- func PrintJsonBytesColor(jsonBytes []byte)
- func PrintJsonColor(jsonObject interface{})
- func PrintYaml(yamlObject interface{})
- func PrintYamlBytesColor(yamlBytes []byte)
- func PrintYamlColor(yamlObject interface{})
- func PromptMsg(msg string) rune
- func RemoveFile(filePath string) error
- func SameType(a, b interface{}) bool
- func SaveFileJson(jsonObject interface{}, filePath string, compress bool)
- func SaveFileText(filePath string, rawBytes []byte) error
- func SaveFileYaml(yamlObject interface{}, filePath string)
- func SortMapStringKeys(obj map[string]string) (sortedKeys []string)
- func SortObjStringKeys(obj map[string]interface{}) (sortedKeys []string)
- func Str(x interface{}) string
- func StrSingleQuote(x interface{}) string
- func StringInJson(jsonObject interface{}, filter string) bool
- func StringToInt64(s string) (int64, error)
- func StripColor(s string) string
- func SubString(large, small string) bool
- func ToStr(value interface{}) string
- func Trace() string
- func ValidDate(dateString, expectedFormat string) bool
- func ValidIpStr(ipStr string) bool
- func ValidUuid(s string) bool
- func ValidateJson(filePath string) error
- func ValidateYaml(filePath string) error
- func YamlToBytes(yamlObject interface{}) (yamlBytes []byte, err error)
- func YamlToBytesIndent(yamlObject interface{}, indent int) (yamlBytes []byte, err error)
- type StringSet
- type TraceInfo
Constants ¶
This section is empty.
Variables ¶
var ( Red = color.FgLightRed.Render Blu = color.FgLightBlue.Render Gre = color.FgGreen.Render Yel = color.FgYellow.Render Whi = color.FgWhite.Render Cya = color.FgCyan.Render Mag = color.FgLightMagenta.Render Gra = color.FgDarkGray.Render Red2 = color.FgRed.Render Blu2 = color.FgBlue.Render Gre2 = color.FgLightGreen.Render Yel2 = color.FgLightYellow.Render Whi2 = color.FgLightWhite.Render Cya2 = color.FgLightCyan.Render Mag2 = color.FgMagenta.Render )
Functions ¶
func Bool ¶ added in v1.2.0
func Bool(x interface{}) bool
Return the boolean value for the given x variable, defaulting to false if not a valid boolean.
func BytesToYamlObject ¶
Convert byte slice to YAML interface object
func ConvertDateFormat ¶
Converts dateString from source format to destination format. Returns date string in destination format and an error if the conversion fails.
func DateStringToEpocInt64 ¶
Convert dateString, given in dateFormat, to Unix Epoc seconds int64
func Die ¶
func Die(format string, args ...interface{})
Same as regular Printf function but always exits, with a return code of 1
func EpocInt64ToTime ¶
Converts an epoch timestamp in int64 format to a time.Time object.
func EpocStringToTime ¶
Converts an epoch timestamp in string format to a time.Time object. Returns a time.Time object and an error if the conversion fails.
func FileAge ¶
Returns the age of the given filePath in seconds as int64. If the file does not exist or is empty, it returns -1.
func FileModTime ¶
Returns given filePath modified time in Unix epoch int
func FileNotExist ¶
Returns true is given filePath does not exist. False otherwise.
func FileUsable ¶
Returns true if filepath exists and has some content. False otherwise.
func GenerateRandomString ¶ added in v1.2.0
Generates a random string of the given length
func GetDateInDays ¶
Print yyyy-mm-dd date for given number of +/- days in future or past
func GetDaysBetween ¶
Return number of days between 2 dates
func GetDaysSinceOrTo ¶
Calculate and return number of +/- days from NOW to date given Note: Calculations are all in UTC time. And it takes leap year into account.
func Int2StrWithCommas ¶ added in v1.2.0
func Int2StrWithCommas(num interface{}) string
Int2StrWithCommas converts an integer (arbitrarily large) to a string with commas as thousand separators.
func Int64 ¶ added in v1.3.3
func Int64(x interface{}) int64
Return the int64 value for the given x variable, defaulting to 0 if not a valid int64.
func IsAlpha ¶
Checks if given rune is an alphabetic character (either uppercase or lowercase). Returns true if it is, false otherwise.
func IsHexDigit ¶
Returns true if rune is a hexadeximal digit. False otherwise.
func IsInternetAvailable ¶ added in v1.3.1
func IsInternetAvailable() bool
Checks if internet is available by attempting to reach reliable URLs.
func IsInternetNotAvailable ¶ added in v1.3.1
func IsInternetNotAvailable() bool
Checks if internet is not available.
func IsIpPortStrReachable ¶
Checks if IP_Address:Port string is reachable
func IsLeapYear ¶
Returns true if given year is a leap year. False otherwise.
func ItemInList ¶
ItemInList checks if a given string (arg) is present in a list of strings (argList). Returns true if found, false otherwise. Consider using utl.NewStringSet() type instead.
func JsonBytesReindent ¶
Convert JSON interface object to byte slice, with option to indent spacing
func JsonBytesToJsonObj ¶
Convert JSON byte slice to JSON interface object, with default 2-space indentation
func JsonToBytes ¶
Convert JSON interface object to byte slice, with default 2-space indentation
func JsonToBytesIndent ¶
Convert JSON interface object to byte slice, with option to indent spacing
func LoadFileJson ¶
Reads, load, and decode given filePath as a JSON object text file. Returns JSON object and err if any. The compression option can sometimes help speed things up for some very large objects.
func LoadFileText ¶
Read and recode given filePath as text byte slice. Returns the byte slice and error if any.
func LoadFileYaml ¶
Tries to read, load, and decode given filePath as some YAML object. Returns YAML object and error if any.
func LoadFileYamlBytes ¶
Tries to read, load, and recode given filePath as some YAML object as byte slice. Returns YAML object as byte slice. and error if any.
func MergeJsonObjects ¶ added in v1.2.0
Alias for MergeObjects function.
func MergeMaps ¶
Combines two string-to-string maps, with keys from the second map overwriting those from the first if duplicates exist. Returns the merged map.
func MergeObjects ¶
Recursively merges the keys from object a into object b. Existing object b attributes are overwritten if there's a conflict.
func PadSpaces ¶
Return string of spaces for padded printing. Needed when printing terminal colors. Colorize output uses % sequences that conflict with Printf's own formatting with %
func PostSpc ¶
Return value as a string, padded with trailing spaces, totalling width size wide. This is needed when printing terminal colors, because they conflict with Printf's own '%' formatting
func PreSpc ¶
Return value as a string, padded with leading spaces, totalling width size wide. This is needed when printing terminal colors, because they conflict with Printf's own '%' formatting
func PrintColorSamples ¶
func PrintColorSamples()
PrintColorSamples is a dormant function and not actively used anywhere. It is left around for those times when someone wants to test and see what the colors would look like.
func PrintJson ¶
func PrintJson(jsonObject interface{})
Prints JSON object, flushing the output buffer
func PrintJsonBytesColor ¶
func PrintJsonBytesColor(jsonBytes []byte)
Prints JSON byte slice in color. Just an alias of yaml.go:PrintYamlBytesColor().
func PrintYamlBytesColor ¶
func PrintYamlBytesColor(yamlBytes []byte)
Print YAML bytes in color, includes comments Also prints JSON byte slice in color, but use json.go:PrintJsonBytesColor() alias instead. Caller must ensure yamlBytes is proper YAML/JSON
func PrintYamlColor ¶
func PrintYamlColor(yamlObject interface{})
Print YAML object (that don't usually include comments) in color
func SaveFileJson ¶
Saves given JSON object as a text file, with compression as an option which can sometimes help to speed things up for some very large objects.
func SaveFileText ¶
Saves given byte slice as text file. Returns error is any.
func SaveFileYaml ¶
func SaveFileYaml(yamlObject interface{}, filePath string)
Save given YAML object to given filePath
func SortMapStringKeys ¶
Return the map string object's keys sorted
func SortObjStringKeys ¶
Return the object's keys sorted
func Str ¶
func Str(x interface{}) string
Return the best printable string value for given x variable
func StrSingleQuote ¶
func StrSingleQuote(x interface{}) string
Returns string value of unknown variable, wrapped in single quotes. This is a special function to lookout for leading '*', which YAML does not allow and must be single-quoted
func StringInJson ¶
Recursive function returns True if filter string value is anywhere within jsonObject
func StripColor ¶ added in v1.3.0
StripColor removes color codes from a string.
func ToStr ¶
func ToStr(value interface{}) string
Converts any value to its string representation using default formatting.
func Trace ¶
func Trace() string
Returns a string showing the current filepath line number and function name.
func ValidDate ¶
Check if string is a valid date in expectedFormat. Return true if so, false otherwise. See https://pkg.go.dev/time
func ValidIpStr ¶
Returns true if given string is a valid IP address. False otherwise.
func ValidateJson ¶ added in v1.2.0
Checks if a given file contains valid JSON
func ValidateYaml ¶ added in v1.2.0
Checks if a given file contains valid YAML
func YamlToBytes ¶
With default 2 space indent
func YamlToBytesIndent ¶
Convert YAML interface object to byte slice, with option indent spacing
Types ¶
type StringSet ¶ added in v1.1.0
type StringSet map[string]struct{}
While Go doesn’t have a built-in 'Set' type, 'map' is a natural and efficient substitute. For better clarity, we wrap it in a custom type with helper methods that align with set operations. This keeps the code clean and self-explanatory while leveraging Go’s simplicity and performance. See protip https://que.one/golang/#using-struct-for-efficient-maps