《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

原文:

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函数,注意函数名首字母大写,代表该函数从包导出
  • 使用双引号表示字符串
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容