1.1 语言特点

优质
小牛编辑
120浏览
2023-12-01

勿在浮沙筑高台

使用这个题目,完全是向《深入浅出MFC》的侯捷致敬。

Go语言优势

  1. 可直接编译成机器码,不依赖其他库,glibc的版本有一定要求,Go 编译生成的是一个静态可执行文件,部署就是扔一个文件上去就完成了。这让部署变得异常方便,完全不需要操心应用所需的各种包、库的依赖关系,大大减轻了维护的负担。如果你是做php的,你就会知道编译一大堆的依赖是多么的繁琐,经常是你需要增加一个memcached支持,却需要安装一大堆的依赖,还很有可能因为版本的原因,你编译不过去,有的时候你--skip-broken,还有的时候你加上这个语句也过不去。
  2. 静态类型、编译型语言,静态类型的语言可以在编译的时候检查出来隐藏的大多数问题。
  3. 跨平台,在大多数平台下,你只要下载与之对应的GO的安装包,并配置好GOPATH、GOROOT,你编写的语言就可以一次编程,处处编译了。在你的程序不包含cgo的情况下,我们在大部分开发中都几乎感觉不到不同平台的差异。
  4. 内置runtime,支持垃圾回收,无需开发者干预,虽然目前来说GC不算完美,但是足以应付我们所能遇到的大多数情况,特别是Go1.1之后的GC。不过,GO也允许我们进行人工的干预。
  5. 代码格式强制统一,Go语言里面内置了很多工具,其中gofmt工具,自动化格式化代码,能够让团队review变得如此的简单,代码格式统一,想不一样都很困难。这点我觉着比Python都要优异。
  6. 内嵌C支持,GO的主导开发人员Thompson,本身就是C语言的发明人之一,所以Go里面也可以直接包含c代码,利用现有的丰富的C库。
  7. 无继承层次的轻量级面向对象编程范式,GO中的接口与实现之间完全是非侵入式的。在Go中只有类型嵌入而没有类型继承。这避免了很多与继承相关的复杂问题,也使类型层次简化了。
  8. 标准库丰富,极适合开发服务端程序和Web程序。

Go适合用来做什么

  1. 服务器编程,以前你如果使用C或者C++做的那些事情,用Go来做很合适,例如处理日志、数据打包、虚拟机处理、文件系统等。
  2. 分布式系统,数据库代理器等
  3. 网络编程,这一块目前应用最广,包括Web应用、API应用、下载应用
  4. 内存数据库,前一段时间google开发的groupcache,couchbase的部分组建
  5. 云平台,目前国外很多云平台在采用Go开发,CloudFoundy的部分组建,前VMare的技术总监自己出来搞的apcera云平台。
  6. Android程序开发,虽然现在才刚刚开始,不过既然GO和Android都属于Google的,那么为什么不让GO去开发Android的程序哪?

    本段文章大部分引自 作者:asta谢 链接:http://www.zhihu.com/question/21409296/answer/18184584 来源:知乎

Go语言之父Rob Pike自己整理的2012年6月在旧金山给Go SF的演讲稿,演讲的题目是《Less is exponentially more(大道至简)》,这里面提到的Go语言的特性

  • 规范的语法(不需要符号表来解析)、
  • 垃圾回收(独有)
  • 无头文件
  • 明确的依赖
  • 无循环依赖
  • 常量只能是数字(现在情况已经变了:常量可以是数值类型(包括整型、 浮点型和复数类型)、布尔类型、字符串类型等。)
  • int和int32是两种类型
  • 字母大小写设置可见性(letter case sets visibility)
  • 任何类型(type)都有方法(不是类型)
  • 没有子类型继承(不是子类)
  • 包级别初始化以及明确的初始化顺序
  • 文件被编译到一个包里
  • 包package-level globals presented in any order
  • 没有数值类型转换(常量起辅助作用)
  • 接口隐式实现(没有“implement”声明)
  • 嵌入(不会提升到超类)
  • 方法按照函数声明(没有特别的位置要求)
  • 方法即函数
  • 接口只有方法(没有数据)
  • 方法通过名字匹配(而非类型)
  • 没有构造函数和析构函数
  • postincrement(如++i)是状态,不是表达式
  • 没有preincrement(i++)和predecrement
  • 赋值不是表达式
  • 明确赋值和函数调用中的计算顺序(没有“sequence point”)
  • 没有指针运算
  • 内存一直以零值初始化
  • 局部变量取值合法
  • 方法中没有“this”
  • 分段的堆栈
  • 没有静态和其它类型的注释
  • 没有模板
  • 没有异常
  • 内建string、slice和map
  • 数组边界检查

原文如下:

  • regular syntax (don't need a symbol table to parse)
  • garbage collection (only)
  • no header files
  • explicit dependencies
  • no circular dependencies
  • constants are just numbers
  • int and int32 are distinct types
  • letter case sets visibility
  • methods for any type (no classes)
  • no subtype inheritance (no subclasses)
  • package-level initialization and well-defined order of initialization
  • files compiled together in a package
  • package-level globals presented in any order
  • no arithmetic conversions (constants help)
  • interfaces are implicit (no "implements" declaration)
  • embedding (no promotion to superclass)
  • methods are declared as functions (no special location)
  • methods are just functions
  • interfaces are just methods (no data)
  • methods match by name only (not by type)
  • no constructors or destructors
  • postincrement and postdecrement are statements, not expressions
  • no preincrement or predecrement
  • assignment is not an expression
  • evaluation order defined in assignment, function call (no "sequence point")
  • no pointer arithmetic
  • memory is always zeroed
  • legal to take address of local variable
  • no "this" in methods
  • segmented stacks
  • no const or other type annotations
  • no templates
  • no exceptions
  • builtin string, slice, map
  • array bounds checking

Go是不是面向对象的语言哪?准确的说,Go 既支持面向对象编程又不是面向对象语言!

是也不是,难道像是薛定谔的猫一样具有不确定性?

其实这个答案是官方的回答,并不是我个人凭空杜撰而来的,如需了解详情可参考 Is Go an object-oriented language?

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 支持封装,却不支持继承,所以严格按照面向对象规范来说, Go 语言不是面向对象的编程语言.

但是,Go 提供的接口是一种非常简单上手且更加通用的方式,虽然和其他主流的编程语言表现形式上略有不同,甚至不能实现多态,但 Go 的接口不仅仅适用于结构体,也可以适用于任何数据类型,这无疑是很灵活的!

争议性比较大的当属继承,由于没有任何关键字支持继承特性,因此是找不到继承的痕迹.虽然的确存在着某些方式可以将类型嵌入到其他类型中以实现子类化,但那却不是真正的继承.

所以说,Go 既支持面向对象的编程风格又不完全是面向对象的编程语言.

如果换个角度看问题的话,正是由于没有继承特性使得Go 相对于面向对象编程语言更加轻量化,不妨想一想继承的特性,子类和父类的关系,单继承还是多继承,访问控制权限等问题吧!