Goutil
💪 Helper Utils(900+): int, byte, string, array/slice, map, struct, dump, convert/format, error, web/http, cli/flag, OS/ENV, filesystem, system, test/assert, time and more. Go 常用的工具函数:数字,字符串,数组,Map,结构体,反射,文本,文件,错误,时间日期,特殊处理,格式化,常用信息获取等等
Install / Use
/learn @gookit/GoutilREADME
GoUtil
💪 Useful utils(900+) package for the Go: int, string, array/slice, map, struct, reflect, error, time, format, CLI, ENV, filesystem, system, testing and more.
Packages
Basic packages
arrutil: Array/Slice util functions. eg: check, convert, formatting, enum, collectionsbyteutil: Provide some common bytes util functions. eg: convert, check and moremaputilMap data util functions. eg: convert, sub-value get, simple mergemathutilMath(int, number) util functions. eg: convert, math calc, randomreflectsProvide extends reflect util functions.structsProvide some extends util functions for struct. eg: tag parse, struct data initstrutilString util functions. eg: bytes, check, convert, encode, format and moresysutilSystem util functions. eg: sysenv, exec, user, processcliutilCommand-line util functions. eg: colored print, read input, exec commandenvutilENV util for current runtime env information. eg: get one, get info, parse varfsutilFilesystem util functions, quick create, read and write file. eg: file and dir check, operatejsonutilProvide some util functions for quick read, write, encode, decode JSON data.
Debug & Test & Errors
dump: GO value printing tool. print slice, map will auto wrap each element and display the call locationerrorxProvide an enhanced error implements for go, allow with stacktrace and wrap another error.assertProvides commonly asserts functions for help testingtestutilTest help util functions. eg: http test, mock ENV valuefakeobjprovides a fake object for testing. such as fake fs.File, fs.FileInfo, fs.DirEntry etc.
Extra Tools packages
cflag: Wraps and extends goflag.FlagSetto build simple command line applicationsccolor: Simple command-line color output library that uses ANSI color codes to output text with colors.timexProvides an enhanced time.Time implementation. Add more commonly used functional methods- Provides datetime format parsing like
Y-m-d H:i:s - such as: DayStart(), DayAfter(), DayAgo(), DateFormat() and more.
- Provides datetime format parsing like
- httpreq An easier-to-use HTTP client that wraps http.Client, and with some http utils.
- syncs Provides synchronization primitives util functions.
More ...
cmdlineProvide cmdline parse, args build to cmdlineencodes: Provide some encoding/decoding, hash, crypto util functions. eg: base64, hex, etc.finderProvides a simple and convenient file/dir lookup function, supports filtering, excluding, matching, ignoring, etc.netutilNetwork util functions. eg: Ip, IpV4, IpV6, Mac, Port, Hostname, etc.textutilProvide some extensions text handle util functions. eg: text replace, etc.textscanImplemented a parser that quickly scans and analyzes text content. It can be used to parse INI, Properties and other formatscmdrProvide for quick build and run a cmd, batch run multi cmd tasksclipboardProvide a simple clipboard read and write operations.processProvide some process handle util functions.fmtutilFormat data util functions. eg: data, size, timegoinfoprovide some standard util functions for go.
Go Doc
Please see Go doc. Wiki docs on ZRead.ai - gookit/goutil
Install
go get github.com/gookit/goutil
Usage
// github.com/gookit/goutil
is.True(goutil.IsEmpty(nil))
is.False(goutil.IsEmpty("abc"))
is.True(goutil.IsEqual("a", "a"))
is.True(goutil.IsEqual([]string{"a"}, []string{"a"}))
is.True(goutil.IsEqual(23, 23))
is.True(goutil.Contains("abc", "a"))
is.True(goutil.Contains([]string{"abc", "def"}, "abc"))
is.True(goutil.Contains(map[int]string{2: "abc", 4: "def"}, 4))
// convert type
str = goutil.String(23) // "23"
iVal = goutil.Int("-2") // 2
i64Val = goutil.Int64("-2") // -2
u64Val = goutil.Uint("2") // 2
Dump go variable
dump.Print(somevar, somevar2, ...)
dump nested struct

Packages
Array and Slice
<details><summary>Click to see functions 👈</summary>Package
github.com/gookit/goutil/arrutil
// source at arrutil/arrutil.go
func GetRandomOne[T any](arr []T) T
func RandomOne[T any](arr []T) T
// source at arrutil/check.go
func SliceHas[T comdef.ScalarType](slice []T, val T) bool
func IntsHas[T comdef.Integer](ints []T, val T) bool
func Int64sHas(ints []int64, val int64) bool
func StringsHas[T ~string](ss []T, val T) bool
func InStrings[T ~string](elem T, ss []T) bool
func NotIn[T comdef.ScalarType](value T, list []T) bool
func In[T comdef.ScalarType](value T, list []T) bool
func ContainsAll[T comdef.ScalarType](list, values []T) bool
func IsSubList[T comdef.ScalarType](values, list []T) bool
func IsParent[T comdef.ScalarType](values, list []T) bool
func HasValue(arr, val any) bool
func Contains(arr, val any) bool
func NotContains(arr, val any) bool
// source at arrutil/collection.go
func StringEqualsComparer(a, b string) int
func ValueEqualsComparer[T comdef.Compared](a, b T) int
func ReflectEqualsComparer[T any](a, b T) int
func ElemTypeEqualsComparer[T any](a, b T) int
func TwowaySearch[T any](data []T, item T, fn Comparer[T]) (int, error)
func CloneSlice[T any](data []T) []T
func Diff[T any](first, second []T, fn Comparer[T]) []T
func Differences[T any](first, second []T, fn Comparer[T]) []T
func Excepts[T any](first, second []T, fn Comparer[T]) []T
func Intersects[T any](first, second []T, fn Comparer[T]) []T
func Union[T any](first, second []T, fn Comparer[T]) []T
func Find[T any](source []T, fn Predicate[T]) (v T, err error)
func FindOrDefault[T any](source []T, fn Predicate[T], defaultValue T) T
func TakeWhile[T any](data []T, fn Predicate[T]) []T
func ExceptWhile[T any](data []T, fn Predicate[T]) []T
// source at arrutil/convert.go
func JoinStrings(sep string, ss ...string) string
func StringsJoin(sep string, ss ...string) string
func JoinTyped[T any](sep string, arr ...T) string
func JoinSlice(sep string, arr ...any) string
func IntsToString[T comdef.Integer](ints []T) string
func ToInt64s(arr any) (ret []int64, err error)
func MustToInt64s(arr any) []int64
func SliceToInt64s(arr []any) []int64
func ToMap[T any, K comdef.ScalarType, V any](list []T, mapFn func(T) (K, V)) map[K]V
func AnyToSlice(sl any) (ls []any, err error)
func AnyToStrings(arr any) []string
func MustToStrings(arr any) []string
func ToStrings(arr any) (ret []string, err error)
func SliceToStrings(arr []any) []string
func QuietStrings(arr []any) []string
func ConvType[T any, R any](arr []T, newElemTyp R) ([]R, error)
func AnyToString(arr any) string
func SliceToString(arr ...any) string
func ToString[T any](arr []T) string
func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V
func CombineToSMap(keys, values []string) map[string]string
// source at arrutil/format.go
func NewFormatter(arr any) *ArrFormatter
func FormatIndent(arr any, indent string) string
// source at arrutil/process.go
func Reverse[T any](ls []T)
func Remove[T comdef.Compared](ls []T, val T) []T
func Filter[T any](ls []T, filter ...comdef.MatchFunc[T]) []T
func Map[T, V any](list []T, mapFn MapFn[T, V]) []V
func Map1[T, R any](list []T, fn func(t T) R) []R
func Column[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V
func Unique[T comdef.NumberOrString](list []T) []T
func IndexOf[T comdef.NumberOrString](val T, list []T) int
func FirstOr[T any](list []T, defVal ...T) T
// source at arrutil/strings.go
func StringsToAnys(ss []string) []any
func StringsToSlice(ss []string) []any
func StringsAsInts(ss []string) []int
func StringsToInts(ss []string) (ints []int, err error)
func StringsTryInts(ss []string) (ints []int, err error)
func StringsUnique(ss []string) []string
func StringsContains(ss []string, s string) bool
func StringsRemove(ss []string, s string) []string
func StringsFilter(ss []string, filter ...comdef.StringMatchFunc) []string
func StringsMap(ss []string, mapFn func(s string) string) []string
func TrimStrings(ss []string, cutSet ...string) []string
</details>
ArrUtil Usage
check value:
arrutil.IntsHas([]int{2, 4, 5}, 2) // True
arrutil.Int64sHas([]int64{2, 4, 5}, 2) // True
arrutil.StringsHas([]string{"a", "b"}, "a") // True
// list and val interface{}
arrutil.Contains(list, val)
arrutil.Contains([]uint32{9, 2, 3}, 9) // True
convert:
ints, err := arrutil.ToInt64s([]string{"1", "2"}) // ints: []int64{1, 2}
ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"}
Bytes Utils
<details><summary>Click to see functions 👈</summary>Package
github.com/gookit/goutil/byteutil
// source at byteutil/buffer.go
f
