Types(類型)

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 最有成效的事情之一。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末经伙,一起剝皮案震驚了整個(gè)濱河市扶叉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌帕膜,老刑警劉巖枣氧,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異泳叠,居然都是意外死亡作瞄,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門危纫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宗挥,“玉大人,你說我怎么就攤上這事种蝶∑豕ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵螃征,是天一觀的道長(zhǎng)搪桂。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么踢械? 我笑而不...
    開封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任酗电,我火速辦了婚禮,結(jié)果婚禮上内列,老公的妹妹穿的比我還像新娘撵术。我一直安慰自己,他們只是感情好话瞧,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開白布嫩与。 她就那樣靜靜地躺著,像睡著了一般交排。 火紅的嫁衣襯著肌膚如雪划滋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天埃篓,我揣著相機(jī)與錄音处坪,去河邊找鬼。 笑死架专,一個(gè)胖子當(dāng)著我的面吹牛稻薇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播胶征,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼塞椎,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了睛低?” 一聲冷哼從身側(cè)響起案狠,我...
    開封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎钱雷,沒想到半個(gè)月后骂铁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡罩抗,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年拉庵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片套蒂。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡钞支,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出操刀,到底是詐尸還是另有隱情烁挟,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布骨坑,位于F島的核電站撼嗓,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜且警,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一粉捻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧斑芜,春花似錦杀迹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浅碾。三九已至大州,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間垂谢,已是汗流浹背厦画。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滥朱,地道東北人根暑。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像徙邻,于是被迫代替她去往敵國(guó)和親排嫌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容