{"id":17296589,"url":"https://github.com/chen3feng/safecast","last_synced_at":"2025-03-26T20:42:43.598Z","repository":{"id":59045720,"uuid":"524544612","full_name":"chen3feng/safecast","owner":"chen3feng","description":"Safe Numeric Type Cast Library for Go","archived":false,"fork":false,"pushed_at":"2022-09-08T17:06:23.000Z","size":72,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T02:30:22.672Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chen3feng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-14T01:16:31.000Z","updated_at":"2024-11-11T08:16:25.000Z","dependencies_parsed_at":"2022-09-11T04:41:47.077Z","dependency_job_id":null,"html_url":"https://github.com/chen3feng/safecast","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/chen3feng%2Fsafecast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen3feng%2Fsafecast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen3feng%2Fsafecast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen3feng%2Fsafecast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chen3feng","download_url":"https://codeload.github.com/chen3feng/safecast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245733792,"owners_count":20663584,"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-10-15T11:13:14.516Z","updated_at":"2025-03-26T20:42:43.568Z","avatar_url":"https://github.com/chen3feng.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# safecast\n\nSafe Numeric Type Cast Library for Go\n\nEnglish | [简体中文](README_zh.md)\n\n[![License Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-red.svg)](COPYING)\n[![Golang](https://img.shields.io/badge/Language-go1.18+-blue.svg)](https://go.dev/)\n![Build Status](https://github.com/chen3feng/safecast/actions/workflows/go.yml/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/chen3feng/safecast/badge.svg?branch=master)](https://coveralls.io/github/chen3feng/safecast?branch=master)\n[![GoReport](https://goreportcard.com/badge/github.com/securego/gosec)](https://goreportcard.com/report/github.com/chen3feng/safecast)\n[![Go Reference](https://pkg.go.dev/badge/github.com/chen3feng/safecast.svg)](https://pkg.go.dev/github.com/chen3feng/safecast)\n\nSafe numeric type cast library. suppoer all integral and floating types, except uintptr.\n\nThis library depends on go generics, which is introduced in 1.18+.\n\nUsage:\n\n```go\nval, ok := To[type](value)\n```\n\n`ok == false` indicates overflow occured. but whatever，`val` is always equals to the result of the normal type cast (`type(value)`) expression。\n\nThere are also non-parametric forms of the target types:\n\n````go\nval, ok := ToInt32(value)\n````\n\nThe usage is the same as the according full generic version, and the performance will be better.\n\n\u003c!-- gomarkdoc:embed:start --\u003e\n\n\u003c!-- Code generated by gomarkdoc. DO NOT EDIT --\u003e\n\n# safecast\n\n```go\nimport \"github.com/chen3feng/safecast\"\n```\n\nPackage safecast provide a safe way to cast a numeric value from type A to type B, with overflow and underflow check.\n\n## Index\n\n- [func To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)](\u003c#func-to\u003e)\n- [func ToFloat32[FromType numericType](value FromType) (float32, bool)](\u003c#func-tofloat32\u003e)\n- [func ToFloat64[FromType numericType](value FromType) (float64, bool)](\u003c#func-tofloat64\u003e)\n- [func ToInt[FromType numericType](value FromType) (int, bool)](\u003c#func-toint\u003e)\n- [func ToInt16[FromType numericType](value FromType) (int16, bool)](\u003c#func-toint16\u003e)\n- [func ToInt32[FromType numericType](value FromType) (int32, bool)](\u003c#func-toint32\u003e)\n- [func ToInt64[FromType numericType](value FromType) (int64, bool)](\u003c#func-toint64\u003e)\n- [func ToInt8[FromType numericType](value FromType) (int8, bool)](\u003c#func-toint8\u003e)\n- [func ToUint[FromType numericType](value FromType) (uint, bool)](\u003c#func-touint\u003e)\n- [func ToUint16[FromType numericType](value FromType) (uint16, bool)](\u003c#func-touint16\u003e)\n- [func ToUint32[FromType numericType](value FromType) (uint32, bool)](\u003c#func-touint32\u003e)\n- [func ToUint64[FromType numericType](value FromType) (uint64, bool)](\u003c#func-touint64\u003e)\n- [func ToUint8[FromType numericType](value FromType) (uint8, bool)](\u003c#func-touint8\u003e)\n\n\n## func [To](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L12\u003e)\n\n```go\nfunc To[ToType numericType, FromType numericType](value FromType) (result ToType, ok bool)\n```\n\nTo converts a numeric value from the FromType to the specified ToType type safely. result will always be same as the usual type cast \\(type\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n\u003cdetails\u003e\u003csummary\u003eExample (Float Overflow)\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/chen3feng/safecast\"\n\t\"math\"\n)\n\nfunc main() {\n\tn, ok := safecast.To[float32](math.MaxFloat32 * 2)\n\tfmt.Print(n, ok)\n}\n```\n\n#### Output\n\n```\n+Inf false\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eExample (Int No Overflow)\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/chen3feng/safecast\"\n)\n\nfunc main() {\n\tb, ok := safecast.To[byte](255)\n\tfmt.Print(b, ok)\n}\n```\n\n#### Output\n\n```\n255 true\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eExample (Int Overflow)\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/chen3feng/safecast\"\n)\n\nfunc main() {\n\tb, ok := safecast.To[byte](256)\n\tfmt.Print(b, ok)\n}\n```\n\n#### Output\n\n```\n0 false\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eExample (Value In Range)\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/chen3feng/safecast\"\n)\n\nfunc main() {\n\tn, ok := safecast.To[uint](1)\n\tfmt.Print(n, ok)\n}\n```\n\n#### Output\n\n```\n1 true\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eExample (Value Out Of Range)\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/chen3feng/safecast\"\n)\n\nfunc main() {\n\tn, ok := safecast.To[uint32](-1)\n\tfmt.Print(n, ok)\n}\n```\n\n#### Output\n\n```\n4294967295 false\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n## func [ToFloat32](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L398\u003e)\n\n```go\nfunc ToFloat32[FromType numericType](value FromType) (float32, bool)\n```\n\nToFloat32 converts value to float32 type safely. result will always be same as the usual type cast\\(float32\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToFloat64](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L432\u003e)\n\n```go\nfunc ToFloat64[FromType numericType](value FromType) (float64, bool)\n```\n\nToFloat64 converts value to float64 type safely. result will always be same as the usual type cast\\(float64\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToInt](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L194\u003e)\n\n```go\nfunc ToInt[FromType numericType](value FromType) (int, bool)\n```\n\nToInt converts value to int type safely. result will always be same as the usual type cast\\(int\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToInt16](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L92\u003e)\n\n```go\nfunc ToInt16[FromType numericType](value FromType) (int16, bool)\n```\n\nToInt16 converts value to int16 type safely. result will always be same as the usual type cast\\(int16\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToInt32](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L126\u003e)\n\n```go\nfunc ToInt32[FromType numericType](value FromType) (int32, bool)\n```\n\nToInt32 converts value to int32 type safely. result will always be same as the usual type cast\\(int32\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToInt64](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L160\u003e)\n\n```go\nfunc ToInt64[FromType numericType](value FromType) (int64, bool)\n```\n\nToInt64 converts value to int64 type safely. result will always be same as the usual type cast\\(int64\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToInt8](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L58\u003e)\n\n```go\nfunc ToInt8[FromType numericType](value FromType) (int8, bool)\n```\n\nToInt8 converts value to int8 type safely. result will always be same as the usual type cast\\(int8\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToUint](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L364\u003e)\n\n```go\nfunc ToUint[FromType numericType](value FromType) (uint, bool)\n```\n\nToUint converts value to uint type safely. result will always be same as the usual type cast\\(uint\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToUint16](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L262\u003e)\n\n```go\nfunc ToUint16[FromType numericType](value FromType) (uint16, bool)\n```\n\nToUint16 converts value to uint16 type safely. result will always be same as the usual type cast\\(uint16\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToUint32](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L296\u003e)\n\n```go\nfunc ToUint32[FromType numericType](value FromType) (uint32, bool)\n```\n\nToUint32 converts value to uint32 type safely. result will always be same as the usual type cast\\(uint32\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToUint64](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L330\u003e)\n\n```go\nfunc ToUint64[FromType numericType](value FromType) (uint64, bool)\n```\n\nToUint64 converts value to uint64 type safely. result will always be same as the usual type cast\\(uint64\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n## func [ToUint8](\u003chttps://github.com/chen3feng/safecast/blob/master/generics.go#L228\u003e)\n\n```go\nfunc ToUint8[FromType numericType](value FromType) (uint8, bool)\n```\n\nToUint8 converts value to uint8 type safely. result will always be same as the usual type cast\\(uint8\\(value\\)\\), but ok is false when overflow or underflow occured.\n\n\n\nGenerated by [gomarkdoc](\u003chttps://github.com/princjef/gomarkdoc\u003e)\n\n\n\u003c!-- gomarkdoc:embed:end --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen3feng%2Fsafecast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchen3feng%2Fsafecast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen3feng%2Fsafecast/lists"}