“接受接口”是否会破坏废弃工具?

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

Does "Accept Interfaces" break deprecation tooling?

问题

弃用

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

type MyStruct struct {
}

// MyFunc 返回 hello
// Deprecated: 使用 YourFunc
func (m MyStruct) MyFunc() string {
  return "hello"
}

现代的集成开发环境(IDE)会突出显示该函数的任何使用情况,代码检查工具也可能会发出警告(我个人没有检查过)。

“接受接口”是否会破坏废弃工具?

接受接口。返回结构体。

一种流行的最佳实践是“接受接口。返回结构体。”,这倾向于在软件中鼓励SOLID设计。

然而,下面的代码遵循了这个最佳实践,但却隐藏了弃用警告:

// MyInterface 指定我们从依赖项中需要的单个函数
type MyInterface interface {
	MyFunc() string
}

func main() {

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

}

“接受接口”是否会破坏废弃工具?

问题

有解决这个问题的方法吗?

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

英文:

Deprecation

The supported way of marking functions as deprecated is something like this:

type MyStruct struct {
}

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

Modern IDEs will highlight any usages of this function and linters might also raise warnings (I haven't personally checked this)

“接受接口”是否会破坏废弃工具?

Accept interfaces. Return structs.

A popular best practise is "Accept interfaces. Return structs." - which tends to encourage SOLID design in software.

However, the following code - which follows this best practise - conceals the deprecation warning:


// 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()

}

“接受接口”是否会破坏废弃工具?

Question

Is there a solution to this problem?

If I were, for example, a library maintainer: how can I ensure that my deprecation warnings are seen by users of the library who are also following best practises and defining their own dependency interfaces.

答案1

得分: 3

这似乎是合乎逻辑的,因为接口的方法尚未被弃用。在接口函数中添加Deprecated:行可能会在这种情况下有所帮助(由于VSCode尚未执行此操作,我没有进行测试)。

// MyInterface 指定我们从依赖项中需要的单个函数
type MyInterface interface {
    // Deprecated: 使用 YourFunc
    MyFunc() string
}

在这种情况下,因为接口只有一个函数,你应该将整个接口标记为已弃用。我知道godoc/pkg.go.dev支持这一点,以Queryer为例。

// MyInterface 指定我们从依赖项中需要的单个函数
// Deprecated: 使用 YourInterface
type MyInterface interface {
    MyFunc() string
}
英文:

That seems logical since the method of the interface has not been deprecated. Adding the Deprecated: line to the interface function might help in this case (didn't test, since VSCode doesn't do this yet).

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

In this case, because the interface only has 1 function you should deprecated the whole thing. Which I know is supported by godoc/pkg.go.dev, take Queryer for example.

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

huangapple
  • 本文由 发表于 2021年11月23日 04:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/70071972.html
匿名

发表评论

匿名网友

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

确定