{"id":13413490,"url":"https://github.com/cstockton/go-conv","last_synced_at":"2025-12-15T18:21:51.014Z","repository":{"id":52216056,"uuid":"70568579","full_name":"cstockton/go-conv","owner":"cstockton","description":"Fast conversions across various Go types with a simple API.","archived":false,"fork":false,"pushed_at":"2021-08-23T21:52:24.000Z","size":138,"stargazers_count":385,"open_issues_count":1,"forks_count":20,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-10-25T05:23:43.104Z","etag":null,"topics":["conv","conversion","conversion-functions","strconv","string-conversion","time-conversion"],"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/cstockton.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":"2016-10-11T07:41:41.000Z","updated_at":"2024-08-01T11:05:50.000Z","dependencies_parsed_at":"2022-09-02T04:11:14.548Z","dependency_job_id":null,"html_url":"https://github.com/cstockton/go-conv","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstockton%2Fgo-conv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstockton%2Fgo-conv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstockton%2Fgo-conv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cstockton%2Fgo-conv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cstockton","download_url":"https://codeload.github.com/cstockton/go-conv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635451,"owners_count":20322941,"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":["conv","conversion","conversion-functions","strconv","string-conversion","time-conversion"],"created_at":"2024-07-30T20:01:41.551Z","updated_at":"2025-12-15T18:21:50.955Z","avatar_url":"https://github.com/cstockton.png","language":"Go","funding_links":[],"categories":["Microsoft Office","Miscellaneous","其他杂项","Uncategorized","杂项","其他","\u003cspan id=\"其他-miscellaneous\"\u003e其他 Miscellaneous\u003c/span\u003e"],"sub_categories":["Uncategorized","Strings","暂未分类","Advanced Console UIs","未分类的","暂未分类这些库被放在这里是因为其他类别似乎都不适合。","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# Go Package: conv\n\n  [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/cstockton/go-conv)\n  [![Go Report Card](https://goreportcard.com/badge/github.com/cstockton/go-conv?style=flat-square)](https://goreportcard.com/report/github.com/cstockton/go-conv)\n  [![Coverage Status](https://img.shields.io/codecov/c/github/cstockton/go-conv/master.svg?style=flat-square)](https://codecov.io/github/cstockton/go-conv?branch=master)\n  [![Build Status](http://img.shields.io/travis/cstockton/go-conv.svg?style=flat-square)](https://travis-ci.org/cstockton/go-conv)\n  [![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/cstockton/go-conv/master/LICENSE)\n\n  \u003e Get:\n  \u003e ```bash\n  \u003e go get -u github.com/cstockton/go-conv\n  \u003e ```\n  \u003e\n  \u003e Example:\n  \u003e ```Go\n  \u003e // Basic types\n  \u003e if got, err := conv.Bool(`TRUE`); err == nil {\n  \u003e \tfmt.Printf(\"conv.Bool(`TRUE`)\\n  -\u003e %v\\n\", got)\n  \u003e }\n  \u003e if got, err := conv.Duration(`1m2s`); err == nil {\n  \u003e \tfmt.Printf(\"conv.Duration(`1m2s`)\\n  -\u003e %v\\n\", got)\n  \u003e }\n  \u003e var date time.Time\n  \u003e err := conv.Infer(\u0026date, `Sat Mar 7 11:06:39 PST 2015`)\n  \u003e fmt.Printf(\"conv.Infer(\u0026date, `Sat Mar 7 11:06:39 PST 2015`)\\n  -\u003e %v\\n\", got)\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e conv.Bool(`TRUE`)\n  \u003e   -\u003e true\n  \u003e conv.Duration(`1m2s`)\n  \u003e   -\u003e 1m2s\n  \u003e conv.Infer(\u0026date, `Sat Mar 7 11:06:39 PST 2015`)\n  \u003e   -\u003e 2015-03-07 11:06:39 +0000 PST\n  \u003e ```\n\n\n## Intro\n\n**Notice:** If you begin getting compilation errors use the v1 import path `gopkg.in/cstockton/go-conv.v1` for an immediate fix and to future-proof.\n\nPackage conv provides fast and intuitive conversions across Go types. This library uses reflection to be robust but will bypass it for common conversions, for example string conversion to any type will never use reflection. All functions are safe for concurrent use by multiple Goroutines.\n\n### Overview\n\n  All conversion functions accept any type of value for conversion, if unable\n  to find a reasonable conversion path they will return the target types zero\n  value and an error.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // The zero value and a non-nil error is returned on failure.\n  \u003e fmt.Println(conv.Int(\"Foo\"))\n  \u003e \n  \u003e // Conversions are allowed as long as the underlying type is convertable, for\n  \u003e // example:\n  \u003e type MyString string\n  \u003e fmt.Println(conv.Int(MyString(\"42\"))) // 42, nil\n  \u003e \n  \u003e // Pointers will be dereferenced when appropriate.\n  \u003e str := \"42\"\n  \u003e fmt.Println(conv.Int(\u0026str)) // 42, nil\n  \u003e \n  \u003e // You may infer values from the base type of a pointer, giving you one\n  \u003e // function signature for all conversions. This may be convenient when the\n  \u003e // types are not known until runtime and reflection must be used.\n  \u003e var val int\n  \u003e err := conv.Infer(\u0026val, `42`)\n  \u003e fmt.Println(val, err) // 42, nil\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e 0 cannot convert \"Foo\" (type string) to int\n  \u003e 42 \u003cnil\u003e\n  \u003e 42 \u003cnil\u003e\n  \u003e 42 \u003cnil\u003e\n  \u003e ```\n\n\n### Bool\n\n  Bool conversion supports all the paths provided by the standard libraries\n  strconv.ParseBool when converting from a string, all other conversions are\n  simply true when not the types zero value. As a special case zero length map\n  and slice types are also false, even if initialized.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // Bool conversion from other bool values will be returned without\n  \u003e // modification.\n  \u003e fmt.Println(conv.Bool(true))\n  \u003e fmt.Println(conv.Bool(false))\n  \u003e \n  \u003e // Bool conversion from strings consider the following values true:\n  \u003e //   \"t\", \"T\", \"true\", \"True\", \"TRUE\",\n  \u003e // \t \"y\", \"Y\", \"yes\", \"Yes\", \"YES\", \"1\"\n  \u003e //\n  \u003e // It considers the following values false:\n  \u003e //   \"f\", \"F\", \"false\", \"False\", \"FALSE\",\n  \u003e //   \"n\", \"N\", \"no\", \"No\", \"NO\", \"0\"\n  \u003e fmt.Println(conv.Bool(\"T\"))\n  \u003e fmt.Println(conv.Bool(\"False\"))\n  \u003e \n  \u003e // Bool conversion from other supported types will return true unless it is\n  \u003e // the zero value for the given type.\n  \u003e fmt.Println(conv.Bool(int64(123)))\n  \u003e fmt.Println(conv.Bool(int64(0)))\n  \u003e fmt.Println(conv.Bool(time.Duration(123)))\n  \u003e fmt.Println(conv.Bool(time.Duration(0)))\n  \u003e fmt.Println(conv.Bool(time.Now()))\n  \u003e fmt.Println(conv.Bool(time.Time{}))\n  \u003e \n  \u003e // All other types will return false.\n  \u003e fmt.Println(conv.Bool(struct{ string }{\"\"}))\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e true \u003cnil\u003e\n  \u003e false \u003cnil\u003e\n  \u003e true \u003cnil\u003e\n  \u003e false \u003cnil\u003e\n  \u003e true \u003cnil\u003e\n  \u003e false \u003cnil\u003e\n  \u003e true \u003cnil\u003e\n  \u003e false \u003cnil\u003e\n  \u003e true \u003cnil\u003e\n  \u003e false \u003cnil\u003e\n  \u003e false cannot convert struct { string }{string:\"\"} (type struct { string }) to bool\n  \u003e ```\n\n\n### Duration\n\n  Duration conversion supports all the paths provided by the standard libraries\n  time.ParseDuration when converting from strings, with a couple enhancements\n  outlined below.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // Duration conversion from strings will first attempt to parse as a Go\n  \u003e // duration value using ParseDuration, then fall back to numeric conventions.\n  \u003e fmt.Println(conv.Duration(\"1h1m100ms\"))     // 1h1m0.1s\n  \u003e fmt.Println(conv.Duration(\"3660100000000\")) // 1h1m0.1s\n  \u003e \n  \u003e // Numeric conversions directly convert to time.Duration nanoseconds.\n  \u003e fmt.Println(conv.Duration(3660100000000)) // 1h1m0.1s\n  \u003e \n  \u003e // Floats deviate from the numeric conversion rules, instead\n  \u003e // separating the integer and fractional portions into seconds.\n  \u003e fmt.Println(conv.Duration(\"3660.10\"))        // 1h1m0.1s\n  \u003e fmt.Println(conv.Duration(float64(3660.10))) // 1h1m0.1s\n  \u003e \n  \u003e // Complex numbers are Float conversions using the real number.\n  \u003e fmt.Println(conv.Duration(complex(3660.10, 0))) // 1h1m0.1s\n  \u003e \n  \u003e // Duration conversion from time.Duration and any numerical type will be\n  \u003e // converted using a standard Go conversion. This includes strings\n  \u003e fmt.Println(conv.Duration(time.Nanosecond)) // 1s\n  \u003e fmt.Println(conv.Duration(byte(1)))         // 1ns\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e 1h1m0.1s \u003cnil\u003e\n  \u003e 1h1m0.1s \u003cnil\u003e\n  \u003e 1h1m0.1s \u003cnil\u003e\n  \u003e 1h1m0.1s \u003cnil\u003e\n  \u003e 1h1m0.1s \u003cnil\u003e\n  \u003e 1h1m0.1s \u003cnil\u003e\n  \u003e 1ns \u003cnil\u003e\n  \u003e 1ns \u003cnil\u003e\n  \u003e ```\n\n\n### Float64\n\n  Float64 conversion from other float values of an identical type will be\n  returned without modification. Float64 from other types follow the general\n  numeric rules.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e fmt.Println(conv.Float64(float64(123.456))) // 123.456\n  \u003e fmt.Println(conv.Float64(\"-123.456\"))       // -123.456\n  \u003e fmt.Println(conv.Float64(\"1.7976931348623157e+308\"))\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e 123.456 \u003cnil\u003e\n  \u003e -123.456 \u003cnil\u003e\n  \u003e 1.7976931348623157e+308 \u003cnil\u003e\n  \u003e ```\n\n\n### Infer\n\n  Infer will perform conversion by inferring the conversion operation from\n  a pointer to a supported T of the `into` param. Since the value is assigned\n  directly only a error value is returned, meaning no type assertions needed.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // Infer requires a pointer to all types.\n  \u003e var into int\n  \u003e if err := conv.Infer(into, `42`); err != nil {\n  \u003e \tfmt.Println(err)\n  \u003e }\n  \u003e if err := conv.Infer(\u0026into, `42`); err == nil {\n  \u003e \tfmt.Println(into)\n  \u003e }\n  \u003e \n  \u003e // Same as above but using new()\n  \u003e truth := new(bool)\n  \u003e if err := conv.Infer(truth, `TRUE`); err != nil {\n  \u003e \tfmt.Println(\"Failed!\")\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e cannot infer conversion for unchangeable 0 (type int)\n  \u003e 42\n  \u003e ```\n\n\n### Int\n\n  Int conversions follow the the general numeric rules.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e fmt.Println(conv.Uint(\"123.456\"))               // 123\n  \u003e fmt.Println(conv.Uint(\"-123.456\"))              // 0\n  \u003e fmt.Println(conv.Uint8(uint64(math.MaxUint64))) // 255\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e 123 \u003cnil\u003e\n  \u003e 0 \u003cnil\u003e\n  \u003e 255 \u003cnil\u003e\n  \u003e ```\n\n\n### String\n\n  String conversion from any values outside the cases below will simply be the\n  result of calling fmt.Sprintf(\"%v\", value), meaning it can not fail. An error\n  is still provided and you should check it to be future proof.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // String conversion from other string values will be returned without\n  \u003e // modification.\n  \u003e fmt.Println(conv.String(\"Foo\"))\n  \u003e \n  \u003e // As a special case []byte will also be returned after a Go string conversion\n  \u003e // is applied.\n  \u003e fmt.Println(conv.String([]byte(\"Foo\")))\n  \u003e \n  \u003e // String conversion from types that do not have a valid conversion path will\n  \u003e // still have sane string conversion for troubleshooting.\n  \u003e fmt.Println(conv.String(struct{ msg string }{\"Foo\"}))\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e Foo \u003cnil\u003e\n  \u003e Foo \u003cnil\u003e\n  \u003e {Foo} \u003cnil\u003e\n  \u003e ```\n\n\n### Time\n\n  Time conversion from other time values will be returned without modification.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // Time conversion from other time.Time values will be returned without\n  \u003e // modification.\n  \u003e fmt.Println(`Times:`)\n  \u003e fmt.Println(conv.Time(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)))\n  \u003e \n  \u003e // Time conversion from strings will be passed through time.Parse using a\n  \u003e // variety of formats. Strings that could not be parsed along with all other\n  \u003e // values will return an empty time.Time{} struct.\n  \u003e fmt.Println(`Strings:`)\n  \u003e formats := []string{\n  \u003e \t`Mon, 02 Jan 2006 15:04:05`,\n  \u003e \t`Mon, 02 Jan 2006 15:04:05 UTC`,\n  \u003e \t`Mon, 2 Jan 2006 15:04:05`,\n  \u003e \t`Mon, 2 Jan 2006 15:04:05 UTC`,\n  \u003e \t`02 Jan 2006 15:04 UTC`,\n  \u003e \t`2 Jan 2006 15:04:05`,\n  \u003e \t`2 Jan 2006 15:04:05 UTC`,\n  \u003e }\n  \u003e for _, format := range formats {\n  \u003e \tt, err := conv.Time(format)\n  \u003e \tif err != nil {\n  \u003e \t\tfmt.Println(`Conversion error: `, err)\n  \u003e \t}\n  \u003e \tfmt.Printf(\"%v \u003c-- (%v)\\n\", t, format)\n  \u003e }\n  \u003e \n  \u003e // Time conversion from types that do not have a valid conversion path will\n  \u003e // return the zero value and an error.\n  \u003e fmt.Println(`Errors:`)\n  \u003e fmt.Println(conv.Time(1))    // cannot convert 1 (type int) to time.Time\n  \u003e fmt.Println(conv.Time(true)) // cannot convert true (type bool) to time.Time\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e Times:\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003cnil\u003e\n  \u003e Strings:\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003c-- (Mon, 02 Jan 2006 15:04:05)\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003c-- (Mon, 02 Jan 2006 15:04:05 UTC)\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003c-- (Mon, 2 Jan 2006 15:04:05)\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003c-- (Mon, 2 Jan 2006 15:04:05 UTC)\n  \u003e 2006-01-02 15:04:00 +0000 UTC \u003c-- (02 Jan 2006 15:04 UTC)\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003c-- (2 Jan 2006 15:04:05)\n  \u003e 2006-01-02 15:04:05 +0000 UTC \u003c-- (2 Jan 2006 15:04:05 UTC)\n  \u003e Errors:\n  \u003e 0001-01-01 00:00:00 +0000 UTC cannot convert 1 (type int) to time.Time\n  \u003e 0001-01-01 00:00:00 +0000 UTC cannot convert true (type bool) to time.Time\n  \u003e ```\n\n\n### Uint\n\n  Uint conversions follow the the general numeric rules.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e fmt.Println(conv.Uint(\"123.456\"))               // 123\n  \u003e fmt.Println(conv.Uint(\"-123.456\"))              // 0\n  \u003e fmt.Println(conv.Uint8(uint64(math.MaxUint64))) // 255\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e 123 \u003cnil\u003e\n  \u003e 0 \u003cnil\u003e\n  \u003e 255 \u003cnil\u003e\n  \u003e ```\n\n\n### Numerics\n\n  Numeric conversion from other numeric values of an identical type will be\n  returned without modification. Numeric conversions deviate slightly from Go\n  when dealing with under/over flow. When performing a conversion operation\n  that would overflow, we instead assign the maximum value for the target type.\n  Similarly, conversions that would underflow are assigned the minimun value\n  for that type, meaning unsigned integers are given zero values instead of\n  spilling into large positive integers.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // For more natural Float -\u003e Integer when the underlying value is a string.\n  \u003e // Conversion functions will always try to parse the value as the target type\n  \u003e // first. If parsing fails float parsing with truncation will be attempted.\n  \u003e fmt.Println(conv.Int(\"-123.456\")) // -123\n  \u003e \n  \u003e // This does not apply for unsigned integers if the value is negative. Instead\n  \u003e // performing a more intuitive (to the human) truncation to zero.\n  \u003e fmt.Println(conv.Uint(\"-123.456\")) // 0\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e -123 \u003cnil\u003e\n  \u003e 0 \u003cnil\u003e\n  \u003e ```\n\n\n### Panics\n\n  In short, panics should not occur within this library under any circumstance.\n  This obviously excludes any oddities that may surface when the runtime is not\n  in a healthy state, i.e. uderlying system instability, memory exhaustion. If\n  you are able to create a reproducible panic please file a bug report.\n\n  \u003e Example:\n  \u003e ```Go\n  \u003e // The zero value for the target type is always returned.\n  \u003e fmt.Println(conv.Bool(nil))\n  \u003e fmt.Println(conv.Bool([][]int{}))\n  \u003e fmt.Println(conv.Bool((chan string)(nil)))\n  \u003e fmt.Println(conv.Bool((*interface{})(nil)))\n  \u003e fmt.Println(conv.Bool((*interface{})(nil)))\n  \u003e fmt.Println(conv.Bool((**interface{})(nil)))\n  \u003e ```\n  \u003e\n  \u003e Output:\n  \u003e ```Go\n  \u003e false cannot convert \u003cnil\u003e (type \u003cnil\u003e) to bool\n  \u003e false \u003cnil\u003e\n  \u003e false \u003cnil\u003e\n  \u003e false cannot convert (*interface {})(nil) (type *interface {}) to bool\n  \u003e false cannot convert (*interface {})(nil) (type *interface {}) to bool\n  \u003e false cannot convert (**interface {})(nil) (type **interface {}) to bool\n  \u003e ```\n\n\n## Contributing\n\nFeel free to create issues for bugs, please ensure code coverage remains 100%\nwith any pull requests.\n\n\n## Bugs and Patches\n\n  Feel free to report bugs and submit pull requests.\n\n  * bugs:\n    \u003chttps://github.com/cstockton/go-conv/issues\u003e\n  * patches:\n    \u003chttps://github.com/cstockton/go-conv/pulls\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcstockton%2Fgo-conv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcstockton%2Fgo-conv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcstockton%2Fgo-conv/lists"}