Golang go-huge-util 工具库介绍
文章目錄
- Synopsis
- Encode
- Net
- Slice
- SQL
- String
- Struct
- Time
- Type Conversion
- to another type
- to set
- to slice
- URL
- Crypto
- Rand
- JSON
- Other
- Summary
Synopsis
Go common and huge utility functions help you to create your go program quickly and easily.
Encode
Some useful functions can be used to handle conversion of different character encoding, such as gbk to utf8.
Example:
import (huge "github.com/dablelv/go-huge-util" )gbkStr := []byte{0xC4, 0xE3, 0xBA, 0xC3} // 你好 in gbk utf8Str, _ := huge.GbkToUtf8(gbkStr) // 你好 in utf8 fmt.Println(string(utf8Str)) // 你好gbkStrRes, _ := huge.Utf8ToGbk(utf8Str) fmt.Println(gbkStrRes) // [196 227 186 195]Net
Some useful functions can be used to handle network. For example you can use IPv4StrToU32() transform ipv4 string to uint32 value.
Example:
import (huge "github.com/dablelv/go-huge-util" )huge.IsReservedIP("0.0.0.256") // -1 invalid ip huge.IsReservedIP("39.156.69.79") // 0 public ip huge.IsReservedIP("127.0.0.1") // 1 reserved iphuge.IPv4StrToU32("127.0.0.1") // 2130706433 huge.U32ToIPv4Str(2130706433) // "127.0.0.1" huge.GetNativeEndian() // LittleEndian huge.IsLittleEndian() // trueSlice
Some useful functions can be used to handle slice.
Example:
import (huge "github.com/dablelv/go-huge-util" )huge.UniqueIntSlice([]int{1, 2, 2, 3}) // [1 2 3] huge.UniqueUintSlice([]uint{1, 2, 2, 3}) // [1 2 3] huge.UniqueStrSlice([]string{"a", "b", "b", "c"}) // [a b c]huge.ReverseIntSlice([]int{1, 2, 3}) // [3 2 1] huge.ReverseUintSlice([]uint{1, 2, 3}) // [3 2 1] huge.ReverseStrSlice([]string{"a", "b", "c"}) // [c b a]huge.SumSlice([]int{1,2,3}) // 6 huge.SumSlice([]uint{1,2,3}) // 6 huge.SumSlice([]float32{1.1, 2.2, 3.3}) // 6.6 huge.SumSlice([]float64{1.1, 2.2, 3.3}) // 6.6huge.JoinSliceWithSep([]int{1, 2, 3}, ",") // 1,2,3 huge.JoinSliceWithSep([]uint{1, 2, 3}, ",") // 1,2,3 huge.JoinSliceWithSep([]float64{1.1, 2.2, 3.3}, ",") // 1.1,2.2,3.3 huge.JoinSliceWithSep([]string{"a", "b", "c"}, ",") // a,b,c// CRUD(Create Read Update Delete) on slice by index fib := []int{1, 1, 2, 3, 5, 8} r, _ := huge.InsertSliceE(fib, 6, 13) // [1 1 2 3 5 8 13] r, _ := huge.DeleteSliceE(fib, 0) // [1 2 3 5 8] r, _ := huge.UpdateSliceE(fib, 5, 88) // [1 1 2 3 5 88] r, _ := huge.GetEleIndexesSliceE(fib, 1) // [0 1]// or r := huge.InsertIntSlice(fib, 5, 13) // [1 1 2 3 5 8 13] r := huge.DeleteIntSliceE(fib, 0) // [1 2 3 5 8] r := huge.UpdateIntSliceE(fib, 5, 88) // [1 1 2 3 5 88] r := huge.GetEleIndexesSlice(fib, 1) // [0 1]SQL
Some useful functions can be used to handle sql statement.
Example:
import (huge "github.com/dablelv/go-huge-util" )var sql = `select * from t where field1="{name}"` huge.FormatSql(sql, map[string]string{"name": "dablelv"}, false) // select * from t where field1="dablelv"String
Some useful functions can be used to handle string.
Example:
import (huge "github.com/dablelv/go-huge-util" )huge.Split("a,b,c", ",") // []string{"a", "b", "c"}huge.JoinStr(",", "a", "", "b") // "a,,b" huge.JoinStrSkipEmpty(",", "a", "", "b") // "a,b"huge.ReverseStr("abc") // "cba"huge.GetAlphanumericNumByASCII("108條梁山man") // 6 huge.GetAlphanumericNumByASCIIV2("108條梁山man") // 6 huge.GetAlphanumericNumByRegExp("108條梁山man") // 6Struct
Some useful functions can be used to handle string.
Example:
import (huge "github.com/dablelv/go-huge-util" )var st = struct {I intS string }{I: 1, S: "a"}huge.Struct2Map(st) // map[I:1 S:a] huge.Struct2MapString(st) // map[I:1 S:a]Time
Some useful functions can be used to handle date and time.
Example:
import (huge "github.com/dablelv/go-huge-util" )tc := huge.NewTimeCounter() // ... // do your statements tc.GetMs() // get the time cost in millisecondhuge.GetNowDate() // just a example, the same below. 2020-05-16 huge.GetNowTime() // 00:15:42 huge.GetNowDateTime() // 2020-05-16 00:15:42 huge.GetNowDateTimeZ() // 2020-05-16 00:15:42 +08:00huge.GetNowS() // 1589559342 huge.GetNowMs() // 1589559342963 huge.GetNowUs() // 1589559342963062 huge.GetNowNs() // 1589559342964063200huge.GetDayBeginMoment(time.Now()) // 2020-05-16 00:00:00 +0800 CST huge.GetDayEndMoment(time.Now()) // 2020-05-16 23:59:59.999999999 +0800 CSTType Conversion
Some useful functions can be used to convert one type to another types, such as to map or slice.
to another type
// Example to string s, err := ToAny[string]("foo") // "foo" s, err := ToAny[string](8) // "8" s, err := ToAny[string](8.31) // "8.31" s, err := ToAny[string]([]byte("one time")) // "one time" s, err := ToAny[string](nil) // ""var foo any = "one more time" s, err := ToAny[string](foo) // "one more time"// Example to int i, err := ToAny[int](8) // 8 i, err := ToAny[int](8.31) // 8 i, err := ToAny[int]("8") // 8 i, err := ToAny[int](true) // 1 i, err := ToAny[int](false) // 0 i, err := ToAny[int](nil) // 0var eight any = 8 i, err := ToAny[int](eight) // 8// Example to bool b, err := ToAny[bool]("true") // true b, err := ToAny[bool]("false") // false b, err := ToAny[bool]("True") // true b, err := ToAny[bool]("False") // false b, err := ToAny[bool](1) // true b, err := ToAny[bool](0) // false b, err := ToAny[bool](nil) // falsevar one any = 1 b, err := ToAny[bool](one) // trueto set
import ("github.com/dablelv/go-huge-util/conv" )// Convert bool slice or array to set. bools := []bool{true, false, true} set := conv.ToBoolSet(bools) set, _ := conv.ToBoolSetE(bools) set := conv.ToSet[bool](bools) set, _ := conv.ToSetE[bool](bools)// Convert int slice or array to set. ints := []int{1, 2, 3} set := conv.ToIntSet(ints) set, _ := conv.ToIntSetE(ints) set := conv.ToSetG[int](ints) set, _ := conv.ToSetE[int](ints)// Convert string slice or array to set. strs := []string{"foo", "bar", "baz"} set := conv.ToStrSet(strs) set, _ := conv.ToStrSetE(strs) set := conv.ToSet[string](strs) set, _ := conv.ToSetE[string](strs)// Split string to set. conv.SplitStrToSet("a,b,c", ",") // map[a:{}, b:{}, c:{}]to slice
import ("github.com/dablelv/go-huge-util/conv" )// Convert string separated by white space character to string slice. sl := conv.ToStrSlice("a b c") // []string{"a","b","c"} sl, _ := conv.ToStrSliceE("a b c") // []string{"a","b","c"}// Convert int slice or array to string slice. sl := conv.ToStrSlice([]int{1, 2, 3}) // []string{"1","2","3"} sl, _ := conv.ToStrSliceE([]int{1, 2, 3}) // []string{"1","2","3"}// Convert map to slice in random order. ks, vs := conv.Map2Slice(map[int]int{1:1, 2:2, 3:3}) ks, vs, _ := conv.Map2SliceE(map[int]int{1:1, 2:2, 3:3})slK, _ : = ks.([]int) slV, _ : = vs.([]int)ks, vs := conv.Map2Slice(map[string]int{"foo":1, "bar":2, "baz":3}) ks, vs, _ := conv.Map2SliceE(map[string]int{"foo":1, "bar":2, "baz":3})slK, _ : = ks.([]string) slV, _ : = vs.([]int)// Split string to slice. // int[1,2,3] ints := conv.SplitStrToIntSlice("1,2,3", ",") ints := conv.SplitStrToSlice[int]("1,2,3", ",") ints, _ := conv.SplitStrToSliceE[int]("1,2,3", ",") // uint[1,2,3] uints := conv.SplitStrToUintSlice("1,2,3", ",") uints := conv.SplitStrToSlice[uint]("1,2,3", ",") uints, _ := conv.SplitStrToSliceE[uint]("1,2,3", ",") // float64[1.1,2.2,3.3] f64s := conv.SplitStrToFloat64Slice("1.1,2.2,3.3", ",") f64s := conv.SplitStrToSlice[float64]("1.1,2.2,3.3", ",") f64s, _ := conv.SplitStrToSliceE[float64]("1.1,2.2,3.3", ",") // bool[true,false,true,false] bs := conv.SplitStrToBoolSlice("1,0,true,false", ",") bs := conv.SplitStrToSlice[bool]("1,0,true,false", ",") bs, _ := conv.SplitStrToSliceE[bool]("1,0,true,false", ",")URL
Some useful functions can be used to handle url.
Example:
import (huge "github.com/dablelv/go-huge-util" )var rawUrl=`http://www.aspxfans.com:8080/news/index.asp?boardID=520&page=1&page=2#name` huge.RawUrlGetDomain(rawUrl) // "www.aspxfans.com" huge.RawUrlGetPort(rawUrl) // "8080"huge.RawURLGetParam(rawUrl, "page") // 1 <nil> huge.RawURLGetParams(rawUrl, "page") // [1 2] <nil> huge.RawURLGetAllParams(rawUrl) // map[boardID:[520] page:[1 2]] <nil>huge.RawURLAddParam(rawUrl, "keyword", "dog") // http://www.aspxfans.com:8080/news/index.asp?boardID=520&keyword=dog&page=1&page=2#name huge.RawURLDelParam(rawUrl, "page") // http://www.aspxfans.com:8080/news/index.asp?boardID=520#name huge.RawURLSetParam(rawUrl, "boardID", "521") // http://www.aspxfans.com:8080/news/index.asp?boardID=521&page=1&page=2#nameCrypto
Some useful functions can be used to create Hash, HMAC and crypt data.
import (huge "github.com/dablelv/go-huge-util" )// Hash functions huge.MD5L("") // d41d8cd98f00b204e9800998ecf8427e huge.MD5U("") // D41D8CD98F00B204E9800998ECF8427Ehuge.SHA1L("") // da39a3ee5e6b4b0d3255bfef95601890afd80709 huge.SHA1U("") // DA39A3EE5E6B4B0D3255BFEF95601890AFD80709huge.SHA224L("") // d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f huge.SHA224U("") // D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42Fhuge.SHA256L("") // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 huge.SHA256U("") // E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855huge.SHA384L("") // 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b huge.SHA384U("") // 38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95Bhuge.SHA512L("") // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e huge.SHA512U("") // CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E// HMAC functions huge.HMACMD5L("", "") // 74e6f7298a9c2d168935f58c001bad88 huge.HMACMD5U("", "") // 74E6F7298A9C2D168935F58C001BAD88huge.HMACSHA1L("", "") // fbdb1d1b18aa6c08324b7d64b71fb76370690e1d huge.HMACSHA1U("", "") // FBDB1D1B18AA6C08324B7D64B71FB76370690E1Dhuge.HMACSHA224L("", "") // 5ce14f72894662213e2748d2a6ba234b74263910cedde2f5a9271524 huge.HMACSHA224U("", "") // 5CE14F72894662213E2748D2A6BA234B74263910CEDDE2F5A9271524huge.HMACSHA256L("", "") // b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad huge.HMACSHA256U("", "") // B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5ADhuge.HMACSHA384L("", "") // 6c1f2ee938fad2e24bd91298474382ca218c75db3d83e114b3d4367776d14d3551289e75e8209cd4b792302840234adc huge.HMACSHA384U("", "") // 6C1F2EE938FAD2E24BD91298474382CA218C75DB3D83E114B3D4367776D14D3551289E75E8209CD4B792302840234ADChuge.HMACSHA512L("", "") // b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47 huge.HMACSHA512U("", "") // B936CEE86C9F87AA5D3C6F2E84CB5A4239A5FE50480A6EC66B70AB5B1F4AC6730C6C515421B327EC1D69402E53DFB49AD7381EB067B338FD7B0CB22247225D47// Encryption functions p := []byte("plaintext") key16 := []byte("12345678abcdefgh") c, _ := Base64AESCBCEncrypt(p, key16) // A67NhD3RBiNaMgG6HTm8LQ== p, _ = Base64AESCBCDecrypt(c, key16) // plaintextkey8 := []byte("12345678") c, _ := Base64DESCBCEncrypt(p, key8) // UZS/y4By6ksePYMBbvZdig== p, _ := Base64DESCBCDecrypt(c, key8) // plaintextkey24 := []byte("12345678abcdefgh12345678") c, _ := Base64TriDESCBCEncrypt(p, key24) // dau0DzmDGQbHasZaOvxxwg== p, _ := Base64TriDESCBCDecrypt(c, key24) // plaintextRand
Some functions to create a real non-negative random int number, specified length random string, and so on.
huge.GetRandInt() // 2040723487295132865 huge.GetRandIntn(100) // 49 huge.GetRandIntRange(0, 100) // 44 huge.GetRandByteSlice(3) // [241 16 101] huge.GetRandStr(3) // dAt huge.GetRandLowerStr(3) // lts huge.GetRandUpperStr(3) // YUTJSON
Some convert function about to json.
student := struct {Hobby stringAge int32 }{Hobby: "pingpopng",Age: 28, } huge.ToIndentJSON(&student) /* output: {"Hobby": "pingpopng","Age": 28 } */Other
Some useful functions now unclassified. Of course, it may be classified and moved to a new go file in the future.
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") // trueSummary
The above examples is 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.
總結
以上是生活随笔為你收集整理的Golang go-huge-util 工具库介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 团队开发之:代码规范说明
- 下一篇: CrossWalk切换到chromium