How do you mark code as deprecated in Go?

Go

Go Problem Overview


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

Go Solutions


Solution 1 - Go

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.

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).

There is a golint issue for reporting use of deprecated identifiers.

Solution 2 - Go

Add this Comment to your function / struct // Deprecated: FunctionName is deprecated.

Solution 3 - Go

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.

Solution 4 - Go

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMat RyerView Question on Stackoverflow
Solution 1 - GoBayta DarellView Answer on Stackoverflow
Solution 2 - GoGoHumbleView Answer on Stackoverflow
Solution 3 - Gouser811773View Answer on Stackoverflow
Solution 4 - GoFelixView Answer on Stackoverflow