Go?interface?接口的最佳實(shí)踐經(jīng)驗(yàn)分享
Go語言-Go 接口的最佳實(shí)踐
原文連接:https://blog.boot.dev/golang/golang-interfaces/
Go 中的接口允許我們暫時(shí)將不同的類型視為相同的數(shù)據(jù)類型,因?yàn)檫@兩種類型實(shí)現(xiàn)相同的行為。它們是Go程序員工具箱的核心,并且經(jīng)常被新的Go開發(fā)人員不正確地使用,導(dǎo)致代碼不可讀且經(jīng)常有錯(cuò)誤。
什么是Golang中的interface
In Go, an interface is a custom type that other types are able to implement, which gives Go developers a powerful way to use abstraction. Interfaces are named collections of method signatures, and when other types implement all the required methods, they implicitly implement the interface.
在 Go 中,接口是其他類型可以實(shí)現(xiàn)的自定義類型,這為 Go 開發(fā)人員提供了使用抽象的強(qiáng)大方式。接口是方法簽名的集合,當(dāng)其他類型實(shí)現(xiàn)所有需要的方法時(shí),它們隱式地實(shí)現(xiàn)了接口。
例如,Go 中的errors
是接口,標(biāo)準(zhǔn)error
接口很簡(jiǎn)單,一個(gè)類型要被認(rèn)為是error
,所需要做的就是定義一個(gè) Error ()
方法,該方法不接受任何參數(shù),并返回一個(gè)字符串。
type error interface { Error() string }
錯(cuò)誤error
的簡(jiǎn)單性使得編寫日志和metrics 實(shí)現(xiàn)更加容易。讓我們定義一個(gè)表示網(wǎng)絡(luò)問題的結(jié)構(gòu)體:
type networkProblem struct { message string code int }
然后我們可以定義一個(gè) Error ()方法:
func (np networkProblem) Error() string { return fmt.Sprintf("network error! message: %s, code: %v", np.message, np.code) }
現(xiàn)在,我們可以在接受錯(cuò)誤的任何地方使用 networkProblem
struct 的實(shí)例。
func handleErr(err error) { fmt.Println(err.Error()) } np := networkProblem{ message: "we received a problem", code: 404, } handleErr(np) // prints "network error! message: we received a problem, code: 404"
編寫接口的最佳實(shí)踐
編寫干凈的接口是困難的。坦率地說,任何時(shí)候你都在處理代碼中的抽象,如果你不小心的話,簡(jiǎn)單可以很快變成復(fù)雜。讓我們回顧一下保持interfaces
整潔的一些經(jīng)驗(yàn)法則。
- Keep interfaces small 保持interfaces足夠小
- Interfaces should have no knowledge of satisfying types 接口應(yīng)該沒有令人滿意的類型的知識(shí)
- Interfaces are not classes 接口不是類
1. 保持interfaces足夠小
If there is only one piece of advice that you take away from this article, make it this: keep interfaces small! Interfaces are meant to define the minimal behavior necessary to accurately represent an idea or concept.
如果您從本文中只得到了一條建議,那就是: 保持接口小一些!接口意味著定義精確表示一個(gè)想法或概念所需的最小行為。
下面是一個(gè)大型接口的標(biāo)準(zhǔn) HTTP package的例子,它是定義最小行為的一個(gè)很好的例子:
type File interface { io.Closer io.Reader io.Seeker Readdir(count int) ([]os.FileInfo, error) Stat() (os.FileInfo, error) }
Any type that satisfies the interface’s behaviors can be considered by the HTTP package as a File. This is convenient because the HTTP package doesn’t need to know if it’s dealing with a file on disk, a network buffer, or a simple []byte.
任何滿足接口行為的類型都可以被 HTTP package 視為File
。這很方便,因?yàn)?HTTP package 不需要知道它是在處理磁盤上的文件、還是網(wǎng)絡(luò)緩沖區(qū)或是[]byte
。
2. Interfaces Should Have No Knowledge of Satisfying Types
An interface should define what is necessary for other types to classify as a member of that interface. They shouldn’t be aware of any types that happen to satisfy the interface at design time.
接口應(yīng)該定義其他類型作為該接口的成員所必需的內(nèi)容。他們不應(yīng)該知道在設(shè)計(jì)時(shí)為了滿足接口而發(fā)生的任何類型。
例如,假設(shè)我們正在構(gòu)建一個(gè)接口來描述定義汽車所必需的構(gòu)成元素。
type car interface { Color() string Speed() int IsFiretruck() bool }
Color() and Speed() make perfect sense, they are methods confined to the scope of a car. IsFiretruck() is an anti-pattern. We are forcing all cars to declare whether or not they are firetrucks. In order for this pattern to make any amount of sense, we would need a whole list of possible subtypes. IsPickup(), IsSedan(), IsTank()… where does it end??
Color()
和Speed()
非常合理,它們是限制在汽車范圍內(nèi)的方法。IsFiretruck ()
是一個(gè)反模式。我們正在強(qiáng)制所有的汽車申報(bào)它們是否是消防車。為了使這個(gè)模式具有任何意義,我們需要一個(gè)可能的子類型的完整列表。IsPickup () ,IsSedan () ,IsTank () … 它在哪里結(jié)束?
Instead, the developer should have relied on the native functionality of type assertion to derive the underlying type when given an instance of the car interface. Or, if a sub-interface is needed, it can be defined as:
相反,當(dāng)給定汽車接口的實(shí)例時(shí),開發(fā)人員應(yīng)該依賴于類型斷言的原生功能來派生基礎(chǔ)類型?;蛘?,如果需要子接口,可以將其定義為:
type firetruck interface { car HoseLength() int }
它繼承了汽車所需的方法,并增加了一個(gè)額外的所需方法,使汽車一輛消防車。
3. 接口不是類
- Interfaces are not classes, they are slimmer. 接口不是類,它們更小
- Interfaces don’t have constructors or deconstructors that require that data is created or destroyed. 接口沒有要求創(chuàng)建或銷毀數(shù)據(jù)的構(gòu)造函數(shù)或解構(gòu)函數(shù)
- Interfaces aren’t hierarchical by nature, though there is syntactic sugar to create interfaces that happen to be supersets of other interfaces. 接口本質(zhì)上并不具有層次性,盡管在創(chuàng)建恰好是其他接口的超集的接口時(shí)存在語法糖
- Interfaces define function signatures, but not underlying behavior. Making an interface often won’t 接口定義函數(shù)簽名,但不定義底層行為。制作interface通常不會(huì)在結(jié)構(gòu)方法方面不干擾您的代碼。例如,如果五種類型滿足錯(cuò)誤接口,那么它們都需要自己的版本的Error() function. 函數(shù)
有關(guān)接口的更多信息
空的接口
空接口沒有指定任何方法,因此 Go 中的每個(gè)類型都實(shí)現(xiàn)了空接口。
interface{}
It’s for this reason that developers sometimes use a map[string]interface{} to work with arbitrary JSON data, although I recommend using anonymous structs instead where possible.
出于這個(gè)原因,開發(fā)人員有時(shí)使用 map[string]interface{}
來處理任意 JSON 數(shù)據(jù),盡管我推薦在可能的情況下使用匿名結(jié)構(gòu)。
Zero value of an interface
Interfaces can be nil, in fact, it’s their zero value. That’s why when we check for errors in Go, we’re always checking if err != nil, because err is an interface.
接口可以是 nil,事實(shí)上,這是它們的零值。這就是為什么當(dāng)我們?cè)?Go 中檢查錯(cuò)誤時(shí),我們總是檢查err != nil
,因?yàn)?err
是一個(gè)接口。
指針上的接口
It’s a common “gotcha” in Go to implement a method on a pointer type and expect the underlying type to implement the interface, it doesn’t work like that.
在 Go 中,在指針類型上實(shí)現(xiàn)一個(gè)方法并期望底層類型實(shí)現(xiàn)接口是一個(gè)常見的“明白了”,它不是這樣工作的。
type rectangle interface { height() int width() int } type square struct { length int } func (sq *square) width() int { return sq.length } func (sq *square) height() int { return sq.length }
Though you may expect it to, in this example the square type does not implement the rectangle interface. The *square type does. If I wanted the square type to implement the rectangle interface I would just need to remove the pointer receivers.
雖然您可能希望這樣做,但是在這個(gè)示例中,正方形類型不實(shí)現(xiàn)矩形接口。它使用的是*square
。如果我想讓正方形類型實(shí)現(xiàn)矩形接口,我只需要?jiǎng)h除指針接收器。
type rectangle interface { height() int width() int } type square struct { length int } func (sq square) width() int { return sq.length } func (sq square) height() int { return sq.length }
到此這篇關(guān)于Go interface 接口的最佳實(shí)踐的文章就介紹到這了,更多相關(guān)Go interface 接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本詳解
這篇文章主要給大家介紹了關(guān)于golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01golang給函數(shù)參數(shù)設(shè)置默認(rèn)值的幾種方式小結(jié)(函數(shù)參數(shù)默認(rèn)值
在日常開發(fā)中我們有時(shí)候需要使用默認(rèn)設(shè)置,下面這篇文章主要給大家介紹了關(guān)于golang給函數(shù)參數(shù)設(shè)置默認(rèn)值的幾種方式小結(jié)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01利用golang的字符串解決leetcode翻轉(zhuǎn)字符串里的單詞
這篇文章主要介紹了利用golang的字符串解決leetcode翻轉(zhuǎn)字符串里的單詞,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12Go?實(shí)現(xiàn)?WebSockets和什么是?WebSockets
這篇文章主要介紹了Go?實(shí)現(xiàn)?WebSockets和什么是?WebSockets,WebSockets?是構(gòu)建實(shí)時(shí)應(yīng)用程序的第一大解決方案,在線游戲、即時(shí)通訊、跟蹤應(yīng)用程序等,下文相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下2022-04-04go實(shí)現(xiàn)一個(gè)分布式限流器的方法步驟
項(xiàng)目中需要對(duì)api的接口進(jìn)行限流,本文主要介紹了go實(shí)現(xiàn)一個(gè)分布式限流器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01