gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:08:07 25 4
gpt4 key购买 nike

我正在试验 gofmt 工具的重构功能 基于此的代码blog post ,我有一个简单的例子:

package main

import (
"fmt"
)

var v = 12

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

我正在尝试重命名 v 变量为m应用此配方:

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

重构后的代码看起来(损坏)如下:

package m

import (
"fmt"
)

var m = m

func m() {
m
}

我在这里错过了什么?

最佳答案

您正在尝试的内容有问题,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.

(添加了突出显示)

如果你有 var vee = 12 并使用 -r vee -> foo 一切都会好起来的。但是,使用 v -> mv -> m 匹配每个 Go 表达式,将其标识为 v 并将其替换为 m

关于go - 使用 gofmt 重构工具重命名全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21705172/

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