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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With