A modern, comprehensive and efficient utility library of Go.
A modern, comprehensive and efficient utility library of Go.
A modern, comprehensive and efficient utility library of Golang.
Some utility functions can be used to handle conversion of different character encoding, such as gbk to utf8.
gbk := []byte{0xC4, 0xE3, 0xBA, 0xC3} // 你好 in gbk
GbkToUtf8(gbk) // 你好 in utf8
Utf8ToGbk(utf8) // [196 227 186 195]Some converting function to json.
student := struct {
Hobby string
Age int32
}{
Hobby: "pingpopng",
Age: 28,
}
encoding.ToIndentJSON(&student)
/*
output:
{
"Hobby": "pingpopng",
"Age": 28
}
*/Some utility functions can be used to handle network. For example you can use IPv4StrToU32() transform ipv4 string to uint32 value.
IsReservedIP("0.0.0.256") // -1 invalid ip
IsReservedIP("39.156.69.79") // 0 public ip
IsReservedIP("127.0.0.1") // 1 reserved ip
IPv4StrToU32("127.0.0.1") // 2130706433
U32ToIPv4Str(2130706433) // "127.0.0.1"
GetNativeEndian() // LittleEndian
IsLittleEndian() // trueSome useful utility functions can be used to handle slice.
…Some utility functions can be used to handle string.
Split("a,b,c", ",") // []string{"a", "b", "c"}
SplitSeps("a,b|c", ",", "|") // []string{"a", "b", "c"}
SplitSeps("a,bSEPc", ",", "SEP") // []string{"a", "b", "c"}
Join([]int{1, 2, 3}, ",") // "1,2,3"
Join([]string{"a", "", "b"}, ",") // "a,,b"
JoinNonEmptyStrs(",", "a", "", "b") // "a,b"
Reverse("abc") // "cba"
AlphanumericNum("108条梁山man") // 6
AlphanumericNumV2("108条梁山man") // 6
AlphanumericNumRegExp("108条梁山man") // 6Some utility functions can be used to handle date and time.
…Some utility functions can be used to convert one type to another type.
…// Convert bool slice or array to set.
bools := []bool{true, false, true}
ToSet[bool](bools)
ToSetE[bool](bools)
// Convert int slice or array to set.
ints := []int{1, 2, 3}
ToSet[int](ints)
ToSetE[int](ints)
// Convert string slice or array to set.
strs := []string{"foo", "bar", "baz"}
ToSet[string](strs)
ToSetE[string](strs)
// Split string to set.
SplitStrToSet("a,b,c", ",") // map[a:{}, b:{}, c:{}]…var st = struct {
I int
S string
}{I: 1, S: "a"}
// to map[string]any
StructToMap(st) // map["I":1 "S":"a"]
StructToMapStr(st) // map["I":"1" "S":"a"]
// json or other type to map[string]string
ToMapStr(`{"foo":"foo","bar":"bar","baz":"baz"}`) // map["foo":"foo" "bar":"bar" "baz":"baz"]
ToMapStrE(`{"foo":"foo","bar":"bar","baz":"baz"}`) // map["foo":"foo" "bar":"bar" "baz":"baz"], nilSome utility functions can be used to handle URL.
…Some utility functions can be used to create Hash, HMAC and crypt data.
…Some utility functions to create a real non-negative random int number, specified length random string and so on.
GetRandInt() // 2040723487295132865
GetRandIntn(100) // 49
GetRandIntRange(0, 100) // 44
GetRandByteSlice(3) // [241 16 101]
GetRandStr(3) // dAt
GetRandLowerStr(3) // lts
GetRandUpperStr(3) // YUTSome utility functions to operate on the file.
// List contents of the directory.
ListDir()
ListFileNames()
ListDirEntryPaths()
ListDirEntryPathsSymlink()
// Check the file or directory.
IsExist()
IsDir()
IsDirE()
IsFile()
IsFileE()
IsSymlink()
IsSymlinkE()
IsShortcutFile()
// Create a file.
Create()
CreateFile()
// Clears a file content.
ClearFile()
// Gets file info.
FileMd5()
FileMd5Reader()
FileSize()
FileSizeFile()
// and so on.Some utility functions to handle comparison.
// Compare two any type value.
Cmp(888, 889) // LT
Cmp(888, 888) // EQ
Cmp(889, 888) // GT
Cmp(88.8, 88.9) // LT
Cmp(88.8, 88.8) // EQ
Cmp(88.9, 88.8) // GT
Cmp("abc", "b") // LT
Cmp("abc", "abc") // EQ
Cmp("b", "abc") // GT
// Compare semantic version.
b, _ := VerGTVer("1.0.5", "1.0.4") // true
b, _ := VerLTVer("1.0.5", "2.0.4") // true
b, _ := VerGEVer("2.0.4", "2.0.4") // true
b, _ := VerLEVer("1.0.5", "1.0.5") // trueSome utility functions now unclassified. Of course, it may be classified and moved to a new subdirectory in the future.
// NO NOW.We really appreciate any code commits which make this lib powerful. Please follow the rules below to create your pull request.
The above examples are just the tip of the iceberg. For more usage, please read the source code.
Due to the limited personal ability, you are welcome to criticize and correct. Of course, welcome to join in the construction of this library.
No open issues yet, or sync has not completed.