main
main包具有特殊含义,Go编译器识别main包,并执行底下的main函数,将其编译为二进制可执行文件。
main作为主函数,每个代码文件中的init函数都会在main函数执行前调用。
import
import (
"fmt"
"log"
_ "github.com/goinaction/code/chapter2/sample/matchers"
"github.com/goinaction/code/chapter2/sample/search"
)
下划线代表别名,因为Go不允许代码文件里存在未使用的import,使用_忽视这个规则,可以调用引入库的init方法。
for
支持range,每次迭代返回两个值,第一个是迭代的元素在切片里的索引位置,第二个值是元素值的一个副本。示例:
for _, feed := range feeds {
...
}
for index := 2; index < len(slice); index++ {
fmt.Printf("Index: %d" Value: %d\n", index, slice[index])
}
内置函数len返回长度,cap返回容量
struct
type Feed struct {
Name string `json:"site"`
URI string `json:"link"`
Type string `json:"type"`
}
`引号里的部分被称作标记(tag),描述了json阶码的元数据,将结构类型的字段与json里指定的字段一一对应,同样也可以匹配于别的数据源。
interface
type Matcher interface {
Search(feed *Feed, searchTer