《Go|《Go in Action》第一章读书笔记
本文为《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 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.go具有interface这个特性。但不一样的是,开发人员不必声明类型实现了哪个interface,编译器会自己判断。
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的理念就是定义小的行为,注意要小。
内存管理
【《Go|《Go in Action》第一章读书笔记】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 mainimport "fmt"func main() {
fmt.Println("Hello World!")
}
然后在什么地方运行呢?书里面说在play.golang.org线上运行。
这里解释一下这段代码:
- 每一行不需要分号
- 第一行定义了包,名字为main
- 使用import引入了标准库fmt,使用的时候就用fmt
- 创建了函数main,使用了fnc关键字
- 调用fmt.Println函数,注意函数名首字母大写,代表该函数从包导出
- 使用双引号表示字符串
推荐阅读
- 慢慢的美丽
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 《跨界歌手》:亲情永远比爱情更有泪点
- 诗歌:|诗歌: 《让我们举起世界杯,干了!》
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- 人间词话的智慧
- 《一代诗人》37期,生活,江南j,拨动心潭的一泓秋水
- 广角叙述|广角叙述 展众生群像——试析鲁迅《示众》的展示艺术
- 书评——《小行星》