[Go] 閱讀心得 不負責任翻譯 Golang Frequently Asked Questions (FAQ) PART2

https://golang.org/doc/faq

官方FAQ 提供一些有趣的資訊,很認真的試圖都理解一下,由於內容太多,拆成幾個部分來看。

PART2這裡有 Types 


Types


Is Go an object-oriented language?

Go 說是,也不是,有OO的風格,但沒有『繼承』,在GO裡面的interface也跟其他語言的interface有所不同。

透過interface可以達到某些類似subclass的目的,但GO聲明這樣的東西,絕對不等於subclass。

(BOX 和 UBOX 的概念我不清楚,找到的參考是https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/types/boxing-and-unboxing ,但還是不懂意思,GO表示可以把ubox integer也塞進struct,這很厲害?)

再次聲明,沒有了『繼承』,少了負擔感覺真好。
(OS: 你到底多討厭繼承,這好像我第三次看到了,然後我也覺得就直說不是傳統物件導向的語言,別甲仙啦。)



How do I get dynamic dispatch of methods?

『dynamic dispatch of methods』是一個概念,搭配Polymorphism的概念一起看,在GO能做到這件事的唯一方式就是透過interface。

在struct裡面的method和具體的method都是靜態處理。



Why is there no type inheritance?

GO想走別的方向,不想要有『繼承』。

沒有『繼承』帶來實質上的好處,『繼承』機制所需要的紀錄彼此關係這件事情,造成很高的複雜度和負擔。

GO的interface可以達到你的目的,再者,沒有了『繼承』就不需要管型態與interface的『明確關係』,每個type可以乾淨的跟interface結合。

(所以其他OO語言,有繼承的type,如果有使用interface,他們之間會定義某種關係狀態,拖累某些處理和速度是嗎?)

舉例fmt.Fprintf 可以印出任何型態的東西,io.Writer可以各種寫入,就是靠『沒繼承』+ 『interface』的成果。

如此用interface來隱性表現彼此的依賴,是go最大的特點。



Why is len a function and not a method?

len 對於Go來講要成為method,勢必要使用interface,做為大家都能使用的函式用得好好的,若要用interface介接使用,勢必增加很多複雜度。

(function:沒有特別屬於誰的函式,大家都可用(global的概念))
(method:屬於某某的函式,如strconv.Atoi(),Atoi()屬於strconv)



Why does Go not support overloading of methods and operators?

如果method 對應只需要管名字,不需要管型態,這件事情就很簡單。

GO 希望這件事情上能夠簡單化(簡單,臭了嗎?)



Why doesn’t Go have “implements” declarations?

GO 使用interface的方式就是在做implements。

GO的使用interface語意簡潔,並且被視為GO語言靈活的特色之一。



How can I guarantee my type satisfies an interface?(code示範,我會,略)

GO 在編譯時就會檢查是否有滿足使用interface的定義。



Why doesn’t type T satisfy the Equal interface?


type T int
func (t T) Equal(u T) bool { return t == u } // does not satisfy Equaler

這個就是單純method on struct 的部分,沒有用到Equaler,不難理解


type T2 int
func (t T2) Equal(u Equaler) bool { return t == u.(T2) }  // satisfies Equaler

這個就有點妙了,interface 指定要有的method,在method 本身傳入了interface的結構,
GO說明若是滿足T2的型態,當然可以這樣傳,而且型態斷言成T2也會成功

第三個例子,因為GO本身沒有overload,所以理所當然的,不被認為有實作Equaler。



Can I convert a []T to an []interface{}?

這是不同東西,原本有指定type不可轉為interface,以memory來看也是不同位置。



Can I convert []T1 to []T2 if T1 and T2 have the same underlying type?

大意是說GO允許型態轉換,但可不代表他們只是改個名字的差別,要轉換,GO希望大家就明確地去做轉換這件事。



Why is my nil error value not equal to nil?(不是很肯定我的理解是否正確)

interfaces,或者說GO裡面的東西,會包含兩個項目,一個是type(型態),一個是value(值)。

value是動態的,type與value具體內容息息相關。

假設 var i int = 3 ,在裡面翻譯翻譯就是(int,3),而nil的type和value永遠是nil,所以是(nil,nil)

然而對於point,他的內容可能是(*int, nil),有時候根據運行狀況,後來的value可能是有其他值,或者nil。

如此的不確定在回傳的指定type,可能造成困擾,於是GO鼓勵你回傳用『error』,error會幫你處理好(太神啦)。



Why are there no untagged unions, as in C?

對於union這種怪咖的型態,只有一點稀薄印象,GO表示不安全,就不用。



Why does Go not have variant types?

Variant types我不確定要翻成什麼,但我知道這像是C#的var 。

Variant types是多種type的集合,雖然終點是個集合體,但對於語法樹來講,來源的每個type還是各佔一個點。

(想起compilier課,心中一陣惡寒)

考慮到與interface的項目可能有混淆,GO最後決定捨棄它。

但GO也是有類似的東西,如上述舉例的error type,Variant types雖然可行,但不優雅。



Why does Go not have covariant result types?

covariant return type,中文翻『共變回傳』,這個概念是我之前不知道的。

這個東西在java比較好實作,簡單來說繼承要覆寫被繼承的method,回傳一定要跟原本回傳的型態一樣。

支援covariant return type的話,回傳的型態可以容許繼承原本回傳型態的子型態。

GO 表示interface裡面的訂出要實作的method,回傳型態用interface{}就解決啦!


Created Date : 2018/08/02

Last Updated Date : 2018/08/02

留言

這個網誌中的熱門文章

[Go] 型態轉換 type convert

[Go] Golang用法 package import 前面的底線

[Go] 指標 pointer with golang