Is Go an object-oriented language?
Go 是面向对象的语言吗?
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 具有类型和方法,并且允许面向对象的编程风格,但没有类型层次结构。Go 中的“接口”概念提供了一种不同的方法,我们认为这种方法易于使用,并且在某些方面更通用。还有一些方法可以将类型嵌入到其他类型中,以提供与子类化类似但不相同的东西。此外,Go 中的方法比 C++ 或 Java 中的方法更通用:它们可以为任何类型的数据定义,甚至是内置类型,例如普通的“未装箱”整数。它们不限于结构(类)。
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.
此外,由于缺乏类型层次结构,Go 中的“对象”感觉比 C++ 或 Java 等语言中的“对象”轻得多。
How do I get dynamic dispatch of methods?
如何获得方法的动态调度?
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.
动态分派方法的唯一方法是通过接口。结构体或任何其他具体类型上的方法始终以静态方式解析。
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.
至少在最著名的语言中,面向对象的编程卷入过多关于类型之间关系的讨论,而这些关系通常可以自动推断出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 中,类型会自动满足指定其方法子集的任何接口,而不是要求程序员提前声明两种类型相关。除了减少簿记工作之外,这种方法还有真正的优点。类型可以同时满足多个接口,而没有传统多重继承的复杂性。接口可以非常轻量级——具有一个甚至零个方法的接口可以表达一个有用的概念。如果出现新想法或用于测试,可以在事后添加接口,而无需注释原始类型。由于类型和接口之间没有显式关系,因此没有要管理或讨论的类型层次结构。
annotating
v. 注解,注释( annotate的现在分词 );
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.
可以使用这些想法来构建类似于类型安全 Unix 管道的东西。例如,了解如何fmt.Fprintf 启用对任何输出(而不仅仅是文件)的格式化打印,或者 bufio包如何与文件 I/O 完全分离,或者image包如何生成压缩图像文件。所有这些想法都源于io.Writer代表单个方法 () 的单个接口 ( Write)。这只是表面现象。Go 的接口对程序的构造方式有着深远的影响。
scratching
刮痕;
profound
adj. 深厚的;意义深远的;严重的;知识渊博的;
It takes some getting used to but this implicit style of type dependency is one of the most productive things about Go.
这需要一些时间来适应,但这种隐式的类型依赖风格是 Go 最有成效的事情之一。