5.4 Go语言中自定义类型与结构体(Struct)
Posted 孙琦Ray
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.4 Go语言中自定义类型与结构体(Struct)相关的知识,希望对你有一定的参考价值。
Go是面向对象的语言吗
在很多文章中都提到,Go语言中的struct某种意义就是对应其他语言中的class,就该问题特意阅读了Go语言官网的常见问题,有这样的描述
Is Go an object-oriented language?
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).
Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.
但是又舍弃了很多类在继承的特性,让Go语言变得更轻。
以上是关于5.4 Go语言中自定义类型与结构体(Struct)的主要内容,如果未能解决你的问题,请参考以下文章