Google Go

1258017251|%Y-%m-%d|agohover

最近 Google 最大的新聞大概就是他們發表了 Go 這個語言。就網站上的說法,Go 的賣點大概是:

  • 具有如 Python 般高階語言的特性,例如內建的 hash map、自動記憶體管理等等。
  • 編譯及執行速度都很快。
  • 在語言層面支援平行運算。
  • 不需明確定義類別的繼承關係。

就前面兩點而言,其實許多語言都同時具備高階語言的便利以及中階語言 (ex. C) 的高效能,而 Haskell 或 Erlang 這類函數語言也具備了平行化的特性。不過最後一點倒是相當有趣,以下列的例子來說:

type Stream interface {
    Read(b Buffer);
    Write(b Buffer);
}

type File struct {
    fd int; // file descriptor
}

func (File*) Read(b Buffer) {...}
func (File*) Write(b Buffer) {...}

這邊定義了 Stream 這個 interface,類似 Java 中的 interface 或 C++ 中的純抽象類別。另外也定義了 File 這個實作出 ReadWrite 的類別。需要注意的是,在 Go 中並不需要特別指定 File「實作了」Stream 的界面--只要 File 提供了 Stream 所指定的 method,File 就可以隱式轉型成 Stream 並達成多型。這有點類似 C++ 中 concept 的概念:只要某個東西能夠用 ++ 遞增、用 * 取值、以及用 == 比較,就可以拿來當 iterator 使用,而不需要明確說出他們的繼承關係。不同的地方在於 C++ 是 compile-time polymorphism,而 Go 則是 run-time polymorphism 罷了。

12/25 不學無術的小補充:上述的特性稱之為 duck typing

另一方面,有些顯然是大家覺得應該要有的功能,在 Go 中卻付之闕如。在 Slashdot 上有些人的評論相當中肯:

They're billing Go as a "systems language." If by "system" they mean "application server in a Google data center" then I suppose that's correct. Previously the term "systems language" referred to languages suitable for operation system implementation, including device drivers, virtual memory management and other persnickety low-level software. Lack of pointer arithmetic and explicit memory management probably precludes any attempt to use Go as a "systems" language by that definition (although there are exceptions [microsoft.com] to that thinking.)

Go 禁止指標運算也不允許 programmer 自行管理記憶體,這意味著你最好不要期待 Go 適用於任何環境,比如說 embedded system。

Go seems to suffer from the problem of not being done. Case in point: exceptions.
Google's C++ style guide [googlecode.com] forbids exceptions. This is because of historical reasons - Google's codebase is not exception-tolerant.
Because Go doesn't have exceptions, programmers won't write exception-safe code. Since Go is garbage-collected, this is less of an issue compared with C++, but there are still cases where things like file handles or external resources need to be cleaned up.
Two, three, or four years down the road, it's going to be hard to use exceptions in Go, because the existing code that's out there won't inter-operate properly with code that throws exceptions.

一針見血。根據 Walter Bright 的說法,exception 並非短時間內可以實作出的功能,更何況他們還想設計一套新的機制。Google 宣稱因為大多數 C++ code 不用 exception,所以他們也只好不用,結果現在又重覆了這段黑歷史。


延申閱讀:Why Google's Go is a Bore


Comments

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License