我认为我的 gofmt
没有按预期工作,对吗?
原始文件:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
然后我做了:
gofmt -r 'h -> H' -w "hello.go"
之后的文件内容:
package H
import "fmt"
func H() {
H
}
最佳答案
大概gofmt
按照其作者的意图工作,这可能与您的预期不同。
documentation说:
Both pattern and replacement must be valid Go expressions. In the pattern, single-character lowercase identifiers serve as wildcards matching arbitrary sub-expressions; those expressions will be substituted for the same identifiers in the replacement.
由于模式中只有一个小写字母,它会匹配所有子表达式。然后将它们替换为H
。让我们进一步看一下你的例子,考虑一下:
package main
import "fmt"
func compare(a, b int) {
if a + b < a * b {
fmt.Printf("hello, world\n")
}
}
在相同的gofmt
命令之后,上面的代码变成:
package H
import "fmt"
func H(H, H H) {
if H+H < H*H {
H
}
}
如果这不是您想要的,那么您应该使用更具体的模式表达式。
关于go - 是我的 gofmt 工作错误还是我不明白什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42049267/