0

弃用

将函数标记为已弃用的受支持方式如下所示:

type MyStruct struct {
}

// MyFunc returns hello
// Deprecated: Use YourFunc
func (m MyStruct) MyFunc() string {
  return "hello"
}

现代 IDE 将突出显示此函数的任何用法,并且 linter 也可能会发出警告(我没有亲自检查过)

显示 IDE 突出显示

接受接口。返回结构。

一个流行的最佳实践是“接受接口。返回结构”。- 这往往会鼓励软件中的 SOLID 设计。

但是,以下代码(遵循此最佳实践)隐藏了弃用警告:


// MyInterface specifies a single function that we require from a dependency
type MyInterface interface {
    MyFunc() string
}

func main() {

    var v MyInterface
    v = MyStruct{}
    v.MyFunc()

}

显示缺少 IDE 高亮显示

问题

这个问题有解决方案吗?

例如,如果我是库维护者:我如何确保库的用户看到我的弃用警告,他们也遵循最佳实践并定义自己的依赖接口。

4

1 回答 1

3

这似乎是合乎逻辑的,因为接口的方法尚未被弃用。在这种情况下,将这一行添加Deprecated:到接口函数可能会有所帮助(没有测试,因为 VSCode 还没有这样做)。

// MyInterface specifies a single function that we require from a dependency
type MyInterface interface {
    // Deprecated: use YourFunc
    MyFunc() string
}

在这种情况下,因为接口只有 1 个功能,你应该弃用整个东西。我知道 godoc/pkg.go.dev 支持,以Queryer为例。

// MyInterface specifies a single function that we require from a dependency
// Deprecated: use YourInterface
type MyInterface interface {
    MyFunc() string
}
于 2021-11-22T21:18:36.427 回答