【譯】Golang中使用『棄用(Deprecate)』

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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)』,轉載請註明來源!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.