這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
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)』,轉載請註明來源!