标签:pop read nil error err art return pac struct
package main import ( "fmt" "io/ioutil" "sort" "time" ) type info struct { Name string Time time.Time } type newlist []*info func main() { l, e := getFilelist("./") if e != nil { fmt.Println(e) } sort.Sort(newlist(l)) //调用标准库的sort.Sort必需要先实现Len(),Less(),Swap() 三个方法. for _, v := range l { fmt.Println("文件名称:", v.Name, "改动时间:", v.Time.Unix()) } } func getFilelist(path string) ([]*info, error) { l, err := ioutil.ReadDir(path) if err != nil { return []*info{}, err } var list []*info for _, v := range l { list = append(list, &info{v.Name(), v.ModTime()}) } return list, nil } func (I newlist) Len() int { return len(I) } func (I newlist) Less(i, j int) bool { return I[i].Time.Unix() < I[j].Time.Unix() } func (I newlist) Swap(i, j int) { I[i], I[j] = I[j], I[i] }
标签:pop read nil error err art return pac struct
原文地址:http://www.cnblogs.com/brucemengbm/p/6958294.html