All Projects → haya14busa → go-typeconv

haya14busa / go-typeconv

Licence: MIT license
Bring implicit type conversion into Go in a explicit way

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-typeconv

Gogi
Go client for gitignore.io
Stars: ✭ 51 (+112.5%)
Mutual labels:  golang-tools
Goreportcard
A report card for your Go application
Stars: ✭ 1,608 (+6600%)
Mutual labels:  golang-tools
gogh
GO GitHub project manager
Stars: ✭ 29 (+20.83%)
Mutual labels:  golang-tools
Marlow
golang generator for type-safe sql api constructs
Stars: ✭ 83 (+245.83%)
Mutual labels:  golang-tools
Scopelint
scopelint checks for unpinned variables in go programs
Stars: ✭ 110 (+358.33%)
Mutual labels:  golang-tools
Tinderonline
Find out which of your friends are online on Tinder
Stars: ✭ 155 (+545.83%)
Mutual labels:  golang-tools
Golang Plugin
Automatically installs the Go tools on Jenkins build agents
Stars: ✭ 49 (+104.17%)
Mutual labels:  golang-tools
universalmutator
Regexp based tool for mutating generic source code across numerous languages
Stars: ✭ 105 (+337.5%)
Mutual labels:  golang-tools
Gocmt
Add missing comment on exported function, method, type, constant, variable in go file
Stars: ✭ 111 (+362.5%)
Mutual labels:  golang-tools
Sonar Golang
Sonarqube plugin for the golang language.
Stars: ✭ 229 (+854.17%)
Mutual labels:  golang-tools
Goreuse
Generic Code for Go
Stars: ✭ 93 (+287.5%)
Mutual labels:  golang-tools
Vermin
The smart virtual machines manager. A modern CLI for Vagrant Boxes.
Stars: ✭ 110 (+358.33%)
Mutual labels:  golang-tools
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (+587.5%)
Mutual labels:  golang-tools
Go Search Extension
The ultimate search extension for Golang
Stars: ✭ 69 (+187.5%)
Mutual labels:  golang-tools
typogenerator
Golang string typosquatting generator
Stars: ✭ 67 (+179.17%)
Mutual labels:  golang-tools
Ebreader
一个让你可以在浏览器中阅读Epub电子书的CLI程序,使用Golang编写
Stars: ✭ 50 (+108.33%)
Mutual labels:  golang-tools
Gomod
Go modules analysis tool
Stars: ✭ 139 (+479.17%)
Mutual labels:  golang-tools
nestif
Detect deeply nested if statements in Go source code
Stars: ✭ 30 (+25%)
Mutual labels:  golang-tools
go-notebook
Go-Notebook is inspired by Jupyter Project (link) in order to document Golang code.
Stars: ✭ 33 (+37.5%)
Mutual labels:  golang-tools
Dupl
a tool for code clone detection
Stars: ✭ 228 (+850%)
Mutual labels:  golang-tools

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)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].