{"id":27880855,"url":"https://github.com/atomicgo/utils","last_synced_at":"2025-10-28T07:31:51.543Z","repository":{"id":193599560,"uuid":"689140488","full_name":"atomicgo/utils","owner":"atomicgo","description":"✨ The most sophisticated utils package for Go","archived":false,"fork":false,"pushed_at":"2024-08-08T17:04:43.000Z","size":47,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-09T08:43:33.037Z","etag":null,"topics":["atomicgo","go","golang","golang-library","utils"],"latest_commit_sha":null,"homepage":"https://atomicgo.dev","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/atomicgo.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["MarvinJWendt"]}},"created_at":"2023-09-08T22:56:33.000Z","updated_at":"2024-08-08T17:04:47.000Z","dependencies_parsed_at":"2024-08-08T19:49:09.092Z","dependency_job_id":"955e04c4-1730-4456-98af-0542ca5e6612","html_url":"https://github.com/atomicgo/utils","commit_stats":null,"previous_names":["atomicgo/utils"],"tags_count":2,"template":false,"template_full_name":"atomicgo/template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Futils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Futils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Futils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Futils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atomicgo","download_url":"https://codeload.github.com/atomicgo/utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252442480,"owners_count":21748448,"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":["atomicgo","go","golang","golang-library","utils"],"created_at":"2025-05-05T04:38:50.419Z","updated_at":"2025-10-28T07:31:51.473Z","avatar_url":"https://github.com/atomicgo.png","language":"Go","funding_links":["https://github.com/sponsors/MarvinJWendt"],"categories":[],"sub_categories":[],"readme":"\u003c!--\n\n\n\n┌───────────────────────────────────────────────────────────────────┐\n│                                                                   │\n│                          IMPORTANT NOTE                           │\n│                                                                   │\n│               This file is automatically generated                │\n│           All manual modifications will be overwritten            │\n│                                                                   │\n└───────────────────────────────────────────────────────────────────┘\n\n\n\n--\u003e\n\n\u003ch1 align=\"center\"\u003eAtomicGo | utils\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fatomicgo.dev%2Fapi%2Fshields%2Futils\u0026style=flat-square\" alt=\"Downloads\"\u003e\n\n\u003ca href=\"https://github.com/atomicgo/utils/releases\"\u003e\n\u003cimg src=\"https://img.shields.io/github/v/release/atomicgo/utils?style=flat-square\" alt=\"Latest Release\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/utils\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/atomicgo/utils/go.yml?style=flat-square\" alt=\"Tests\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/utils\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/codecov/c/gh/atomicgo/utils?color=magenta\u0026logo=codecov\u0026style=flat-square\" alt=\"Coverage\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/utils\"\u003e\n\u003c!-- unittestcount:start --\u003e\u003cimg src=\"https://img.shields.io/badge/Unit_Tests-6-magenta?style=flat-square\" alt=\"Unit test count\"\u003e\u003c!-- unittestcount:end --\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://opensource.org/licenses/MIT\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square\" alt=\"License: MIT\"\u003e\n\u003c/a\u003e\n  \n\u003ca href=\"https://goreportcard.com/report/github.com/atomicgo/utils\" target=\"_blank\"\u003e\n\u003cimg src=\"https://goreportcard.com/badge/github.com/atomicgo/utils?style=flat-square\" alt=\"Go report\"\u003e\n\u003c/a\u003e   \n\n\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n\u003cstrong\u003e\u003ca href=\"https://pkg.go.dev/atomicgo.dev/utils#section-documentation\" target=\"_blank\"\u003eDocumentation\u003c/a\u003e\u003c/strong\u003e\n|\n\u003cstrong\u003e\u003ca href=\"https://github.com/atomicgo/atomicgo/blob/main/CONTRIBUTING.md\" target=\"_blank\"\u003eContributing\u003c/a\u003e\u003c/strong\u003e\n|\n\u003cstrong\u003e\u003ca href=\"https://github.com/atomicgo/atomicgo/blob/main/CODE_OF_CONDUCT.md\" target=\"_blank\"\u003eCode of Conduct\u003c/a\u003e\u003c/strong\u003e\n\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/atomicgo/atomicgo/main/assets/header.png\" alt=\"AtomicGo\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/p\u003e\n\u003ch3  align=\"center\"\u003e\u003cpre\u003ego get atomicgo.dev/utils\u003c/pre\u003e\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/p\u003e\n\n\u003c!-- gomarkdoc:embed:start --\u003e\n\n\u003c!-- Code generated by gomarkdoc. DO NOT EDIT --\u003e\n\n# utils\n\n```go\nimport \"atomicgo.dev/utils\"\n```\n\nPackage utils is a collection of useful, quickly accessible utility functions.\n\n## Index\n\n- [func AppendToFile\\[T string | \\[\\]byte\\]\\(path string, content T\\) error](\u003c#AppendToFile\u003e)\n- [func DownloadFile\\(url, path string\\) error](\u003c#DownloadFile\u003e)\n- [func Fetch\\(url string\\) \\(string, error\\)](\u003c#Fetch\u003e)\n- [func FileExists\\(path string\\) bool](\u003c#FileExists\u003e)\n- [func PrettyJSON\\(inputJSON string, indent ...string\\) \\(string, error\\)](\u003c#PrettyJSON\u003e)\n- [func ReadFile\\(path string\\) \\(string, error\\)](\u003c#ReadFile\u003e)\n- [func Ternary\\[T any\\]\\(condition bool, a, b T\\) T](\u003c#Ternary\u003e)\n- [func ToInt\\[T string | constraints.Number\\]\\(value T\\) int](\u003c#ToInt\u003e)\n- [func ToJSON\\(v any\\) \\(string, error\\)](\u003c#ToJSON\u003e)\n- [func ToPrettyJSON\\(v any, indent ...string\\) \\(string, error\\)](\u003c#ToPrettyJSON\u003e)\n- [func ToString\\(v any\\) string](\u003c#ToString\u003e)\n- [func WriteFile\\[T string | \\[\\]byte\\]\\(path string, content T\\) error](\u003c#WriteFile\u003e)\n\n\n\u003ca name=\"AppendToFile\"\u003e\u003c/a\u003e\n## func [AppendToFile](\u003chttps://github.com/atomicgo/utils/blob/main/file.go#L33\u003e)\n\n```go\nfunc AppendToFile[T string | []byte](path string, content T) error\n```\n\nAppendToFile appends the given content to the given file. Accepts a string or a byte slice.\n\n\u003ca name=\"DownloadFile\"\u003e\u003c/a\u003e\n## func [DownloadFile](\u003chttps://github.com/atomicgo/utils/blob/main/file.go#L55\u003e)\n\n```go\nfunc DownloadFile(url, path string) error\n```\n\nDownloadFile downloads the given URL to the given path. If the file already exists, it will be overwritten.\n\n\u003ca name=\"Fetch\"\u003e\u003c/a\u003e\n## func [Fetch](\u003chttps://github.com/atomicgo/utils/blob/main/utils.go#L38\u003e)\n\n```go\nfunc Fetch(url string) (string, error)\n```\n\nFetch returns the body of a GET request to the given URL.\n\n\u003ca name=\"FileExists\"\u003e\u003c/a\u003e\n## func [FileExists](\u003chttps://github.com/atomicgo/utils/blob/main/file.go#L48\u003e)\n\n```go\nfunc FileExists(path string) bool\n```\n\nFileExists returns true if the given file exists.\n\n\u003ca name=\"PrettyJSON\"\u003e\u003c/a\u003e\n## func [PrettyJSON](\u003chttps://github.com/atomicgo/utils/blob/main/utils.go#L22\u003e)\n\n```go\nfunc PrettyJSON(inputJSON string, indent ...string) (string, error)\n```\n\nPrettyJSON returns a pretty\\-printed JSON string. If indent is not provided, it defaults to \" \" \\(two spaces\\).\n\n\n\n\n\n```go\nperson := Person{Name: \"John Doe\", Age: 42}\njson, _ := utils.ToJSON(person)\nprettyJSON, _ := utils.PrettyJSON(json)\nfmt.Println(prettyJSON)\n\n// Output:\n// {\n//   \"Name\": \"John Doe\",\n//   \"Age\": 42\n// }\n```\n\n#### Output\n\n```\n{\n  \"Name\": \"John Doe\",\n  \"Age\": 42\n}\n```\n\n\n\n\u003ca name=\"ReadFile\"\u003e\u003c/a\u003e\n## func [ReadFile](\u003chttps://github.com/atomicgo/utils/blob/main/file.go#L11\u003e)\n\n```go\nfunc ReadFile(path string) (string, error)\n```\n\nReadFile reads the given file and returns its content.\n\n\u003ca name=\"Ternary\"\u003e\u003c/a\u003e\n## func [Ternary](\u003chttps://github.com/atomicgo/utils/blob/main/utils.go#L12\u003e)\n\n```go\nfunc Ternary[T any](condition bool, a, b T) T\n```\n\nTernary is a ternary operator. It returns a if the condition is true, otherwise it returns b.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/utils\"\n)\n\nfunc main() {\n\tfmt.Println(utils.Ternary(true, \"a\", \"b\"))\n\tfmt.Println(utils.Ternary(false, \"a\", \"b\"))\n\n}\n```\n\n#### Output\n\n```\na\nb\n```\n\n\n\n\u003ca name=\"ToInt\"\u003e\u003c/a\u003e\n## func [ToInt](\u003chttps://github.com/atomicgo/utils/blob/main/to.go#L42\u003e)\n\n```go\nfunc ToInt[T string | constraints.Number](value T) int\n```\n\nToInt converts the given value to an int. If the value is a float, it will be rounded to the nearest integer. \\(Rounds up if the decimal is 0.5 or higher\\)\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/utils\"\n)\n\nfunc main() {\n\tfmt.Println(utils.ToInt(1337))\n\tfmt.Println(utils.ToInt(1337.4))\n\tfmt.Println(utils.ToInt(1337.5))\n\tfmt.Println(utils.ToInt(1337.6))\n\tfmt.Println(utils.ToInt(\"1337\"))\n\tfmt.Println(utils.ToInt(\"1337.4\"))\n\tfmt.Println(utils.ToInt(\"1337.5\"))\n\tfmt.Println(utils.ToInt(\"1337.6\"))\n\n}\n```\n\n#### Output\n\n```\n1337\n1337\n1338\n1338\n1337\n1337\n1338\n1338\n```\n\n\n\n\u003ca name=\"ToJSON\"\u003e\u003c/a\u003e\n## func [ToJSON](\u003chttps://github.com/atomicgo/utils/blob/main/to.go#L13\u003e)\n\n```go\nfunc ToJSON(v any) (string, error)\n```\n\nToJSON converts the given value to a JSON string.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/utils\"\n)\n\ntype Person struct {\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\tperson := Person{\"John Doe\", 42}\n\n\tjson, _ := utils.ToJSON(person)\n\tfmt.Println(json)\n\n}\n```\n\n#### Output\n\n```\n{\"Name\":\"John Doe\",\"Age\":42}\n```\n\n\n\n\u003ca name=\"ToPrettyJSON\"\u003e\u003c/a\u003e\n## func [ToPrettyJSON](\u003chttps://github.com/atomicgo/utils/blob/main/to.go#L22\u003e)\n\n```go\nfunc ToPrettyJSON(v any, indent ...string) (string, error)\n```\n\n\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/utils\"\n)\n\ntype Person struct {\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\tperson := Person{Name: \"John Doe\", Age: 42}\n\tprettyJSON, _ := utils.ToPrettyJSON(person)\n\tfmt.Println(prettyJSON)\n\n}\n```\n\n#### Output\n\n```\n{\n  \"Name\": \"John Doe\",\n  \"Age\": 42\n}\n```\n\n\n\n\u003ca name=\"ToString\"\u003e\u003c/a\u003e\n## func [ToString](\u003chttps://github.com/atomicgo/utils/blob/main/to.go#L36\u003e)\n\n```go\nfunc ToString(v any) string\n```\n\nToString converts the given value to a string.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/utils\"\n)\n\ntype Person struct {\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\tperson := Person{\"John Doe\", 42}\n\n\tfmt.Println(utils.ToString(person))\n\n}\n```\n\n#### Output\n\n```\n{John Doe 42}\n```\n\n\n\n\u003ca name=\"WriteFile\"\u003e\u003c/a\u003e\n## func [WriteFile](\u003chttps://github.com/atomicgo/utils/blob/main/file.go#L22\u003e)\n\n```go\nfunc WriteFile[T string | []byte](path string, content T) error\n```\n\nWriteFile writes the given content to the given file. Accepts a string or a byte slice.\n\nGenerated by [gomarkdoc](\u003chttps://github.com/princjef/gomarkdoc\u003e)\n\n\n\u003c!-- gomarkdoc:embed:end --\u003e\n\n---\n\n\u003e [AtomicGo.dev](https://atomicgo.dev) \u0026nbsp;\u0026middot;\u0026nbsp;\n\u003e with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |\n\u003e [MarvinJWendt.com](https://marvinjwendt.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicgo%2Futils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatomicgo%2Futils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicgo%2Futils/lists"}