{"id":13493154,"url":"https://github.com/spf13/cast","last_synced_at":"2025-09-09T21:05:32.616Z","repository":{"id":15675916,"uuid":"18413405","full_name":"spf13/cast","owner":"spf13","description":"safe and easy casting from one type to another in Go ","archived":false,"fork":false,"pushed_at":"2025-05-05T11:29:57.000Z","size":147,"stargazers_count":3704,"open_issues_count":90,"forks_count":315,"subscribers_count":37,"default_branch":"master","last_synced_at":"2025-05-05T20:51:28.725Z","etag":null,"topics":[],"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/spf13.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,"zenodo":null}},"created_at":"2014-04-03T18:21:16.000Z","updated_at":"2025-05-05T09:38:12.000Z","dependencies_parsed_at":"2023-02-19T02:31:49.044Z","dependency_job_id":"a2e6a0d4-d097-46a6-862c-0bfdb88dfe22","html_url":"https://github.com/spf13/cast","commit_stats":{"total_commits":103,"total_committers":32,"mean_commits":3.21875,"dds":0.7184466019417476,"last_synced_commit":"487df0093482b2ca1c00cb3b92e30899c37038a1"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spf13%2Fcast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spf13%2Fcast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spf13%2Fcast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spf13%2Fcast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spf13","download_url":"https://codeload.github.com/spf13/cast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253929358,"owners_count":21985802,"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":[],"created_at":"2024-07-31T19:01:12.744Z","updated_at":"2025-05-13T11:02:09.091Z","avatar_url":"https://github.com/spf13.png","language":"Go","funding_links":[],"categories":["Go","开源类库","Open source library","others"],"sub_categories":["开发辅助包","Development Aid Package"],"readme":"# cast\n\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spf13/cast/ci.yaml?style=flat-square)](https://github.com/spf13/cast/actions/workflows/ci.yaml)\n[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\u0026style=flat-square)](https://pkg.go.dev/mod/github.com/spf13/cast)\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/spf13/cast?style=flat-square\u0026color=61CFDD)\n[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/spf13/cast/badge?style=flat-square)](https://deps.dev/go/github.com%252Fspf13%252Fcast)\n\nEasy and safe casting from one type to another in Go\n\nDon’t Panic! ... Cast\n\n## What is Cast?\n\nCast is a library to convert between different go types in a consistent and easy way.\n\nCast provides simple functions to easily convert a number to a string, an\ninterface into a bool, etc. Cast does this intelligently when an obvious\nconversion is possible. It doesn’t make any attempts to guess what you meant,\nfor example you can only convert a string to an int when it is a string\nrepresentation of an int such as “8”. Cast was developed for use in\n[Hugo](https://gohugo.io), a website engine which uses YAML, TOML or JSON\nfor meta data.\n\n## Why use Cast?\n\nWhen working with dynamic data in Go you often need to cast or convert the data\nfrom one type into another. Cast goes beyond just using type assertion (though\nit uses that when possible) to provide a very straightforward and convenient\nlibrary.\n\nIf you are working with interfaces to handle things like dynamic content\nyou’ll need an easy way to convert an interface into a given type. This\nis the library for you.\n\nIf you are taking in data from YAML, TOML or JSON or other formats which lack\nfull types, then Cast is the library for you.\n\n## Usage\n\nCast provides a handful of To_____ methods. These methods will always return\nthe desired type. **If input is provided that will not convert to that type, the\n0 or nil value for that type will be returned**.\n\nCast also provides identical methods To_____E. These return the same result as\nthe To_____ methods, plus an additional error which tells you if it successfully\nconverted. Using these methods you can tell the difference between when the\ninput matched the zero value or when the conversion failed and the zero value\nwas returned.\n\nThe following examples are merely a sample of what is available. Please review\nthe code for a complete set.\n\n### Example ‘ToString’:\n\n    cast.ToString(\"mayonegg\")         // \"mayonegg\"\n    cast.ToString(8)                  // \"8\"\n    cast.ToString(8.31)               // \"8.31\"\n    cast.ToString([]byte(\"one time\")) // \"one time\"\n    cast.ToString(nil)                // \"\"\n\n\tvar foo interface{} = \"one more time\"\n    cast.ToString(foo)                // \"one more time\"\n\n\n### Example ‘ToInt’:\n\n    cast.ToInt(8)                  // 8\n    cast.ToInt(8.31)               // 8\n    cast.ToInt(\"8\")                // 8\n    cast.ToInt(true)               // 1\n    cast.ToInt(false)              // 0\n\n\tvar eight interface{} = 8\n    cast.ToInt(eight)              // 8\n    cast.ToInt(nil)                // 0\n\n## License\n\nThe project is licensed under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspf13%2Fcast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspf13%2Fcast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspf13%2Fcast/lists"}