我的 gofmt 工作不正常还是我不明白什么?
Does my gofmt work wrongly or I don't understand something?
我想我的 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.
由于模式中只有一个小写字母,它匹配所有 sub-expressions。然后将它们替换为 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
}
}
如果这不是您想要的,那么您应该使用更具体的模式表达式。
我想我的 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.
由于模式中只有一个小写字母,它匹配所有 sub-expressions。然后将它们替换为 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
}
}
如果这不是您想要的,那么您应该使用更具体的模式表达式。