{"id":13582525,"url":"https://github.com/shomali11/util","last_synced_at":"2025-04-06T01:07:50.381Z","repository":{"id":45895538,"uuid":"92232508","full_name":"shomali11/util","owner":"shomali11","description":"A collection of useful utility functions","archived":false,"fork":false,"pushed_at":"2022-07-17T17:51:30.000Z","size":138,"stargazers_count":298,"open_issues_count":1,"forks_count":35,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-29T23:11:11.668Z","etag":null,"topics":["compression","concurrency","concurrent","condition","conditional","conditional-statements","conditions","conversion","conversions","decompression","errors","hash","hashing","parallel","parallelism","string","string-manipulation","strings","utilities","utility"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shomali11.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-24T00:21:29.000Z","updated_at":"2025-03-27T23:53:06.000Z","dependencies_parsed_at":"2022-08-03T13:00:12.747Z","dependency_job_id":null,"html_url":"https://github.com/shomali11/util","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shomali11%2Futil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shomali11%2Futil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shomali11%2Futil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shomali11%2Futil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shomali11","download_url":"https://codeload.github.com/shomali11/util/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419860,"owners_count":20936012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compression","concurrency","concurrent","condition","conditional","conditional-statements","conditions","conversion","conversions","decompression","errors","hash","hashing","parallel","parallelism","string","string-manipulation","strings","utilities","utility"],"created_at":"2024-08-01T15:02:47.849Z","updated_at":"2025-04-06T01:07:50.363Z","avatar_url":"https://github.com/shomali11.png","language":"Go","readme":"# util [![Build Status](https://travis-ci.com/shomali11/util.svg?branch=master)](https://travis-ci.com/shomali11/util) [![Go Report Card](https://goreportcard.com/badge/github.com/shomali11/util)](https://goreportcard.com/report/github.com/shomali11/util) [![GoDoc](https://godoc.org/github.com/shomali11/util?status.svg)](https://godoc.org/github.com/shomali11/util) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA group of generic useful utility functions\n\n## Dependencies\n\n* `parallelizer` [github.com/shomali11/parallelizer](https://github.com/shomali11/parallelizer)\n\n\n# Examples\n\n## Concurrency\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xconcurrency\"\n\t\"time\"\n\t\"fmt\"\n)\n\nfunc main() {\n    func1 := func() error {\n\t\tfor char := 'a'; char \u003c 'a' + 3; char++ {\n\t\t\tfmt.Printf(\"%c \", char)\n\t\t}\n\t\treturn nil\n    }\n    \n    func2 := func() error {\n\t\tfor number := 1; number \u003c 4; number++ {\n\t\t\tfmt.Printf(\"%d \", number)\n\t\t}\n\t\treturn nil\n    }\n    \n    xconcurrency.Parallelize(func1, func2)  // a 1 b 2 c 3\n    \n    xconcurrency.ParallelizeTimeout(time.Minute, func1, func2)  // a 1 b 2 c 3\n}\n```\n\n## Hashes\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xhashes\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(xhashes.FNV32(\"Raed Shomali\"))  // 424383802\n\tfmt.Println(xhashes.FNV32a(\"Raed Shomali\")) // 3711360776\n\tfmt.Println(xhashes.FNV64(\"Raed Shomali\"))  // 13852322269024953050\n\tfmt.Println(xhashes.FNV64a(\"Raed Shomali\")) // 17869303103005344072\n\tfmt.Println(xhashes.MD5(\"Raed Shomali\"))    // c313bc3b48fcfed9abc733429665b105\n\tfmt.Println(xhashes.SHA1(\"Raed Shomali\"))   // e0d66f6f09de72942e83289cc994b3c721ab34c5\n\tfmt.Println(xhashes.SHA256(\"Raed Shomali\")) // 75894b9be21065a833e57bfe4440b375fc216f120a965243c9be8b2dc36709c2\n\tfmt.Println(xhashes.SHA512(\"Raed Shomali\")) // 406e8d495140187a8b09893c30d054cf385ad7359855db0d2e0386c7189ac1c4667a4816d1b63a19f3d8ccdcbace7861ec4cc6ff5e2a1659c8f4360bda699b42\n}\n```\n\n## Compressions\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xcompressions\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(xcompressions.Compress([]byte(\"Raed Shomali\")))\n}\n```\n\n## Encodings\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xencodings\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(xencodings.Base32Encode([]byte(\"Raed Shomali\")))\n\tfmt.Println(xencodings.Base64Encode([]byte(\"Raed Shomali\")))\n}\n```\n\n## Strings\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xstrings\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(xstrings.IsEmpty(\"\"))     // true\n\tfmt.Println(xstrings.IsEmpty(\"text\")) // false\n\tfmt.Println(xstrings.IsEmpty(\"\t\"))  // false\n\n\tfmt.Println(xstrings.IsNotEmpty(\"\"))     // false\n\tfmt.Println(xstrings.IsNotEmpty(\"text\")) // true\n\tfmt.Println(xstrings.IsNotEmpty(\"\t\")) // true\n\n\tfmt.Println(xstrings.IsBlank(\"\"))     // true\n\tfmt.Println(xstrings.IsBlank(\"\t\"))  // true\n\tfmt.Println(xstrings.IsBlank(\"text\")) // false\n\n\tfmt.Println(xstrings.IsNotBlank(\"\"))     // false\n\tfmt.Println(xstrings.IsNotBlank(\"\t\")) // false\n\tfmt.Println(xstrings.IsNotBlank(\"text\")) // true\n\n\tfmt.Println(xstrings.Left(\"\", 5))            // \"     \"\n\tfmt.Println(xstrings.Left(\"X\", 5))           // \"X    \"\n\tfmt.Println(xstrings.Left(\"😎⚽\", 4))        // \"😎⚽  \"\n\tfmt.Println(xstrings.Left(\"ab\\u0301cde\", 8)) // \"ab́cde   \"\n\n\tfmt.Println(xstrings.Right(\"\", 5))            // \"     \"\n\tfmt.Println(xstrings.Right(\"X\", 5))           // \"    X\"\n\tfmt.Println(xstrings.Right(\"😎⚽\", 4))        // \"  😎⚽\"\n\tfmt.Println(xstrings.Right(\"ab\\u0301cde\", 8)) // \"   ab́cde\"\n\n\tfmt.Println(xstrings.Center(\"\", 5))            // \"     \"\n\tfmt.Println(xstrings.Center(\"X\", 5))           // \"  X  \"\n\tfmt.Println(xstrings.Center(\"😎⚽\", 4))        // \" 😎⚽ \"\n\tfmt.Println(xstrings.Center(\"ab\\u0301cde\", 8)) // \"  ab́cde \"\n\n\tfmt.Println(xstrings.Length(\"\"))                                          // 0\n\tfmt.Println(xstrings.Length(\"X\"))                                         // 1\n\tfmt.Println(xstrings.Length(\"b\\u0301\"))                                   // 1\n\tfmt.Println(xstrings.Length(\"😎⚽\"))                                      // 2\n\tfmt.Println(xstrings.Length(\"Les Mise\\u0301rables\"))                      // 14\n\tfmt.Println(xstrings.Length(\"ab\\u0301cde\"))                               // 5\n\tfmt.Println(xstrings.Length(\"This `\\xc5` is an invalid UTF8 character\"))  // 37\n\tfmt.Println(xstrings.Length(\"The quick bròwn 狐 jumped over the lazy 犬\")) // 40\n\n\tfmt.Println(xstrings.Reverse(\"\"))                                            // \"\"\n\tfmt.Println(xstrings.Reverse(\"X\"))                                           // \"X\"\n\tfmt.Println(xstrings.Reverse(\"😎⚽\"))                                        // \"⚽😎\"\n\tfmt.Println(xstrings.Reverse(\"Les Mise\\u0301rables\"))                        // \"selbare\\u0301siM seL\"\n\tfmt.Println(xstrings.Reverse(\"This `\\xc5` is an invalid UTF8 character\"))    // \"retcarahc 8FTU dilavni na si `�` sihT\"\n\tfmt.Println(xstrings.Reverse(\"The quick bròwn 狐 jumped over the lazy 犬\"))  // \"犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT\"\n}\n```\n\n## Conditions\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xconditions\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(xconditions.IfThen(1 == 1, \"Yes\")) // \"Yes\"\n\tfmt.Println(xconditions.IfThen(1 != 1, \"Woo\")) // nil\n\tfmt.Println(xconditions.IfThen(1 \u003c 2, \"Less\")) // \"Less\"\n\n\tfmt.Println(xconditions.IfThenElse(1 == 1, \"Yes\", false)) // \"Yes\"\n\tfmt.Println(xconditions.IfThenElse(1 != 1, nil, 1))       // 1\n\tfmt.Println(xconditions.IfThenElse(1 \u003c 2, nil, \"No\"))     // nil\n\n\tfmt.Println(xconditions.DefaultIfNil(nil, nil))  // nil\n\tfmt.Println(xconditions.DefaultIfNil(nil, \"\"))   // \"\"\n\tfmt.Println(xconditions.DefaultIfNil(\"A\", \"B\"))  // \"A\"\n\tfmt.Println(xconditions.DefaultIfNil(true, \"B\")) // true\n\tfmt.Println(xconditions.DefaultIfNil(1, false))  // 1\n\n\tfmt.Println(xconditions.FirstNonNil(nil, nil))                // nil\n\tfmt.Println(xconditions.FirstNonNil(nil, \"\"))                 // \"\"\n\tfmt.Println(xconditions.FirstNonNil(\"A\", \"B\"))                // \"A\"\n\tfmt.Println(xconditions.FirstNonNil(true, \"B\"))               // true\n\tfmt.Println(xconditions.FirstNonNil(1, false))                // 1\n\tfmt.Println(xconditions.FirstNonNil(nil, nil, nil, 10))       // 10\n\tfmt.Println(xconditions.FirstNonNil(nil, nil, nil, nil, nil)) // nil\n\tfmt.Println(xconditions.FirstNonNil())                        // nil\n}\n```\n\n## Errors\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xerrors\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Println(xerrors.DefaultErrorIfNil(nil, \"Cool\"))                // \"Cool\"\n\tfmt.Println(xerrors.DefaultErrorIfNil(errors.New(\"Oops\"), \"Cool\")) // \"Oops\"\n}\n```\n\n## Manipulations\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xmanipulations\"\n\t\"math/rand\"\n\t\"time\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tsource := rand.NewSource(time.Now().UnixNano())\n\n\tarray := []interface{}{\"a\", \"b\", \"c\"}\n\txmanipulations.Shuffle(array, source)\n\n\tfmt.Println(array) // [c b a]\n}\n```\n\n## Conversions\n\nReturn a pretty JSON representation of any interface\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xconversions\"\n\t\"fmt\"\n)\n\nfunc main() {\n    x := map[string]interface{}{\"number\": 1, \"string\": \"cool\", \"bool\": true, \"float\": 1.5}    \n    fmt.Println(xconversions.PrettyJson(x))\n}\n```\n\n```json\n{\n\t\"bool\": true,\n\t\"float\": 1.5,\n\t\"number\": 1,\n\t\"string\": \"cool\"\n}\n```\n\nConvert any interface to a String\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xconversions\"\n\t\"fmt\"\n)\n\nfunc main() {\n    x := map[string]interface{}{\"number\": 1, \"string\": \"cool\", \"bool\": true, \"float\": 1.5}    \n    fmt.Println(xconversions.Stringify(x))\n}\n```\n\n```\n{\"bool\":true,\"float\":1.5,\"number\":1,\"string\":\"cool\"} \u003cnil\u003e\n```\n\nConvert any string back to its original struct\n\n```go\npackage main\n\nimport (\n\t\"github.com/shomali11/util/xconversions\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tx := \"{\\\"bool\\\":true,\\\"float\\\":1.5,\\\"number\\\":1,\\\"string\\\":\\\"cool\\\"}\"\n\tvar results map[string]interface{}\n    fmt.Println(xconversions.Structify(x, \u0026results))\n    fmt.Println(results)\n}\n```\n\n```\n\u003cnil\u003e\nmap[bool:true float:1.5 number:1 string:cool]\n```\n","funding_links":[],"categories":["工具库","Utilities","实用工具","Utility","Go","公用事业公司","實用工具","工具库`可以提升效率的通用代码库和工具`"],"sub_categories":["查询语","Utility/Miscellaneous","高级控制台界面","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","Fail injection","实用程序/Miscellaneous","HTTP Clients","Advanced Console UIs","高級控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshomali11%2Futil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshomali11%2Futil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshomali11%2Futil/lists"}