Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A collection of useful utility functions

License

NotificationsYou must be signed in to change notification settings

shomali11/util

Repository files navigation

A group of generic useful utility functions

Dependencies

Examples

Concurrency

package mainimport ("github.com/shomali11/util/xconcurrency""time""fmt")funcmain() {func1:=func()error {forchar:='a';char<'a'+3;char++ {fmt.Printf("%c ",char)}returnnil    }func2:=func()error {fornumber:=1;number<4;number++ {fmt.Printf("%d ",number)}returnnil    }xconcurrency.Parallelize(func1,func2)// a 1 b 2 c 3xconcurrency.ParallelizeTimeout(time.Minute,func1,func2)// a 1 b 2 c 3}

Hashes

package mainimport ("github.com/shomali11/util/xhashes""fmt")funcmain() {fmt.Println(xhashes.FNV32("Raed Shomali"))// 424383802fmt.Println(xhashes.FNV32a("Raed Shomali"))// 3711360776fmt.Println(xhashes.FNV64("Raed Shomali"))// 13852322269024953050fmt.Println(xhashes.FNV64a("Raed Shomali"))// 17869303103005344072fmt.Println(xhashes.MD5("Raed Shomali"))// c313bc3b48fcfed9abc733429665b105fmt.Println(xhashes.SHA1("Raed Shomali"))// e0d66f6f09de72942e83289cc994b3c721ab34c5fmt.Println(xhashes.SHA256("Raed Shomali"))// 75894b9be21065a833e57bfe4440b375fc216f120a965243c9be8b2dc36709c2fmt.Println(xhashes.SHA512("Raed Shomali"))// 406e8d495140187a8b09893c30d054cf385ad7359855db0d2e0386c7189ac1c4667a4816d1b63a19f3d8ccdcbace7861ec4cc6ff5e2a1659c8f4360bda699b42}

Compressions

package mainimport ("github.com/shomali11/util/xcompressions""fmt")funcmain() {fmt.Println(xcompressions.Compress([]byte("Raed Shomali")))}

Encodings

package mainimport ("github.com/shomali11/util/xencodings""fmt")funcmain() {fmt.Println(xencodings.Base32Encode([]byte("Raed Shomali")))fmt.Println(xencodings.Base64Encode([]byte("Raed Shomali")))}

Strings

package mainimport ("github.com/shomali11/util/xstrings""fmt")funcmain() {fmt.Println(xstrings.IsEmpty(""))// truefmt.Println(xstrings.IsEmpty("text"))// falsefmt.Println(xstrings.IsEmpty(""))// falsefmt.Println(xstrings.IsNotEmpty(""))// falsefmt.Println(xstrings.IsNotEmpty("text"))// truefmt.Println(xstrings.IsNotEmpty(""))// truefmt.Println(xstrings.IsBlank(""))// truefmt.Println(xstrings.IsBlank(""))// truefmt.Println(xstrings.IsBlank("text"))// falsefmt.Println(xstrings.IsNotBlank(""))// falsefmt.Println(xstrings.IsNotBlank(""))// falsefmt.Println(xstrings.IsNotBlank("text"))// truefmt.Println(xstrings.Left("",5))// "     "fmt.Println(xstrings.Left("X",5))// "X    "fmt.Println(xstrings.Left("😎⚽",4))// "😎⚽  "fmt.Println(xstrings.Left("ab\u0301cde",8))// "ab́cde   "fmt.Println(xstrings.Right("",5))// "     "fmt.Println(xstrings.Right("X",5))// "    X"fmt.Println(xstrings.Right("😎⚽",4))// "  😎⚽"fmt.Println(xstrings.Right("ab\u0301cde",8))// "   ab́cde"fmt.Println(xstrings.Center("",5))// "     "fmt.Println(xstrings.Center("X",5))// "  X  "fmt.Println(xstrings.Center("😎⚽",4))// " 😎⚽ "fmt.Println(xstrings.Center("ab\u0301cde",8))// "  ab́cde "fmt.Println(xstrings.Length(""))// 0fmt.Println(xstrings.Length("X"))// 1fmt.Println(xstrings.Length("b\u0301"))// 1fmt.Println(xstrings.Length("😎⚽"))// 2fmt.Println(xstrings.Length("Les Mise\u0301rables"))// 14fmt.Println(xstrings.Length("ab\u0301cde"))// 5fmt.Println(xstrings.Length("This `\xc5` is an invalid UTF8 character"))// 37fmt.Println(xstrings.Length("The quick bròwn 狐 jumped over the lazy 犬"))// 40fmt.Println(xstrings.Reverse(""))// ""fmt.Println(xstrings.Reverse("X"))// "X"fmt.Println(xstrings.Reverse("😎⚽"))// "⚽😎"fmt.Println(xstrings.Reverse("Les Mise\u0301rables"))// "selbare\u0301siM seL"fmt.Println(xstrings.Reverse("This `\xc5` is an invalid UTF8 character"))// "retcarahc 8FTU dilavni na si `�` sihT"fmt.Println(xstrings.Reverse("The quick bròwn 狐 jumped over the lazy 犬"))// "犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT"}

Conditions

package mainimport ("github.com/shomali11/util/xconditions""fmt")funcmain() {fmt.Println(xconditions.IfThen(1==1,"Yes"))// "Yes"fmt.Println(xconditions.IfThen(1!=1,"Woo"))// nilfmt.Println(xconditions.IfThen(1<2,"Less"))// "Less"fmt.Println(xconditions.IfThenElse(1==1,"Yes",false))// "Yes"fmt.Println(xconditions.IfThenElse(1!=1,nil,1))// 1fmt.Println(xconditions.IfThenElse(1<2,nil,"No"))// nilfmt.Println(xconditions.DefaultIfNil(nil,nil))// nilfmt.Println(xconditions.DefaultIfNil(nil,""))// ""fmt.Println(xconditions.DefaultIfNil("A","B"))// "A"fmt.Println(xconditions.DefaultIfNil(true,"B"))// truefmt.Println(xconditions.DefaultIfNil(1,false))// 1fmt.Println(xconditions.FirstNonNil(nil,nil))// nilfmt.Println(xconditions.FirstNonNil(nil,""))// ""fmt.Println(xconditions.FirstNonNil("A","B"))// "A"fmt.Println(xconditions.FirstNonNil(true,"B"))// truefmt.Println(xconditions.FirstNonNil(1,false))// 1fmt.Println(xconditions.FirstNonNil(nil,nil,nil,10))// 10fmt.Println(xconditions.FirstNonNil(nil,nil,nil,nil,nil))// nilfmt.Println(xconditions.FirstNonNil())// nil}

Errors

package mainimport ("github.com/shomali11/util/xerrors""fmt")funcmain() {fmt.Println(xerrors.DefaultErrorIfNil(nil,"Cool"))// "Cool"fmt.Println(xerrors.DefaultErrorIfNil(errors.New("Oops"),"Cool"))// "Oops"}

Manipulations

package mainimport ("github.com/shomali11/util/xmanipulations""math/rand""time""fmt")funcmain() {source:=rand.NewSource(time.Now().UnixNano())array:= []interface{}{"a","b","c"}xmanipulations.Shuffle(array,source)fmt.Println(array)// [c b a]}

Conversions

Return a pretty JSON representation of any interface

package mainimport ("github.com/shomali11/util/xconversions""fmt")funcmain() {x:=map[string]interface{}{"number":1,"string":"cool","bool":true,"float":1.5}fmt.Println(xconversions.PrettyJson(x))}
{"bool":true,"float":1.5,"number":1,"string":"cool"}

Convert any interface to a String

package mainimport ("github.com/shomali11/util/xconversions""fmt")funcmain() {x:=map[string]interface{}{"number":1,"string":"cool","bool":true,"float":1.5}fmt.Println(xconversions.Stringify(x))}
{"bool":true,"float":1.5,"number":1,"string":"cool"} <nil>

Convert any string back to its original struct

package mainimport ("github.com/shomali11/util/xconversions""fmt")funcmain() {x:="{\"bool\":true,\"float\":1.5,\"number\":1,\"string\":\"cool\"}"varresultsmap[string]interface{}fmt.Println(xconversions.Structify(x,&results))fmt.Println(results)}
<nil>map[bool:true float:1.5 number:1 string:cool]

[8]ページ先頭

©2009-2025 Movatter.jp