{"id":13670118,"url":"https://github.com/demdxx/gocast","last_synced_at":"2025-04-27T09:31:47.007Z","repository":{"id":17883673,"uuid":"20827904","full_name":"demdxx/gocast","owner":"demdxx","description":"Simple basic types cast for GO","archived":false,"fork":false,"pushed_at":"2024-04-18T11:49:41.000Z","size":98,"stargazers_count":23,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-03T09:07:02.344Z","etag":null,"topics":["cast","go","golang","types"],"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/demdxx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","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}},"created_at":"2014-06-14T08:03:45.000Z","updated_at":"2024-07-01T17:32:44.000Z","dependencies_parsed_at":"2024-03-29T11:45:40.941Z","dependency_job_id":"fc71b995-322f-4c84-8650-822799c3ec43","html_url":"https://github.com/demdxx/gocast","commit_stats":{"total_commits":52,"total_committers":2,"mean_commits":26.0,"dds":0.05769230769230771,"last_synced_commit":"0727649276fd49cd63f8ab651b747ee9a8e5573f"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/demdxx%2Fgocast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/demdxx%2Fgocast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/demdxx%2Fgocast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/demdxx%2Fgocast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/demdxx","download_url":"https://codeload.github.com/demdxx/gocast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224067017,"owners_count":17250098,"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":["cast","go","golang","types"],"created_at":"2024-08-02T09:00:33.020Z","updated_at":"2024-11-11T07:31:10.877Z","avatar_url":"https://github.com/demdxx.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# GoCast\n\n[![GoDoc](https://godoc.org/github.com/demdxx/gocast?status.svg)](https://godoc.org/github.com/demdxx/gocast)\n[![Build Status](https://github.com/demdxx/gocast/workflows/Tests/badge.svg)](https://github.com/demdxx/gocast/actions?workflow=Tests)\n[![Go Report Card](https://goreportcard.com/badge/github.com/demdxx/gocast)](https://goreportcard.com/report/github.com/demdxx/gocast)\n[![Coverage Status](https://coveralls.io/repos/github/demdxx/gocast/badge.svg?branch=master)](https://coveralls.io/github/demdxx/gocast?branch=master)\n\n## Introduction\n\nGoCast is a powerful Go library that allows you to easily convert between different basic types in a consistent and efficient way. Whether you need to convert strings, numbers, or other basic types, GoCast has got you covered.\n\n## Features\n\n- **Universal Type Casting**: GoCast provides a set of methods for universal type casting, making it easy to convert data between various types.\n- **Struct Field Manipulation**: GoCast allows you to set and retrieve values of struct fields dynamically, making it a valuable tool for working with complex data structures.\n- **Custom Type Support**: You can define custom types and implement your own conversion logic, giving you full control over how data is cast.\n\n## Installation\n\nTo use GoCast in your Go project, simply import it:\n\n```go\nimport \"github.com/demdxx/gocast/v2\"\n```\n\n## Usage Example\n\nHere are some examples of how you can use GoCast:\n\n```go\n// Example string casting:\ngocast.Str(\"strasstr\")           // \"strasstr\"\ngocast.Str(8)                    // \"8\"\ngocast.Str(8.31)                 // \"8.31\"\ngocast.Str([]byte(\"one time\"))   // \"one time\"\ngocast.Str(nil)                  // \"\"\n\n// Example number casting:\ngocast.Number[int](8)            // 8\ngocast.Number[int](8.31)         // 8\ngocast.Number[int](\"8\")          // 8\ngocast.Number[int](true)         // 1\ngocast.Number[int](false)        // 0\n\nvar eight any = 8\ngocast.Number[int](eight)        // 8\ngocast.Cast[int](eight)          // 8\ngocast.Number[int](nil)          // 0\n\n// Number converts only into numeric values (simpler and faster then Cast)\ngocast.Number[float32](\"2.12\")   // 2.12\n\n// Cast converts any type to any other type\ngocast.Cast[float64](\"2.\")       // 2.0\n\nval, err := gocast.TryCast[int](\"123.2\") // 123, \u003cnil\u003e\n\nres := gocast.Map[string, any](struct{ID int64}{ID: 1}) // map[string]any{\"ID\": 1}\n```\n\n```go\nfunc sumAll(vals ...any) int {\n  var result int = 0\n  for _, v := range vals {\n    result += gocast.Number[int](v)\n  }\n  return result\n}\n```\n\n## Struct Field Manipulation\n\nGoCast also allows you to work with struct fields dynamically:\n\n```go\ntype User struct {\n  ID    uint64\n  Email string\n}\n\nvar user User\n\n// Set structure values\nerr := gocast.SetStructFieldValue(\u0026user, \"ID\", uint64(19))\nerr := gocast.SetStructFieldValue(\u0026user, \"Email\", \"iamawesome@mail.com\")\n\nid, err := gocast.StructFieldValue(user, \"ID\")\nemail, err := gocast.StructFieldValue(user, \"Email\")\nfmt.Printf(\"User: %d - %s\", id, email)\n// \u003e User: 19 - iamawesome@mail.com\n```\n\n## Custom Type Support\n\nYou can define and use custom types with GoCast:\n\n```go\n// Define custom type\ntype Money int64\n\nfunc (m *Money) CastSet(ctx context.Context, v any) error {\n  switch val := v.(type) {\n  case Money:\n    *m = val\n  default:\n    *m = Money(gocast.Float64(v) * 1000000)\n  }\n  return nil\n}\n\n// Use custom type in structs\ntype Car struct {\n  ID int64\n  Price Money\n}\n\nvar car Car\n\n// Mapping values into struct\ngocast.TryCopyStruct(\u0026car, map[string]any{\"ID\":1, \"Price\": \"12000.00\"})\n```\n\n## Benchmarks\n\nHere are some benchmark results for GoCast:\n\n```sh\n\u003e go test -benchmem -v -race -bench=.\n\ngoos: darwin\ngoarch: amd64\npkg: github.com/demdxx/gocast/v2\nBenchmarkApproachTest\nBenchmarkApproachTest/bench1\nBenchmarkApproachTest/bench1-24           348097         3064 ns/op        0 B/op          0 allocs/op\nBenchmarkApproachTest/bench2\nBenchmarkApproachTest/bench2-24           394160         3005 ns/op        0 B/op          0 allocs/op\nBenchmarkBool\nBenchmarkBool-24                        20453542           58.87 ns/op             0 B/op          0 allocs/op\nBenchmarkToBoolByReflect\nBenchmarkToBoolByReflect-24             17354990           70.62 ns/op             0 B/op          0 allocs/op\nBenchmarkToFloat\nBenchmarkToFloat-24                     10951923          107.0 ns/op              0 B/op          0 allocs/op\nBenchmarkToInt\nBenchmarkToInt-24                        9870794          121.1 ns/op              0 B/op          0 allocs/op\nBenchmarkToUint\nBenchmarkToUint-24                       9729873          121.3 ns/op              0 B/op          0 allocs/op\nBenchmarkToStringByReflect\nBenchmarkToStringByReflect-24             922710         1601 ns/op        5 B/op          0 allocs/op\nBenchmarkToString\nBenchmarkToString-24                      836929         1622 ns/op        5 B/op          0 allocs/op\nBenchmarkGetSetFieldValue\nBenchmarkGetSetFieldValue/set\nBenchmarkGetSetFieldValue/set-24         1000000         1021 ns/op       64 B/op          4 allocs/op\nBenchmarkGetSetFieldValue/get\nBenchmarkGetSetFieldValue/get-24         1869465          643.8 ns/op             48 B/op          3 allocs/op\nBenchmarkParseTime\nBenchmarkParseTime-24                     374346         3130 ns/op      700 B/op         17 allocs/op\nBenchmarkIsEmpty\nBenchmarkIsEmpty-24                     37383031           31.23 ns/op             0 B/op          0 allocs/op\nPASS\nok      github.com/demdxx/gocast/v2     17.982s\n```\n\n## License\n\nGoCast is released under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdemdxx%2Fgocast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdemdxx%2Fgocast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdemdxx%2Fgocast/lists"}