gpt4 book ai didi

go - 我的 gofmt 工作不正常还是我不明白什么?

转载 作者:IT王子 更新时间:2023-10-29 01:26:37 24 4
gpt4 key购买 nike

我想我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com