Code Monkey home page Code Monkey logo

go-typeconv's Introduction

go-typeconv - Bring implicit type conversion into Go in a explicit way

Build Status Go Report Card Coverage LICENSE

Go doesn't have implicit type conversion.

Unlike in C, in Go assignment between items of different type requires an explicit conversion. -- Type conversions https://tour.golang.org/basics/13

FAQ: Why does Go not provide implicit numeric conversions? https://golang.org/doc/faq#conversions

I like this design. Explicit is better than implicit. In Go, almost all things are expressed explicitly.

However, sometimes... it's too tedious to fix type coversion errors by hand. If a required type is int64 and got int type, why not converting it automatically? I'm tired of wrapping expressions with int64() or something here and there.

Here comes gotypeconv! gotypeconv takes source code, detects the type conversion errors and fixes them automatically by rewriting AST.

gotypeconv is like gofmt (it actually formats code as well), but it also fixes type conversions errors.

Installation

go get -u github.com/haya14busa/go-typeconv/cmd/gotypeconv

Usage example

./testdata/tour.input.go

// https://tour.golang.org/basics/13
package main

import (
	"fmt"
	"math"
)

func main() {
	var x, y int = 3, 4
	var f float64 = math.Sqrt(x*x + y*y)
	var z uint = f
	fmt.Println(x, y, z)
}

Above code has type conversion errors as follow.

$ go build testdata/tour.input.go
testdata/tour.input.go:11: cannot use x * x + y * y (type int) as type float64 in argument to math.Sqrt
testdata/tour.input.go:12: cannot use f (type float64) as type uint in assignment

gotypeconv can fix them automatically!

$ gotypeconv ./testdata/tour.input.go
// https://tour.golang.org/basics/13
package main

import (
        "fmt"
        "math"
)

func main() {
        var x, y int = 3, 4
        var f float64 = math.Sqrt(float64(x*x + y*y))
        var z uint = uint(f)
        fmt.Println(x, y, z)
}

gotypeconv also supports displaying diff (-d flag) and rewriting files in-place (-w flag) same as gofmt.

More example

Go doesn't have overloading. https://golang.org/doc/faq#overloading I like this design too.

However, sometimes... it's inconvenient. For example, when you want max utility function, you may write something like this func max(x int64, ys ...int64) int64. It works, but when you want to calculate max of given ints, you cannot ues this function unless wrapping them with int64().

You also may start to write func max(x int, ys ...int) int, and change type to int64 later. Then, you need to wrap expressions with int64() here and there in this case as well.

Here comes gotypeconv, again!

package main

import "fmt"

func main() {
	var (
		x int     = 1
		y int64   = 14
		z float64 = -1.4
	)

	var ans int = max(x, x+y, z)
	fmt.Println(ans)
}

func max(x int64, ys ...int64) int64 {
	for _, y := range ys {
		if y > x {
			x = y
		}
	}
	return x
}

Above code can be fixed gotypeconv. ($ gotypeconv -d testdata/max.input.go)

@@ -9,7 +9,7 @@
                z float64 = -1.4
        )

-       var ans int = max(x, x+y, z)
+       var ans int = int(max(int64(x), int64(x)+y, int64(z)))
        fmt.Println(ans)
 }

(I miss generics in this case... but gotypeconv can also solve the problem!)

Hou to Use in Vim

Use https://github.com/haya14busa/vim-gofmt with following sample config.

let g:gofmt_formatters = [
\   { 'cmd': 'gofmt', 'args': ['-s', '-w'] },
\   { 'cmd': 'goimports', 'args': ['-w'] },
\   { 'cmd': 'gotypeconv', 'args': ['-w'] },
\ ]

๐Ÿฆ Author

haya14busa (https://github.com/haya14busa)

go-typeconv's People

Contributors

haya14busa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-typeconv's Issues

Can't go gettable

Hi, thanks for great work :D

But I haven't tried use the go-typeconv yet, sorry...
I'll use it later.

BTW, I was trying install directly that using go get -u github.com/haya14busa/go-typeconv/cmd/gotypeconv, but can't installed.
It seems to caused by go-typeconv using git submodule for managing the vendor packages.
I'm not sure why ran the git submodule update --init --recursive, but failed.

Do you have any plan that uses typical vendoring package manager, ...such as dep? lol

The detailed error is below.

$ go get -u -v -x github.com/haya14busa/go-typeconv/cmd/gotypeconv

cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv
git config remote.origin.url
github.com/haya14busa/go-typeconv (download)
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv
git pull --ff-only
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv
git submodule update --init --recursive
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv
git show-ref
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv
git submodule update --init --recursive
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv
git config remote.origin.url
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv/vendor/golang.org/x/tools
git config remote.origin.url
github.com/haya14busa/go-typeconv/vendor/golang.org/x/tools (download)
package github.com/haya14busa/go-typeconv/vendor/golang.org/x/tools/go/ast/astutil: /Users/zchee/go/src/github.com/haya14busa/go-typeconv/vendor/golang.org/x/tools/.git exists but is not a directory
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv/vendor/golang.org/x/tools
git config remote.origin.url
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv/vendor/golang.org/x/tools
git config remote.origin.url
cd /Users/zchee/go/src/github.com/haya14busa/go-typeconv/vendor/golang.org/x/sync
git config remote.origin.url
github.com/haya14busa/go-typeconv/vendor/golang.org/x/sync (download)
package github.com/haya14busa/go-typeconv/vendor/golang.org/x/sync/errgroup: /Users/zchee/go/src/github.com/haya14busa/go-typeconv/vendor/golang.org/x/sync/.git exists but is not a directory

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.