如何在Intellij中安全删除所有文件中结构字段的用法?

huangapple go评论77阅读模式
英文:

How to safe delete the usages of struct field on all files from Intellij?

问题

尽管我们有重构工具可以重命名结构字段,但我们没有用于删除结构字段及其使用的重构工具。

如何安全地从任何支持golang的IDE中删除结构字段及其使用(包括读写访问)?

据我所见,目前没有任何IDE(如vim-go、intellij)支持此功能。

我曾考虑过删除结构字段并运行go vet命令,在每个文件上返回所有错误(包括行号),然后编写一个脚本来删除这些行,但不幸的是,vet在遇到第一个错误后就停止报告错误了。

英文:

Though we have refactoring tool to rename the struct field we do not have refactoring tool for deleting the struct field & its usages.

How to safely delete the struct field and its usages[Write & read access] across the files from any IDE that supports golang?

As far as I have seen none of the IDE(vim-go, intellij) supports this.

I had thought of deleting the struct field & run

go vet

which will return all error(along with line number) on every file & write a script to delete those lines but unfortunately vet stops reporting errors after very first error encountered in a file.

答案1

得分: 4

这不是可以完全自动化的事情。假设你从类型Point struct {X, Y int}中删除字段X,当它被如下方式使用时,IDE应该怎么做呢:

p := Point{X: 1, Y: 2}
r := p.X / p.Y

或者像这样:

func f(x, y int) {}

f(p.X, p.Y)

很明显,所需的操作不是IDE可以“猜测”的。

要删除一个结构体字段,你需要从类型定义中删除它,并手动修复编译器错误。

英文:

This is not something that could be fully automated. Let's say you remove the field X from the type Point struct {X, Y int}. What should the IDE do when it's used like this:

p := Point{X: 1, Y: 2}
r := p.X / p.Y

Or like this:

func f(x, y int) {}

f(p.X, p.Y)

What's needed is obviously not something the IDE could "guess".

To delete a struct field, delete it from the type definition and proceed to fix the compiler errors manually.

huangapple
  • 本文由 发表于 2021年6月23日 15:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/68095370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定