11package matchers
2-
2+ //包名
33import (
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
1414type (
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"`
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"`
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 接口
6166type rssMatcher struct {}
6267
63- // init registers the matcher with the program.
68+ // init registers the matcher with the program. 匹配器注册到程序里
6469func init () {
6570 var matcher rssMatcher
6671 search .Register ("rss" , matcher )
6772}
6873
6974// Search looks at the document for the specified search term.
7075func (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+ //小写名字 没有对外暴露
115127func (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}
0 commit comments