如何在Go中将代码标记为已弃用?

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

How do you mark code as deprecated in Go?

问题

在Go语言中,你如何将代码标记为已弃用,以便用户在使用时收到警告?

英文:

In Go, how do you mark code as deprecated so that users get a warning when using it?

答案1

得分: 150

Godoc:记录Go代码中提到了关于将代码标记为已弃用的内容:

> 要表示一个标识符不应该被使用,可以在其文档注释中添加一个以“Deprecated:”开头的段落,后面跟上一些关于弃用的信息。

以下是语法的示例(在此处查看更多示例):

// Title treats s as UTF-8-encoded bytes and returns a copy with all Unicode letters that begin
// words mapped to their title case.                                                            
//                                                                             
// Deprecated: The rule Title uses for word boundaries does not handle Unicode              
// punctuation properly. Use golang.org/x/text/cases instead.                 
func Title(s []byte) []byte {  
    ⋮
}

文档网站pkg.go.dev将已弃用的标识符的文档隐藏在一个“显示”按钮后面。

staticcheck工具会报告使用已弃用标识符的情况(参见SA1019)。

Goland IDE代码检查器会报告使用已弃用标识符的情况。

英文:

Godoc: documenting Go code says this about marking code as deprecated:

> To signal that an identifier should not be used, add a paragraph to its doc comment that begins with "Deprecated:" followed by some information about the deprecation.

Here's an example of the syntax (view more here):

// Title treats s as UTF-8-encoded bytes and returns a copy with all Unicode letters that begin
// words mapped to their title case.                                                            
//                                                                         
// Deprecated: The rule Title uses for word boundaries does not handle Unicode              
// punctuation properly. Use golang.org/x/text/cases instead.                 
func Title(s []byte) []byte {  
    ⋮
}

The documentation site pkg.go.dev hides the documentation for deprecated identifiers behind a click of a "show" button.

The staticcheck tool reports use of deprecated identifiers (see SA1019).

The Goland IDE code inspector reports use of deprecated identifiers.

答案2

得分: 12

// 已弃用:FunctionName 已弃用。

英文:

Add this comment to your function / struct:

// Deprecated: FunctionName is deprecated.

答案3

得分: -6

在Go编译器中没有对此提供支持(无论是在5g/6g/8g还是在gccgo中)。

据我所知,目前没有针对此问题的代码检查工具。

唯一的方法是在文档中放置弃用警告,或者直接删除代码。

英文:

There is no support for this in the Go compiler (neither in 5g/6g/8g nor in gccgo).

As far as I know, there is currently no code checking tool for this.

The only way is to put the deprecation warning in documentation, or simply delete the code.

huangapple
  • 本文由 发表于 2011年10月21日 20:54:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/7849663.html
匿名

发表评论

匿名网友

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

确定