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

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

You could create a package named old/yourpackage and advise using it if someone relies on deprecated functionality/interfaces and then create an updated package yourpackage without the old stuff.

There's no way to insert either warnings or marking methods as deprecated in other way than in documentation, as per Godoc: documenting Go code blog post which says:

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. There are a few examples in the standard library.

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.

You could also just print a line to stdout / stderr saying "This method is deprecated!" every time a deprecated method is called. You can also include a hint about how the user should correct this.

the Go compiler does not report warnings, only errors that prevent compilation.

Go FAQ

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.

There is a golint issue for reporting use of deprecated identifiers and a godoc issue for striking through or hiding deprecated identifiers.

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