Package Mainimport ("FMT" "Io/ioutil" "Sort" "time") type info struct {Name stringtime time. Time}type NewList []*infofunc Main () {L, E: = Getfilelist ("./") if E! = nil {fmt. Println (e)}sort. Sort (NewList (L)) //Call the standard library's sort. Sort must first implement Len (), Less (), Swap () three methods. For _, V: = range L {fmt. Println ("File name:", V.name, "Modified time:", V.time.unix ())}}func getfilelist (Path String) ([]*info, error) {L, err: = Ioutil. ReadDir (PATH) if err! = nil {return []*info{}, Err}var list []*infofor _, V: = range L {list = append (list, &info{v.nam E (), 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]}
The go language is structured to sort