本文为《Go in Action》的第一章读书笔记。
第一章主要是对go语言的一个介绍。
Q: go语言想要解决的问题?
A: 原文:
The Go team went to great lengths to solve the problems facing software developers today. Developers have to make an uncomfortable choice between rapid development and performance when choosing a language for their projects. Languages like C and C++ offer fast execution, whereas languages like Ruby and Python offer rapid development.
简单来说,就是为了提供一门既能快速开发然后性能又好的语言。
有些语言,比如c和c++,性能高但是对开发人员的要求也高,也就不能快速开发。而Ruby和Python对开发人员很友好,但是性能却不怎么样。
Q: go有哪些特点?
A: 原文:
As we explore Go, you’ll find well-planned features and concise syntax. As a language, Go is defined not only by what it includes, but by what it doesn’t include. Go has a concise syntax with few keywords to memorize. Go has a compiler that’s so fast, sometimes you’ll forget it’s running. As a Go developer, you’ll spend significantly less time waiting for your project to build. Because of Go’s built-in concurrency features, your software will scale to use the resources available without forcing you to use special threading libraries. Go uses a simple and effective type system that takes much of the overhead out of object-oriented development and lets you focus on code reuse. Go also has a garbage collector, so you don’t have to manage your own memory.
简单来说,go语言具有精心设计的语言特性以及精简的语法。
其具有以下特点:
- go不仅仅定义了它包含什么,还定义了不包含什么
- 精简的语法及较少的关键字,利于记忆
- 编译器很快,有时你都会忘记还有个编译器在运行
- 构建效率高,时间短
- 自带concurrency特性,不需要去用什么线程库之类的
- 简洁高效的类型系统,摒弃了面向对象开发中的冗余,让开发人员聚焦于代码重用
- 具有垃圾收集器,开发人员不必担心内存问题
开发速度
编译一个大型c或c++应用的时间往往超过等待一杯咖啡的时间。go的编译时间缩短,依靠:
- 一个更聪明的编译器
- 简化的依赖解析算法:编译器仅会查找代码直接引用的库,而不是遍历所有库的依赖
动态语言的效率高,是因为在代码编写和运行之间没有其余步骤,就是说写了代码就可以直接运行,不需要什么编译、链接之类的步骤。但是动态语言不能像静态语言那样保证类型安全。因此,很多时候需要写测试以保证代码的类型正确性。
在go语言中,编译器会检查类型匹配问题。
concurrency
并发。原文:
Go’s concurrency support is one of its strongest features. Goroutines are like threads, but use far less memory and require less code to use. Channels are data structures that let you send typed messages between goroutines with synchronization built in. This facilitates a programming model where you send data between goroutines, rather than letting the goroutines fight to use the same data.
go的并发支持是其最强的几个特性之一。
Goroutines跟线程有些相似,但是使用了更少的内存,也不需要那么多代码去编写。
Channels,是一种数据结构,用于在goroutines之间发送消息,同时内建了同步机制。这种方式构建了一种在goroutines之间发送数据的编程模式,而不是让几个goroutines去竞争使用同样的数据。
简单来说,go的并发模型是消息发送,而不是竞争去使用共享数据。
goroutines
原文:
Goroutines are functions that run concurrently with other goroutines, including the entry point of your program.
In Go, the net/http library has concurrency built in using goroutines. Each inbound request automatically runs on its own goroutine.
Goroutines use less memory than threads and the Go runtime will automatically schedule the execution of goroutines against a set of configured logical processors. Each logical processor is bound to a single OS thread
几点:
- goroutines就是并发运行的函数
- net/http库使用一个goroutine来处理一起请求
- goroutines相对线程使用了更少的内容,go的运行时(runtime)会动态对这些goroutines进行调度,调度到机器的多个逻辑核上运行
- 每一个逻辑核绑定了一个操作系统线程
channels
原文:
Channels help to enforce the pattern that only one goroutine should modify the data at any time
It’s important to note that channels don’t provide data access protection between goroutines. If copies of data are exchanged through a channel, then each goroutine has its own copy and can make any changes to that data safely. When pointers to the data are being exchanged, each goroutine still needs to be synchronized if reads and writes will be performed by the different goroutines.
几点:
- 发送给channel的数据,channel会保证任何时候只有一个goroutine能修改数据
- channel并不能保证数据的读取。如果传给channel的是数据的副本,那么每个goroutine就能安全地对副本进行修改。如果传的是指针,那么每个goroutine就需要自己去保证数据的安全性。
类型系统
原文:
Go developers simply embed types to reuse functionality in a design pattern called composition. Other languages use composition, but it’s often deeply tied to inheritance, which can make it complicated and difficult to use. In Go, types are composed of smaller types, which is in contrast to traditional inheritance-based models.
简单来说,go使用的是组合(composition),类型里面有更小的类型,而不是使用继承。
原文:
In addition Go has a unique interface implementation that allows you to model behavior, rather than model types.
You don’t need to declare that you’re implementing an interface in Go; the compiler does the work of determining whether values of your types satisfy the interfaces you’re using.
GO INTERFACES MODEL SMALL BEHAVIORS
go具有interface这个特性。但不一样的是,开发人员不必声明类型实现了哪个interface,编译器会自己判断。
go的interface的理念就是定义小的行为,注意要小。
内存管理
When you write code with garbage collection in mind, Go’s garbage collection adds little overhead to program execution time, but reduces development effort significantly. Go takes the tedium out of programming and leaves the bean counting to the accountants.
就简单介绍了一下,垃圾回收让开发者不必担心内存回收问题。
最简单的例子
本章最后给了一个最简单的例子:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
然后在什么地方运行呢?书里面说在play.golang.org线上运行。
这里解释一下这段代码:
- 每一行不需要分号
- 第一行定义了包,名字为main
- 使用import引入了标准库fmt,使用的时候就用fmt
- 创建了函数main,使用了fnc关键字
- 调用fmt.Println函数,注意函数名首字母大写,代表该函数从包导出
- 使用双引号表示字符串
有疑问加站长微信联系(非本文作者)