duanpo7354 2017-02-05 06:40
浏览 32
已采纳

我的gofmt工作不正确,还是我听不懂?

I suppose that my gofmt works not how it's supposed to, am I right ?

Original file:

package main


import "fmt"


func main() {
    fmt.Printf("hello, world
")
}

Then I did:

gofmt -r 'h -> H' -w "hello.go"

Content of the file after:

package H


import "fmt"


func H() {
 H
}
  • 写回答

1条回答 默认 最新

  • doufeinai6081 2017-02-05 06:58
    关注

    Presumably gofmt works as its authors intended, which might be different from what you expected. The documentation says:

    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.

    As you have only a single lowercase letter in the pattern, it matches all sub-expressions. And then replaces them with H. Let's take your example further, consider this:

    package main
    
    import "fmt"
    
    func compare(a, b int) {
        if a + b < a * b {
            fmt.Printf("hello, world
    ")
        }
    }
    

    After the same gofmt command the above code becomes:

    package H
    
    import "fmt"
    
    func H(H, H H) {
        if H+H < H*H {
            H
        }
    }
    

    If this is not what you want, then you should use a more specific pattern expression.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库