一个gofmt重写规则可以删除多余的参数类型? [英] can a gofmt rewrite rule remove redundant argument types?

查看:234
本文介绍了一个gofmt重写规则可以删除多余的参数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有这样的代码: func MyFunc(a int,b int)



gofmt可以重写规则将其更改为: func MyFunc(a,b int)



我试过: gofmt -rf(xt,yt) - > f(x,yt)myfile.go



但是我得到:解析模式f(xt,yt)为1:5:expected')',找到'IDENT't



我也试过: gofmt -rf(x int,y int) - > f(x,y int)myfile.go



但它为int提供了一个类似的错误而不是t

我读过 gofmt文档



我故意使用单个字符标识符来匹配表达式。



我怀疑问题可能在于尝试匹配类型,因为它可能不被视为表达式



是否可以使用gofmt做到这一点?

/ p>

解决方案

不,它不可能 - 因为要把patter当成Expression,看看 http://golang.org/src/cmd/gofmt/rewrite.go parseExpr()函数。 p>

转到规范( http://golang.org/ref / spec#表达式
清楚地说明了什么一个表达式通过将运算符和函数应用于操作数来指定计算值。
所以去fmt尝试解析你的模式f(xt,yt)作为函数调用,所以不是t而是逗号或括号。

你不能写模式,它会很多func MyFunc(一个int,b int) - 因为它的函数定义,而不是一个有效的去表达式


If you have code like: func MyFunc(a int, b int)

Can a gofmt rewrite rule change it to: func MyFunc(a, b int)

I tried: gofmt -r "f(x t, y t) -> f(x, y t)" myfile.go

But I get: parsing pattern f(x t, y t) at 1:5: expected ')', found 'IDENT' t

I also tried: gofmt -r "f(x int, y int) -> f(x, y int)" myfile.go

But it gives a similar error for int instead of t

I have read the gofmt documentation. A web search didn't turn up anything helpful.

I am deliberately using single character identifiers to match expressions.

I suspect the problem may be in trying to match the type since it may not be regarded as an "expression"

Is it possible to do this with gofmt?

解决方案

No, its not possible - because go fmt treat patter as "Expression", look at the http://golang.org/src/cmd/gofmt/rewrite.go parseExpr() function.

Go specification(http://golang.org/ref/spec#Expressions) clearly says what "An expression specifies the computation of a value by applying operators and functions to operands." so go fmt try to parse your pattern "f(x t, y t)" as function call, so instead of "t" it expects comma or parentheses.

you can not write pattern which will much "func MyFunc(a int, b int)" - because its function definition, not a valid go expression

这篇关于一个gofmt重写规则可以删除多余的参数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆