drq231358 2014-02-11 14:52
浏览 78
已采纳

使用gofmt重构工具重命名全局变量

I'm experimenting the gofmt tool capabilities for refactoring go code based on this blog post, I have this trivial example:

package main

import (
    "fmt"
)

var v = 12

func main() {
    fmt.Println(v)
}

I'm trying to rename the v variable to mapplying this recipe:

gofmt -r 'v -> m' -w main.go

The code after the refactoring looks (broken) like:

package m

import (
    "fmt"
)

var m = m

func m() {
    m
}

What am I missing here?

  • 写回答

1条回答 默认 最新

  • dti70601 2014-02-11 15:08
    关注

    There is a problem with what you're trying, the gofmt manual states:

    The rewrite rule specified with the -r flag must be a string of the form:

    pattern -> replacement

    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.

    (highlighting added)

    If you had var vee = 12 and used -r vee -> foo everything would be fine. With v -> m however, v -> m matches every Go expression, identifies it as v and replaces it by m.

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

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真