Is Go an object-oriented language?
Go 是面向?qū)ο蟮恼Z言嗎蠕嫁?
Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).
是也不是估蹄。盡管 Go 具有類型和方法,并且允許面向?qū)ο蟮木幊田L(fēng)格,但沒有類型層次結(jié)構(gòu)。Go 中的“接口”概念提供了一種不同的方法,我們認(rèn)為這種方法易于使用套鹅,并且在某些方面更通用。還有一些方法可以將類型嵌入到其他類型中汰具,以提供與子類化類似但不相同的東西卓鹿。此外,Go 中的方法比 C++ 或 Java 中的方法更通用:它們可以為任何類型的數(shù)據(jù)定義留荔,甚至是內(nèi)置類型吟孙,例如普通的“未裝箱”整數(shù)。它們不限于結(jié)構(gòu)(類)聚蝶。
analogous
adj. 相似的;可比擬的;
Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.
此外杰妓,由于缺乏類型層次結(jié)構(gòu),Go 中的“對(duì)象”感覺比 C++ 或 Java 等語言中的“對(duì)象”輕得多碘勉。
How do I get dynamic dispatch of methods?
如何獲得方法的動(dòng)態(tài)調(diào)度巷挥?
The only way to have dynamically dispatched methods is through an interface. Methods on a struct or any other concrete type are always resolved statically.
動(dòng)態(tài)分派方法的唯一方法是通過接口。結(jié)構(gòu)體或任何其他具體類型上的方法始終以靜態(tài)方式解析验靡。
Why is there no type inheritance?
為什么沒有類型繼承倍宾?
Object-oriented programming, at least in the best-known languages, involves too much discussion of the relationships between types, relationships that often could be derived automatically. Go takes a different approach.
至少在最著名的語言中,面向?qū)ο蟮木幊叹砣脒^多關(guān)于類型之間關(guān)系的討論胜嗓,而這些關(guān)系通掣咧埃可以自動(dòng)推斷出Go 采用了不同的方法。
Rather than requiring the programmer to declare ahead of time that two types are related, in Go a type automatically satisfies any interface that specifies a subset of its methods. Besides reducing the bookkeeping, this approach has real advantages. Types can satisfy many interfaces at once, without the complexities of traditional multiple inheritance. Interfaces can be very lightweight—an interface with one or even zero methods can express a useful concept. Interfaces can be added after the fact if a new idea comes along or for testing—without annotating the original types. Because there are no explicit relationships between types and interfaces, there is no type hierarchy to manage or discuss.
在 Go 中辞州,類型會(huì)自動(dòng)滿足指定其方法子集的任何接口怔锌,而不是要求程序員提前聲明兩種類型相關(guān)。除了減少簿記工作之外,這種方法還有真正的優(yōu)點(diǎn)产禾。類型可以同時(shí)滿足多個(gè)接口,而沒有傳統(tǒng)多重繼承的復(fù)雜性牵啦。接口可以非常輕量級(jí)——具有一個(gè)甚至零個(gè)方法的接口可以表達(dá)一個(gè)有用的概念亚情。如果出現(xiàn)新想法或用于測(cè)試,可以在事后添加接口哈雏,而無需注釋原始類型楞件。由于類型和接口之間沒有顯式關(guān)系,因此沒有要管理或討論的類型層次結(jié)構(gòu)裳瘪。
annotating
v. 注解土浸,注釋( annotate的現(xiàn)在分詞 );
It's possible to use these ideas to construct something analogous to type-safe Unix pipes. For instance, see how fmt.Fprintf enables formatted printing to any output, not just a file, or how the bufio package can be completely separate from file I/O, or how the image packages generate compressed image files. All these ideas stem from a single interface (io.Writer) representing a single method (Write). And that's only scratching the surface. Go's interfaces have a profound influence on how programs are structured.
可以使用這些想法來構(gòu)建類似于類型安全 Unix 管道的東西。例如彭羹,了解如何fmt.Fprintf 啟用對(duì)任何輸出(而不僅僅是文件)的格式化打印黄伊,或者 bufio包如何與文件 I/O 完全分離,或者image包如何生成壓縮圖像文件派殷。所有這些想法都源于io.Writer代表單個(gè)方法 () 的單個(gè)接口 ( Write)还最。這只是表面現(xiàn)象。Go 的接口對(duì)程序的構(gòu)造方式有著深遠(yuǎn)的影響毡惜。
scratching
刮痕;
profound
adj. 深厚的;意義深遠(yuǎn)的;嚴(yán)重的;知識(shí)淵博的;
It takes some getting used to but this implicit style of type dependency is one of the most productive things about Go.
這需要一些時(shí)間來適應(yīng)拓轻,但這種隱式的類型依賴風(fēng)格是 Go 最有成效的事情之一。