Various utility functions.
- ArrayContains(array interface{}, val interface{}) (exists bool, index int)
- ArrayMerge(array ...[]interface{}) []interface{}
- ArrayEqualsInt(a []int, b []int) bool - checks whether 2 int arrays are the same
- ArrayEqualsStr(a []int, b []int) bool - checks whether 2 string arrays are the same
- ArrayReverse[T any](arr []T) []T - returns a new reversed array
arr := []string{"one", "two", "three"}
newArr := ArrayReverse(arr)
// newArr is: []string{"three", "two", "one"}
- ArrayToArrayAny[T any](arr []T) []any - converts an array of any type to array of interfaces
IsStringArrayEqual(a, b []string) bool- Deprecated. new code should use ArrayEqualsStr
- EmailSend(from string, to []string, subject string, htmlMessage string) (bool, error)
- AppAddress() string - returns the URL address the app is running under (APP_URL:APP:PORT)
- AppEnv() string - returns the environment the app is running in (APP_ENV)
- AppInDevelopment() bool - return whether app is in development
- AppInLive() bool - return whether app is in production / live
- AppInProduction() bool - return whether app is in production / live
- AppInTesting() bool - return whether app is being tested
- AppName() string - returns the name the app is running under (APP_NAME)
- AppPort() string - returns the port the app is running under (APP_PORT)
- AppURL() string - returns the URL the app is running under (APP_URL)
- DbDriver() string - returns the database driver (DB_DRIVER)
- DbHost() string - returns the database host (DB_HOST)
- DbPort() string - returns the database port (DB_PORT)
- DbDatabase() string - returns the database name driver (DB_DATABASE)
- DbUsername() string - returns the database username (DB_USERNAME)
- DbPassword() string - returns the database password (DB_PASSWORD)
- EmailFromAddress() string - returns the mail from address (MAIL_FROM)
- EmailFromName() string - returns the mail from name (MAIL_NAME)
- Env(key string) string - returns an environment variable (i.e. Env("DB_DRIVER"))
- EnvInitialize(key string) string - Initializes an .env file, if exists. Fails loudly if the file is invalid and cannot be parsed
utils.EnvInitialize()
- EnvEncInitialize(password string)</> - Initializes an .env.vault file, if it exists
utils.EnvEncInitialize("abc")
- FileExists(filePath string) bool
- FileGetContents(filename string) (string, error)
- FilePutContents(filename string, data string, mode os.FileMode) error - writes a string to file
- FileToBase64(filePath string) string - converts a file to Base64 encoded string
- ImgToBase64Url(filePath string) string - converts an image file to Base64 encoded URL string
This functionality is moved to github.com/gouniverse/htmlutils and can be used from there
- IP(r *http.Request) string - Returns the IP address of the user
- Req(r *http.Request, key string, defaultValue string) string - Returns a POST or GET value for a key, or default if not exists
- ReqAll(r *http.Request) url.Values - Returns all request variables
- ReqArray(r *http.Request, key string, defaultValue []string) []string - return an array for a key from the request
- ReqMap(r *http.Request, key string) map[string]string - returns a map for a key from the request
RespondJSON(w http.ResponseWriter, response api.Response)- DEPRECATED. use https://github.com/gouniverse/api
- BytesToBase64Url(imgBytes []byte) string - converts bytes to Base64 encode URL string
- ImgPlaceholderURL(width, height, text string) - returns a placeholder image
- ImgToBase64Url(filePath string) string - converts an image file to Base64 encoded URL string
- PicsumURL(width int, height int, opt PicsumURLOptions) - returns an image from the online service Lorem Picsum
- InterfaceToStringArray(v interface{}) []string
- LinkWebsite() string
- MapToColumn(inputMap []map[string]string, keyName string) []string - returns a column from map
- MapToKeyValue(inputMap []map[string]string, keyName string, valueName string) map[string]string - returns a key-value array from an array of maps
The deprecated string functions are moved to: https://github.com/gouniverse/strutils
- AddSlashes(str string) string - adds slashes
- Base64Decode(src string) ([]byte, error) - decodes a string from Base64
- Base64Encode(src []byte) string - encodes a string to Base64
StrBetween(str string, startNeedle string, endNeedle string) (result string, found bool)- returns the substring between two needlesStrContainsOnlySpecifiedCharacters(str string, chars string) bool- checks string contains character from the specified in the chars parameterStrLeftFrom(s string, needle, string) string- returns the substring on the left side of the needle- StrPadLeftFrom(s string, padLength int, pad string) string - returns the string padded on the left side
StrRandom(len int) string- generates a random stringStrRandomFromGamma(length int, gamma string) string- generates random string of specified length with the characters specified in the gamma stringStrRightFrom(s string, needle, string) string- returns the substring on the right side of the needle- StrSlugify(s string, replaceWith rune) string - converts a string to slug
- StrToBcryptHash(str string) (string, error) - converts a string to bcrypt hash
- StrToBcryptHashCompare(str string, hash string) - compares a string to a bcrypt hash
- StrToFloat(s string) (int, error) - converts a string to Float32
- StrToFloat64(s string) (int64, error) - converts a string to Float64
- StrToInt(s string) (int, error) - converts a string to Int32
- StrToInt64(s string) (int64, error) - converts a string to Int64
- StrToMD5Hash(text string) string - StrToMD5Hash converts a string to MD5 hash
- StrToSHA1Hash(text string) string - StrToSHA1Hash converts a string to SHA1 hash
- StrToSHA256Hash(text string) string - converts a string to SHA256 hash
- TemplateParseString(template string, data) string - parses a template file and returns as string
- ToString(v interface{}) string - converts an interface to string
- StrToTimeUnix(str string) (int64, error) - converts string to Unix time
time, err := StrToTimeUnix("2020-12-29 11:00:00")
- ArgsToMap([]string) map[string]string - converts an CLI arguments array to map
- CookieGet(r *http.Request, name string) string - gets a cookie
- CookieRemove(w http.ResponseWriter, r *http.Request, name string) - removes a cookie
- CookieSet(w http.ResponseWriter, r *http.Request, name string, value string, seconds int) - sets a cookie
- Exec(cms string, args... string) map[string]string - executes a CLI command
- ExecLine(cmd string) map[string]string - executes a full CLI command
- IsInt(s string) bool - checks if a string is integer
- IsFloat(s string) bool - checks if a string is float
- IsJSON(s string) bool - naive implementation for rough and fast check if a string is JSON
- IsNumeric(s string) bool - checks if a string is numeric
- IsZero[T comparable](v T) bool
- FromJSON(jsonString string, valueDefault interface{}) (interface{}, error) - JSON decodes a string
- RandBool() bool - returns a random boolean value
random := RandBool()
- ToJSON(value interface{}) (string, error) - JSON encodes a value
- ToBool(str string) bool - converts a string with common names ("yes", "true", "1") to boolean
- ZeroT any (ret T)
isDebugEnabled := ToBool("yes")
- ToFloat(value str) (float64, error) - converts a string to float
- ToInt(value str) (int64, error) - converts a string to int
- XOREncode(buffer []byte, key []byte) []byte - XOR encodes a byte array
- XORDencode(buffer []byte, key []byte) []byte - XOR decodes a byte array
2024-02-10 - Added EnvEncInitialize functions
2024-01-20 - Added RandBool, ReqMap, BytesToBase64Url functions
2023-10-13 - Added function IsJSON, IsZero, Zero, removed deprecations
2023-01-27 - Added function PicsumURL
2022-09-10 - Added function StrContainsOnlySpecifiedCharacters
2022-08-28 - Added function StrToInt, StrToInt64, StrToFloat, StrToFloat64
2022-08-27 - Added function ToInt, ToFloat, IsInt, IsFloat, IsEmpty
2022-08-17 - Added functions ImgPlaceholderURL
2022-08-11 - Added functions StrToBcryptHash, StrToBcryptHashCompare
2022-08-06 - Added function ToBool
2022-08-04 - Added functions CookieGet, CookieSet, FromJSON, ToJSON, XORDecode, XOREncode
2022-08-02 - Added function ArrayReverse
2022-07-31 - Added functions ArrayEqualsInt, ArrayEqualsStr
2022-07-30 - Added functions StrBetween, StrRandom, StrRandomFromGamma, StrToBytes. Deprecated RandStr, RandStrFromGamma
2022-07-28 - Added functions StrLeftFrom, StrRightFrom
2022-06-01 - Moved Fiber functions into separate repo to remove extra dependencies on Fiber
2021-07-19 - Added functions AppInDevelopment, AppInLive, AppInProduction, AppInTesting
- https://github.com/samber/lo (14k stars) - A Lodash-style Go library (map, filter, contains, find...)
- https://github.com/shopspring/decimal (5.3k stars) - Arbitrary-precision fixed-point decimal numbers in go
- https://github.com/duke-git/lancet (2.3k stars) - A comprehensive, efficient, and reusable util function library of Go.
- https://github.com/gookit/goutil (1.7k stars) - Helper Utils(700+): int, byte, string, array/slice, map, struct, dump, convert/format, error, web/http, cli/flag, OS/ENV, filesystem, system, test/assert
- https://github.com/shomali11/util (280 stars) - A collection of useful utility functions
- https://github.com/juju/utils (210 stars) - General utility functions
- https://github.com/go-assist/helper
- https://github.com/billmi/go-utils
- https://github.com/Nx-117/cyan (53 stars)
- https://github.com/dablelv/go-huge-util (48 stars)
- https://github.com/ik5/gostrutils (35 stars)
- https://github.com/dyweb/gommon (26 stars)
- https://github.com/corestoreio/pkg
- https://github.com/appleboy/com (17 stars)
- https://github.com/Akagi201/utils-go