代码如下,想问下最后一个语句怎么理解?
```
type Domain_list_t map[string]interface{}
var DomainList Domain_list_t
//DomainList.txt 文件存放域名列表,一行一个
func LoadOrCreateDomainList() bool {
buf, err := ioutil.ReadFile("./Domainlist.txt")
if err != nil {
return false
}
DomainList = make(Domain_list_t)
//domain为Domainlist.txt里的一行,假设是第一行是 www.baidu.com
for _, domain := range strings.Split(string(buf), "\n") {
//subs为domain的各级域名组成的切片 {www,baidu,com}
subs := strings.Split(strings.Trim(domain, "\r "), ".")
if len(subs) == 0 || len(domain) == 0 || domain[0] == '#' {
continue
}
top := DomainList
//i=2时
for i := len(subs) - 1; i >= 0; i-- {
//sub[2]="com",top["com"]==nil
if top[subs[i]] == nil {
//初始化top["com"]
top[subs[i]] = make(Domain_list_t)
}
if i == 0 {
top[subs[0]].(Domain_list_t)["_"] = true
}
//这里不明白,为啥 top["com"] 后面还能加点号,为啥 Domain_list_t 要加括号
top = top[subs[i]].(Domain_list_t)
}
}
```
top = top[subs[i]].(Domain_list_t)
最左边的top类型是: Domain_list_t, 因为top := DomainList
也因为DomainList的值类型是interface, interface是不能直接赋值到类型为DomainList的top
所以需要将interface类型断言为DomainList, 然后再赋值给top
#1
更多评论