{"id":15655446,"url":"https://github.com/haya14busa/go-typeconv","last_synced_at":"2025-05-05T03:26:35.818Z","repository":{"id":142020596,"uuid":"79860924","full_name":"haya14busa/go-typeconv","owner":"haya14busa","description":"Bring implicit type conversion into Go in a explicit way","archived":false,"fork":false,"pushed_at":"2017-02-09T16:28:07.000Z","size":45,"stargazers_count":24,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-04T12:59:13.647Z","etag":null,"topics":["go","golang","golang-tools"],"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/haya14busa.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":"haya14busa","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-01-23T23:50:01.000Z","updated_at":"2017-09-29T01:39:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"274e4fdb-8e3f-4fd9-b0b6-0e2399417abd","html_url":"https://github.com/haya14busa/go-typeconv","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/haya14busa%2Fgo-typeconv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haya14busa%2Fgo-typeconv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haya14busa%2Fgo-typeconv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haya14busa%2Fgo-typeconv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haya14busa","download_url":"https://codeload.github.com/haya14busa/go-typeconv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221137295,"owners_count":16762712,"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":["go","golang","golang-tools"],"created_at":"2024-10-03T12:59:16.352Z","updated_at":"2024-10-22T22:20:21.888Z","avatar_url":"https://github.com/haya14busa.png","language":"Go","funding_links":["https://github.com/sponsors/haya14busa"],"categories":[],"sub_categories":[],"readme":"## go-typeconv - Bring implicit type conversion into Go in a explicit way\n\n[![Build Status](https://travis-ci.org/haya14busa/go-typeconv.svg?branch=master)](https://travis-ci.org/haya14busa/go-typeconv)\n[![Go Report Card](https://goreportcard.com/badge/github.com/haya14busa/go-typeconv)](https://goreportcard.com/report/github.com/haya14busa/go-typeconv)\n[![Coverage](https://codecov.io/gh/haya14busa/go-typeconv/branch/master/graph/badge.svg)](https://codecov.io/gh/haya14busa/go-typeconv)\n[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nGo doesn't have implicit type conversion.\n\n\u003e Unlike in C, in Go assignment between items of different type requires an explicit conversion.\n\u003e -- Type conversions https://tour.golang.org/basics/13\n\nFAQ: Why does Go not provide implicit numeric conversions?  https://golang.org/doc/faq#conversions\n\nI like this design. Explicit is better than implicit.\nIn Go, almost all things are expressed explicitly.\n\nHowever, sometimes... it's too tedious to fix type coversion errors by hand.\nIf a required type is `int64` and got `int` type, why not converting it automatically?\nI'm tired of wrapping expressions with `int64()` or something here and there.\n\nHere comes gotypeconv! gotypeconv takes source code, detects the type conversion errors and fixes them automatically by rewriting AST.\n\ngotypeconv is like gofmt (it actually formats code as well), but it also fixes type conversions errors.\n\n### Installation\n\n```\ngo get -u github.com/haya14busa/go-typeconv/cmd/gotypeconv\n```\n\n### Usage example\n\n#### ./testdata/tour.input.go\n\n```go\n// https://tour.golang.org/basics/13\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\nfunc main() {\n\tvar x, y int = 3, 4\n\tvar f float64 = math.Sqrt(x*x + y*y)\n\tvar z uint = f\n\tfmt.Println(x, y, z)\n}\n```\n\nAbove code has type conversion errors as follow.\n\n```\n$ go build testdata/tour.input.go\ntestdata/tour.input.go:11: cannot use x * x + y * y (type int) as type float64 in argument to math.Sqrt\ntestdata/tour.input.go:12: cannot use f (type float64) as type uint in assignment\n```\n\ngotypeconv can fix them automatically!\n\n```\n$ gotypeconv ./testdata/tour.input.go\n// https://tour.golang.org/basics/13\npackage main\n\nimport (\n        \"fmt\"\n        \"math\"\n)\n\nfunc main() {\n        var x, y int = 3, 4\n        var f float64 = math.Sqrt(float64(x*x + y*y))\n        var z uint = uint(f)\n        fmt.Println(x, y, z)\n}\n```\n\ngotypeconv also supports displaying diff (`-d` flag) and rewriting files in-place (`-w` flag) same as gofmt.\n\n### More example\n\nGo doesn't have overloading. https://golang.org/doc/faq#overloading\nI like this design too.\n\nHowever, sometimes... it's inconvenient.\nFor example, when you want `max` utility function, you may write something like this `func max(x int64, ys ...int64) int64`.\nIt works, but when you want to calculate max of given `int`s, you cannot ues this function unless wrapping them with `int64()`.\n\nYou also may start to write `func max(x int, ys ...int) int`, and change type to int64 later.\nThen, you need to wrap expressions with `int64()` here and there in this case as well.\n\nHere comes gotypeconv, again!\n\n```go\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tvar (\n\t\tx int     = 1\n\t\ty int64   = 14\n\t\tz float64 = -1.4\n\t)\n\n\tvar ans int = max(x, x+y, z)\n\tfmt.Println(ans)\n}\n\nfunc max(x int64, ys ...int64) int64 {\n\tfor _, y := range ys {\n\t\tif y \u003e x {\n\t\t\tx = y\n\t\t}\n\t}\n\treturn x\n}\n```\n\nAbove code can be fixed gotypeconv. (`$ gotypeconv -d testdata/max.input.go`)\n\n```diff\n@@ -9,7 +9,7 @@\n                z float64 = -1.4\n        )\n\n-       var ans int = max(x, x+y, z)\n+       var ans int = int(max(int64(x), int64(x)+y, int64(z)))\n        fmt.Println(ans)\n }\n```\n\n(I miss generics in this case... but gotypeconv can also solve the problem!)\n\n#### Hou to Use in Vim\n\nUse https://github.com/haya14busa/vim-gofmt with following sample config.\n\n```vim\nlet g:gofmt_formatters = [\n\\   { 'cmd': 'gofmt', 'args': ['-s', '-w'] },\n\\   { 'cmd': 'goimports', 'args': ['-w'] },\n\\   { 'cmd': 'gotypeconv', 'args': ['-w'] },\n\\ ]\n```\n\n## :bird: Author\nhaya14busa (https://github.com/haya14busa)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaya14busa%2Fgo-typeconv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaya14busa%2Fgo-typeconv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaya14busa%2Fgo-typeconv/lists"}