本文為《Go in Action》的第一章讀書(shū)筆記。
第一章主要是對(duì)go語(yǔ)言的一個(gè)介紹挣跋。
Q: go語(yǔ)言想要解決的問(wèn)題?
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.
簡(jiǎn)單來(lái)說(shuō)扛伍,就是為了提供一門(mén)既能快速開(kāi)發(fā)然后性能又好的語(yǔ)言孽文。
有些語(yǔ)言矗烛,比如c和c++校赤,性能高但是對(duì)開(kāi)發(fā)人員的要求也高堡牡,也就不能快速開(kāi)發(fā)则涯。而Ruby和Python對(duì)開(kāi)發(fā)人員很友好复局,但是性能卻不怎么樣冲簿。
Q: go有哪些特點(diǎn)?
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.
簡(jiǎn)單來(lái)說(shuō)亿昏,go語(yǔ)言具有精心設(shè)計(jì)的語(yǔ)言特性以及精簡(jiǎn)的語(yǔ)法峦剔。
其具有以下特點(diǎn):
- go不僅僅定義了它包含什么,還定義了不包含什么
- 精簡(jiǎn)的語(yǔ)法及較少的關(guān)鍵字角钩,利于記憶
- 編譯器很快吝沫,有時(shí)你都會(huì)忘記還有個(gè)編譯器在運(yùn)行
- 構(gòu)建效率高,時(shí)間短
- 自帶concurrency特性递礼,不需要去用什么線程庫(kù)之類(lèi)的
- 簡(jiǎn)潔高效的類(lèi)型系統(tǒng)野舶,摒棄了面向?qū)ο箝_(kāi)發(fā)中的冗余,讓開(kāi)發(fā)人員聚焦于代碼重用
- 具有垃圾收集器宰衙,開(kāi)發(fā)人員不必?fù)?dān)心內(nèi)存問(wèn)題
開(kāi)發(fā)速度
編譯一個(gè)大型c或c++應(yīng)用的時(shí)間往往超過(guò)等待一杯咖啡的時(shí)間平道。go的編譯時(shí)間縮短,依靠:
- 一個(gè)更聰明的編譯器
- 簡(jiǎn)化的依賴(lài)解析算法:編譯器僅會(huì)查找代碼直接引用的庫(kù)供炼,而不是遍歷所有庫(kù)的依賴(lài)
動(dòng)態(tài)語(yǔ)言的效率高一屋,是因?yàn)樵诖a編寫(xiě)和運(yùn)行之間沒(méi)有其余步驟,就是說(shuō)寫(xiě)了代碼就可以直接運(yùn)行袋哼,不需要什么編譯冀墨、鏈接之類(lèi)的步驟。但是動(dòng)態(tài)語(yǔ)言不能像靜態(tài)語(yǔ)言那樣保證類(lèi)型安全涛贯。因此诽嘉,很多時(shí)候需要寫(xiě)測(cè)試以保證代碼的類(lèi)型正確性。
在go語(yǔ)言中弟翘,編譯器會(huì)檢查類(lèi)型匹配問(wèn)題虫腋。
concurrency
并發(fā)。原文:
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的并發(fā)支持是其最強(qiáng)的幾個(gè)特性之一稀余。
Goroutines跟線程有些相似悦冀,但是使用了更少的內(nèi)存,也不需要那么多代碼去編寫(xiě)睛琳。
Channels盒蟆,是一種數(shù)據(jù)結(jié)構(gòu),用于在goroutines之間發(fā)送消息师骗,同時(shí)內(nèi)建了同步機(jī)制历等。這種方式構(gòu)建了一種在goroutines之間發(fā)送數(shù)據(jù)的編程模式,而不是讓幾個(gè)goroutines去競(jìng)爭(zhēng)使用同樣的數(shù)據(jù)辟癌。
簡(jiǎn)單來(lái)說(shuō)寒屯,go的并發(fā)模型是消息發(fā)送,而不是競(jìng)爭(zhēng)去使用共享數(shù)據(jù)愿待。
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
幾點(diǎn):
- goroutines就是并發(fā)運(yùn)行的函數(shù)
- net/http庫(kù)使用一個(gè)goroutine來(lái)處理一起請(qǐng)求
- goroutines相對(duì)線程使用了更少的內(nèi)容浩螺,go的運(yùn)行時(shí)(runtime)會(huì)動(dòng)態(tài)對(duì)這些goroutines進(jìn)行調(diào)度靴患,調(diào)度到機(jī)器的多個(gè)邏輯核上運(yùn)行
- 每一個(gè)邏輯核綁定了一個(gè)操作系統(tǒng)線程
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.
幾點(diǎn):
- 發(fā)送給channel的數(shù)據(jù),channel會(huì)保證任何時(shí)候只有一個(gè)goroutine能修改數(shù)據(jù)
- channel并不能保證數(shù)據(jù)的讀取要出。如果傳給channel的是數(shù)據(jù)的副本鸳君,那么每個(gè)goroutine就能安全地對(duì)副本進(jìn)行修改。如果傳的是指針患蹂,那么每個(gè)goroutine就需要自己去保證數(shù)據(jù)的安全性或颊。
類(lèi)型系統(tǒng)
原文:
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.
簡(jiǎn)單來(lái)說(shuō),go使用的是組合(composition)传于,類(lèi)型里面有更小的類(lèi)型囱挑,而不是使用繼承。
原文:
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這個(gè)特性沼溜。但不一樣的是平挑,開(kāi)發(fā)人員不必聲明類(lèi)型實(shí)現(xiàn)了哪個(gè)interface,編譯器會(huì)自己判斷系草。
go的interface的理念就是定義小的行為通熄,注意要小。
內(nèi)存管理
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.
就簡(jiǎn)單介紹了一下找都,垃圾回收讓開(kāi)發(fā)者不必?fù)?dān)心內(nèi)存回收問(wèn)題唇辨。
最簡(jiǎn)單的例子
本章最后給了一個(gè)最簡(jiǎn)單的例子:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
然后在什么地方運(yùn)行呢?書(shū)里面說(shuō)在play.golang.org線上運(yùn)行能耻。
這里解釋一下這段代碼:
- 每一行不需要分號(hào)
- 第一行定義了包赏枚,名字為main
- 使用import引入了標(biāo)準(zhǔn)庫(kù)fmt,使用的時(shí)候就用fmt
- 創(chuàng)建了函數(shù)main晓猛,使用了fnc關(guān)鍵字
- 調(diào)用fmt.Println函數(shù)饿幅,注意函數(shù)名首字母大寫(xiě),代表該函數(shù)從包導(dǎo)出
- 使用雙引號(hào)表示字符串