Hah. You caught me! The sort should have been moved up into the print function(s) at the very least. It's a bad design decision - I put it there at first just for the sake of simplicity and never got around to cleaning it up. It doesn't really matter in a directory of a few files but would really impact performance in a larger directory. Something like the following might be fine, and still pretty simple to follow.
func plainList(m map[string][]string, v []string) {
for _, value := range v {
sort.Strings(m[value])
for _, file := range m[value] {
fmt.Println(file)
}
}
}
I think I may update the article to make sure it's called out for clarity.
funcplainlist(mmap[string][]string,orderstring)string{// 1. get all keys of the mapvarkeys[]stringfork:=rangem{keys=append(keys,k)}// 2. sort by order typeswitchorder{case"desc","Desc","DESC":forext:=rangem{sort.Sort(sort.Reverse(sort.StringSlice(m[ext])))}sort.Sort(sort.Reverse(sort.StringSlice(keys)))default:forext:=rangem{sort.Strings(m[ext])}sort.Strings(keys)}// 3. build a concatenated stringvarliststringfor_,k:=rangekeys{list=fmt.Sprintf("%v\n%v",list,m[k])}returnlist}
Hah. You caught me! The
sort
should have been moved up into the print function(s) at the very least. It's a bad design decision - I put it there at first just for the sake of simplicity and never got around to cleaning it up. It doesn't really matter in a directory of a few files but would really impact performance in a larger directory. Something like the following might be fine, and still pretty simple to follow.I think I may update the article to make sure it's called out for clarity.
You don't mind?
use it with: