Skip to content

Commit 9b7f5e4

Browse files
committed
第二章笔记
1 parent bc2a616 commit 9b7f5e4

10 files changed

Lines changed: 226 additions & 18 deletions

File tree

‎chapter2/README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# README
2+
3+
## 代码目录
4+
5+
```
6+
/-sample 外部目录
7+
/-data
8+
data.json --数据源
9+
/-matachers
10+
rss.go --搜索rss源的匹配器
11+
/-search
12+
default.go --搜索默认匹配起
13+
feed.go --用于读取json数据的文件
14+
match.go --用于支持不同匹配器的接口
15+
search.go --执行搜索的主控制逻辑
16+
main.go -- 程序的入口
17+
rss.xml -- 示例rss文档 RSS匹配器会下载这些RSS文档。 并将这些返回给results通道
18+
```
19+
20+
第二章介绍的的是一个rss 匹配程序内容,介绍从每个代码片段介绍语法规则及现象,介绍众多的使用方法和具体实例。
21+
22+
## 小结
23+
24+
- 每个代码文件都属于一个包,报名应该与代码文件所在文件夹同名
25+
每个文件下面的文件头部都必须明确写 package + 文件夹名字
26+
- 多种初始化方式和声明的变量的方式 如果值没有显式初始化,会默认为零/nil
27+
var 初始化那些是空的变量
28+
:= 初始化赋值 也会有类型推断
29+
- 指针可以在函数或者goroutine间共享数据
30+
例子在search.go 的Run函数中 go func
31+
- 通过启动goroutine和使用通道完成并发和同步
32+
search.go 使用sync.WaitGroup方法
33+
- 内置函数支持Go语言内部数据结构
34+
make方法的使用初始化 引用类型的数据比如
35+
make(map[string]Matcher)
36+
make(chan *Result)
37+
- 标准库包含很多包
38+
- Go接口可以编写通用的代码和框架
39+
接口里面定义方法,单一方法使用er结尾,多个使用相关名字

‎chapter2/sample/matchers/rss.go‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package matchers
2-
2+
//包名
33
import (
44
"encoding/xml"
55
"errors"
66
"fmt"
77
"log"
88
"net/http"
99
"regexp"
10-
10+
//标准库 和search包
1111
"github.com/goinaction/code/chapter2/sample/search"
1212
)
1313

1414
type (
1515
// item defines the fields associated with the item tag
16+
//item 根据item字段的标签,将定义的字段与rss文档字段关联起来
1617
// in the rss document.
1718
item struct {
1819
XMLName xml.Name `xml:"item"`
@@ -26,6 +27,7 @@ type (
2627

2728
// image defines the fields associated with the image tag
2829
// in the rss document.
30+
//根据image字段标签将定义的字段与rss文档字段关联起来
2931
image struct {
3032
XMLName xml.Name `xml:"image"`
3133
URL string `xml:"url"`
@@ -35,6 +37,7 @@ type (
3537

3638
// channel defines the fields associated with the channel tag
3739
// in the rss document.
40+
//根据channel 字段标签,将定义的字段与rss文档的字段关联起来
3841
channel struct {
3942
XMLName xml.Name `xml:"channel"`
4043
Title string `xml:"title"`
@@ -51,24 +54,27 @@ type (
5154
}
5255

5356
// rssDocument defines the fields associated with the rss document.
57+
//rssDocument定义了与rss文档关联的字段
5458
rssDocument struct {
5559
XMLName xml.Name `xml:"rss"`
5660
Channel channel `xml:"channel"`
5761
}
5862
)
5963

6064
// rssMatcher implements the Matcher interface.
65+
// rssMatcher实现了Matcher 接口
6166
type rssMatcher struct{}
6267

63-
// init registers the matcher with the program.
68+
// init registers the matcher with the program. 匹配器注册到程序里
6469
func init() {
6570
var matcher rssMatcher
6671
search.Register("rss", matcher)
6772
}
6873

6974
// Search looks at the document for the specified search term.
7075
func (m rssMatcher) Search(feed *search.Feed, searchTerm string) ([]*search.Result, error) {
71-
var results []*search.Result
76+
var results []*search.Result//声明一个值为nil的切片
77+
//每个切片都指向Result类型值的指针
7278

7379
log.Printf("Search Feed Type[%s] Site[%s] For URI[%s]\n", feed.Type, feed.Name, feed.URI)
7480

@@ -79,18 +85,22 @@ func (m rssMatcher) Search(feed *search.Feed, searchTerm string) ([]*search.Resu
7985
}
8086

8187
for _, channelItem := range document.Channel.Item {
88+
//依次访问内部每一项
8289
// Check the title for the search term.
8390
matched, err := regexp.MatchString(searchTerm, channelItem.Title)
91+
使用regexp包里的MatchString函数对channelItem里的title进行搜索 匹配
8492
if err != nil {
8593
return nil, err
8694
}
8795

8896
// If we found a match save the result.
89-
if matched {
97+
if matched {//如果值为真
98+
//使用append 将结果加倒入results切片
99+
//append内置函数会根据切片需要决定是否增加切片的长度和容量
90100
results = append(results, &search.Result{
91101
Field: "Title",
92102
Content: channelItem.Title,
93-
})
103+
})//加入到切片里的是指向Results类型值的指针,取地址用& 最终将指针存入切片
94104
}
95105

96106
// Check the description for the search term.
@@ -112,29 +122,36 @@ func (m rssMatcher) Search(feed *search.Feed, searchTerm string) ([]*search.Resu
112122
}
113123

114124
// retrieve performs a HTTP Get request for the rss feed and decodes the results.
125+
//get请求rss数据源并解码
126+
//小写名字 没有对外暴露
115127
func (m rssMatcher) retrieve(feed *search.Feed) (*rssDocument, error) {
116128
if feed.URI == "" {
117129
return nil, errors.New("No rss feed uri provided")
118130
}
119131

120132
// Retrieve the rss feed document from the web.
133+
//从网络获取rss数据源文档
121134
resp, err := http.Get(feed.URI)
122135
if err != nil {
123136
return nil, err
124-
}
137+
}//http.Get返回一个指向Response 类型的指针
125138

126139
// Close the response once we return from the function.
140+
//一旦函数返回 关闭返回的响应链接
127141
defer resp.Body.Close()
128142

129143
// Check the status code for a 200 so we know we have received a
130-
// proper response.
144+
// proper response. 检查状态码200 是不是收到了正确响应
131145
if resp.StatusCode != 200 {
132146
return nil, fmt.Errorf("HTTP Response Error %d\n", resp.StatusCode)
133147
}
134148

135149
// Decode the rss feed document into our struct type.
150+
//将rss数据源文档解码到我们定义的结构类型里 不需要检查错误调用者会做
136151
// We don't need to check for errors, the caller can do this.
137-
var document rssDocument
152+
var document rssDocument //document 类型的rssDocument
153+
//传入局部变量document 地址并返回
138154
err = xml.NewDecoder(resp.Body).Decode(&document)
155+
//NewDecoder 函数返回一个指向Decoder值的指针,之后调用指针的Decode方法
139156
return &document, err
140157
}

‎chapter2/sample/rss.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<rss xmlns:npr="http://www.npr.org/rss/" xmlns:nprml="http://api" <channel>
2+
<title>News</title>
3+
<link>...</link>
4+
<description>...</description>
5+
<language>en</language>
6+
<copyright>Copyright 2014 NPR - For Personal Use
7+
<image>...</image>
8+
<item>
9+
<title>
10+
Putin Says He'll Respect Ukraine Vote But U.S.
11+
</title>
12+
<description>
13+
The White House and State Department have called on the
14+
</description>

‎chapter2/sample/search/default.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ type defaultMatcher struct{} //使用空结构体声明
66
//空结构体 不会分配内存 很适合不需要维护状态的类型
77

88
// init registers the default matcher with the program.
9+
//被引用时 会被编译器发现,保证在main函数前调用
910
func init() {//函数将默认匹配器注册到程序里
1011
var matcher defaultMatcher
12+
//创建一个defaultMatcher类型值 传递给 search.go register函数
1113
Register("default", matcher)
1214
}
15+
//将一个matcher值保存到注册匹配的映射中
16+
//会在main函数之前被完成。
17+
//init 可以完美的完成这种初始化注册的任务
1318

1419
// Search implements the behavior for the default matcher.
1520
//Search 实现默认匹配器的行为
@@ -25,8 +30,25 @@ func (m defaultMatcher) Search(feed *Feed, searchTerm string) ([]*Result, error)
2530
//可以使用defaultMatcher 类型的值或者指向这个类型的指针来调用search方法
2631
//无论是接收者类型的值来调用这个方法,还是使用者类型值的指针来调用这个方法
2732
//编译器都会正确的引用对应的值,作为接收者传递给search方法
33+
34+
35+
2836
//例子 绑定使用
37+
//--->调用方法的例子
38+
//方法声明为使用defaultMatcher类型的值作为接受者
39+
//func (m defaultMatcher) Search(feed *Feed, searchTerm string)
40+
//生命一个指向defaultMatcher类型的指针
41+
//dm := new (defaultMatcher)
42+
//dm.Search(feed, "test")//编译器会揭开dm指针的引用,使用对应的值调用方法
43+
//方法为使用指向defaultMater类型的指针作为接受者
44+
//func (m *defaultMatcher) Search(feed *Feed, searchTerm string)
45+
//var dm defaultMatcher//声明一个 defaultMatcher类型的值
46+
//dm.Search(feed, "test")//编译器会自动生成指针引用dm值,使用指针调用方法
47+
//因大部分方法在调用后都需要维护接受者的状态,所以在一个最佳实践是,将方法的接收者声明为指针
48+
//对于defaultMatcher类型来说使用值作为接收者是因为创建defaultMatcher类型的值不需要分配聂村
49+
//由于defaultMatcher不需要维护状态,所以不需要指针像是接受者
2950

51+
//<----- 👇🤔️没大明白需要测试执行
3052
//--受限制
3153
//声明为使用指向defaultMatcher类型的指针作为接受者
3254
// func (m *defaultMatcher) Search(feed *Feed, searchTerm string)

‎chapter2/sample/search/match.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,30 @@ type Matcher interface {//接口类型
2727
//如何实现? 要实现接口类型里声明的所有方法 =>default.go
2828

2929
// Match is launched as a goroutine for each individual feed to run
30-
// searches concurrently.
30+
//为每个数据源单独启动goroutine来执行这个函数
31+
// searches concurrently. 并发地执行搜索
32+
//Matcher 类型的值作为第一个参数 只有实现了Matcher接口的值或指针才能被接收
3133
func Match(matcher Matcher, feed *Feed, searchTerm string, results chan<- *Result) {
32-
// Perform the search against the specified matcher.
34+
// Perform the search against the specified matcher. 对特定匹配器执行搜索
3335
searchResults, err := matcher.Search(feed, searchTerm)
34-
if err != nil {
36+
// 使用实现了Matcher接口的函数的值或指针进行搜索
37+
if err != nil {//判断是否为错误
3538
log.Println(err)
3639
return
3740
}
3841

39-
// Write the results to the channel.
42+
// Write the results to the channel. 结果写入通道
4043
for _, result := range searchResults {
41-
results <- result
42-
}
44+
results <- result //结果写入通道
45+
}//写入结果 --关闭通道--处理结果串在一起
4346
}
4447

4548
// Display writes results to the console window as they
49+
// 从每个goroutine接收结果后在终端打印
4650
// are received by the individual goroutines.
4751
func Display(results chan *Result) {
4852
// The channel blocks until a result is written to the channel.
53+
//通道会一直被阻塞,直到有结果写入 一旦通道被关闭 for循环就终止
4954
// Once the channel is closed the for loop terminates.
5055
for result := range results {
5156
log.Printf("%s:\n%s\n\n", result.Field, result.Content)

‎chapter2/sample/search/search.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func Run(searchTerm string) {//一个string类型参数
112112
go func() {//匿名函数 使用闭包访问waitGroup 和results变量
113113
// Wait for everything to be processed.
114114
waitGroup.Wait()//递减计数
115-
115+
//直到调用了done方法 wait 就是回放 之后关闭通道 通道关闭 goroutine就不再工作
116116
// Close the channel to signal to the Display
117117
//results 之前定义了 用关闭通道的方式 通知display函数 可以退出了
118118
// function that we can exit the program.
@@ -124,6 +124,7 @@ func Run(searchTerm string) {//一个string类型参数
124124
// return after the final result is displayed.
125125
//调用match的display函数
126126
Display(results)
127+
//关闭通道 display也会返回
127128
}
128129

129130
// Register is called to register a matcher for use by the program.
@@ -134,4 +135,7 @@ func Register(feedType string, matcher Matcher) {
134135

135136
log.Println("Register", feedType, "matcher")
136137
matchers[feedType] = matcher
138+
//将一个matcher值保存到注册匹配的映射中
139+
//会在main函数之前被完成。
140+
//
137141
}

‎chapter3/READMe.md‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# 打包和工具链
2+
3+
- 如何组织Go代码
4+
- 使用Go自带工具命令
5+
- 与其他开发者合作
6+
7+
## 包
8+
9+
设计理念是使用包来封装不同的语义单元功能,更好的复用代码。
10+
11+
- 包名
12+
13+
包名及目录命名使用,简洁,清晰,全小写的名字
14+
- main包
15+
16+
编译时会把包含main名字的包编译为二进制可执行文件。所有的Go语言编译的可执行程序必须有一个叫main的包
17+
main()函数是程序的入口,会使用main包代码所在目录目录名作为二进制可执行文件的文件名
18+
- 命令 command 指可执行的程序。包值语义上可以导入的功能单元
19+
20+
例子 编写$GOPATH/src/test/hello.go
21+
里面的package 写的是main 生成一个二进制文件名字叫test
22+
23+
```
24+
package main
25+
import "fmt"
26+
func main()
27+
fmt.Println("oooo")
28+
```
29+
30+
如果$GOPATH/src/test/hello.go
31+
里面的package 写的是hello并且包含了func main() 是无效的,认为它是一个包不是命令
32+
33+
## 导入
34+
35+
- import关键字
36+
37+
告诉编译器到磁盘的哪里去找要到包
38+
39+
- 一旦找到一个满足import 语句的包就是停止查找,先找安装Go的目录然后才找GOPATH变量里面的目录
40+
- 远程导入
41+
42+
如果是url 工具链使用DVCS获取包。使用go get命令完成,go get将获取任意指定url的包 具有递归属性 扫描源码树
43+
- 命名导入
44+
45+
重名的包使用命名导入
46+
47+
```
48+
package main
49+
import (
50+
"fmt"
51+
myfmt "mylib/fmt"
52+
)
53+
```
54+
- 导入不需要引用这个包的标识符
55+
56+
使用下划线 (_) 空白标识符
57+
58+
## 函数init
59+
60+
每个包可以包含任意个init函数并且都在执行开始时被被调用。
61+
都在main函数之前执行。
62+
main.go 使用postgres.go init函数及空白符使用
63+
64+
## 工具命令
65+
66+
- go run 先构建程序 然后执行
67+
- go build 先编译 然后执行
68+
- go vet 检查错误
69+
- go fmt 格式化代码
70+
- go doc 查看文档 go doc tar …… 展示tar命令
71+
- godoc -http=:6060启动web服务
72+
- 注释风格 /* */ //
73+
- 函数的文档
74+
75+
写直接写在声明之前// 如果是大段文字 可以在工程里
76+
写一个叫做doc.go的文件 使用同样的包名
77+
并把包的介绍使用注释/**/加载报名之前
78+
79+
## 与其他Go合作开发
80+
81+
- 包应该在代码库的根目录中
82+
83+
go get 指定了要导入的全部路径,意味着包名就是代码库的名字且包名应该位于代码库目录结构的根目录
84+
- 包可以非常小
85+
- 对代码指定 go fmt
86+
- 写文档
87+
88+
## 依赖管理
89+
90+
- godep vender gopkg.in 工具
91+
92+
## 第三方依赖库
93+
94+
godep 和vender 第三方(verdoring)导入路径重写特性解决了依赖问题。
95+
思想是把所有的依赖包复制到工程代码目录里,然后使用工程内部的依赖包在所在目录来重写所有的导入路径
96+
- mod
97+
98+
- gb 第三方代码 vendored code 工具链 有些不兼容

0 commit comments

Comments
 (0)