When did Go get generic types?
Go 何時獲得泛型類型游岳?
The Go 1.18 release added type parameters to the language. This permits a form of polymorphic or generic programming. See the language spec and the proposal for details.
Go 1.18 版本向該語言添加了類型參數(shù)绑谣。這允許某種形式的多態(tài)或通用編程曾棕。有關(guān)詳細(xì)信息崔挖,請參閱語言規(guī)范和 提案。
polymorphic
adj. 多形的恢氯,多態(tài)的靖苇,多形態(tài)的;多晶形;
Why was Go initially released without generic types?
為什么 Go 最初發(fā)布時沒有泛型類型?
Go was intended as a language for writing server programs that would be easy to maintain over time. (See this article for more background.) The design concentrated on things like scalability, readability, and concurrency. Polymorphic programming did not seem essential to the language's goals at the time, and so was initially left out for simplicity.
Go 的目的是作為一種用于編寫易于維護(hù)的服務(wù)器程序的語言模捂。(有關(guān)更多背景信息捶朵,請參閱本文。)設(shè)計重點(diǎn)關(guān)注可擴(kuò)展性狂男、可讀性和并發(fā)性等方面综看。多態(tài)編程對于當(dāng)時的語言目標(biāo)來說似乎并不重要,因此最初為了簡單起見而被排除在外岖食。
concentrated
adj. 集中的;全神貫注的;濃縮的;汰選出來的;
v. 集中(concentrate的過去分詞);聚集红碑,集結(jié);凝縮,濃縮;[礦]汰選;
Generics are convenient but they come at a cost in complexity in the type system and run-time. It took a while to develop a design that we believe gives value proportionate to the complexity.
泛型很方便泡垃,但代價是類型系統(tǒng)和運(yùn)行時的復(fù)雜性析珊。我們花了一段時間才開發(fā)出一種設(shè)計羡鸥,我們相信這種設(shè)計的價值與復(fù)雜性成正比。
Why does Go not have exceptions?
為什么 Go 沒有異常唾琼?
We believe that coupling exceptions to a control structure, as in the try-catch-finally
idiom, results in convoluted code. It also tends to encourage programmers to label too many ordinary errors, such as failing to open a file, as exceptional.
我們認(rèn)為兄春,將異常耦合到控制結(jié)構(gòu)(如try-catch-finally慣用語中那樣)會導(dǎo)致代碼變得復(fù)雜。它還傾向于鼓勵程序員將太多普通錯誤(例如無法打開文件)標(biāo)記為異常錯誤锡溯。
coupling
n. [電]耦合;聯(lián)結(jié);[機(jī)]管箍;(火車的)車鉤;
v. 連接(couple的現(xiàn)在分詞);
idiom
n. 習(xí)語赶舆,成語;方言,土語;(語言)風(fēng)格;慣用語法;
convoluted
adj. 盤繞的祭饭,卷曲的;復(fù)雜的;
v. 回旋芜茵,卷繞,盤旋( convolute的過去式和過去分詞 );
label
n. 標(biāo)簽;標(biāo)記;稱謂;唱片公司;
v. 貼標(biāo)簽于;用標(biāo)簽表明;(尤指不公平地)把…稱之為;
Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. A canonical error type, coupled with Go's other features, makes error handling pleasant but quite different from that in other languages.
Go 采用了不同的方法倡蝙。對于簡單的錯誤處理九串,Go 的多值返回可以輕松報告錯誤,而無需重載返回值寺鸥。 規(guī)范的錯誤類型與 Go 的其他功能相結(jié)合猪钮,使錯誤處理變得令人愉快,但與其他語言中的錯誤處理有很大不同胆建。
approach
v. 接近烤低,臨近;對付,處理;接洽笆载,要求;
n. 方法扑馁,方式;接近,來臨;接洽凉驻,要求;途徑腻要,道路;相似的事物;
canonical
adj. 權(quán)威的;見于<圣經(jīng)>正經(jīng)篇目的;大教堂教士的;按照教規(guī)的;
Go also has a couple of built-in functions to signal and recover from truly exceptional conditions. The recovery mechanism is executed only as part of a function's state being torn down after an error, which is sufficient to handle catastrophe but requires no extra control structures and, when used well, can result in clean error-handling code.
Go 還有一些內(nèi)置函數(shù)來發(fā)出信號并從真正的異常情況中恢復(fù)±缘牵恢復(fù)機(jī)制僅作為錯誤后被拆除的函數(shù)狀態(tài)的一部分執(zhí)行雄家,這足以處理災(zāi)難,但不需要額外的控制結(jié)構(gòu)胀滚,并且如果使用得當(dāng)咳短,可以產(chǎn)生干凈的錯誤處理代碼。
See the Defer, Panic, and Recover article for details. Also, the Errors are values blog post describes one approach to handling errors cleanly in Go by demonstrating that, since errors are just values, the full power of Go can be deployed in error handling.
有關(guān)詳細(xì)信息蛛淋, 請參閱延遲、緊急和恢復(fù)一文篡腌。此外褐荷,錯誤是值博客文章描述了一種在 Go 中干凈地處理錯誤的方法,通過演示嘹悼,由于錯誤只是值叛甫,因此可以在錯誤處理中部署 Go 的全部功能层宫。
Why does Go not have assertions?
為什么 Go 沒有斷言?
Go doesn't provide assertions. They are undeniably convenient, but our experience has been that programmers use them as a crutch to avoid thinking about proper error handling and reporting. Proper error handling means that servers continue to operate instead of crashing after a non-fatal error. Proper error reporting means that errors are direct and to the point, saving the programmer from interpreting a large crash trace. Precise errors are particularly important when the programmer seeing the errors is not familiar with the code.
Go 不提供斷言其监。不可否認(rèn)萌腿,它們很方便,但我們的經(jīng)驗是抖苦,程序員將它們用作拐杖毁菱,以避免考慮正確的錯誤處理和報告。正確的錯誤處理意味著服務(wù)器在非致命錯誤后繼續(xù)運(yùn)行而不是崩潰锌历。正確的錯誤報告意味著錯誤是直接且切題的贮庞,使程序員無需解釋大量的崩潰跟蹤。當(dāng)看到錯誤的程序員不熟悉代碼時究西,精確的錯誤尤為重要窗慎。
crutch
n. 拐杖;支持物;精神上的寄托;胯部;
We understand that this is a point of contention. There are many things in the Go language and libraries that differ from modern practices, simply because we feel it's sometimes worth trying a different approach.
我們知道這是一個爭論點(diǎn)。Go 語言和庫中有很多與現(xiàn)代實踐不同的東西卤材,僅僅是因為我們覺得有時值得嘗試不同的方法遮斥。