【译】Golang中使用『弃用(Deprecate)』

您所在的位置:网站首页 deprecuate 【译】Golang中使用『弃用(Deprecate)』

【译】Golang中使用『弃用(Deprecate)』

2023-05-13 07:19| 来源: 网络整理| 查看: 265

【译】Golang中使用『弃用(Deprecate)』 Bryce · · 6011 次点击 · · 开始浏览     这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。 第一次,站长亲自招 Gopher 了>>>

Go语言很长时间都没有一套标记弃用API的定义规范。这几年,出现了个规范可以在文档当中添加弃用注释。

现在,标准库开始使用这个格式了。

举个例子,Go 1.8的包中sql/driver.Execer被弃用,这里增加了一套注释,它可以被godoc识别。

// Execer is an optional interface that may be implemented by a Conn. // // If a Conn does not implement Execer, the sql package's DB.Exec will // first prepare a query, execute the statement, and then close the // statement. // // Exec may return ErrSkip. // // Deprecated: Drivers should implement ExecerContext instead (or additionally). type Execer interface { Exec(query string, args []Value) (Result, error) }

弃用的通知会出现在godoc里面,注释需要以Deprecated开头,后面再写上代替此API的提示。

// Deprecated: Use strings.HasPrefix instead.

使用API的用户就可以根据这个提示使用新的API了。

Additional to the notices, there is an effort going on to discourage users to keep depending on the deprecated APIs. 除了注释通知之外,还可以有一些方法来阻止用户一直使用被弃用的API。

可以参考下面的文章:

《默认隐藏弃用API的方法》 《在godoc.org上面隐藏弃用的API》 《通过golint通知弃用的API》

总之,可以使用这个注释格式来支持弃用通知。不要用DEPRECATED或者This type is deprecated。将来,你可以用一些工具来通知用户停止使用弃用的API。

阅读原文Deprecating things in Go

原文链接:【译】Golang中使用『弃用(Deprecate)』,转载请注明来源!

有疑问加站长微信联系(非本文作者)

本文来自:Cyeam

感谢作者:Bryce

查看原文:【译】Golang中使用『弃用(Deprecate)』

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信 6011 次点击   加入收藏 微博 赞


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3