DEV Community

Discussion on: Attempting to Learn Go - Listing Files By Extension

shindakun profile image
Steve Layton Author

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 {
        for _, file := range m[value] {

I think I may update the article to make sure it's called out for clarity.

dirkolbrich profile image
Dirk Olbrich

You don't mind?

func plainlist(m map[string][]string, order string) string {
    // 1. get all keys of the map
    var keys []string
    for k := range m {
        keys = append(keys, k)

    // 2. sort by order type
    switch order {
    case "desc", "Desc", "DESC":
        for ext := range m {
        for ext := range m {

    // 3. build a concatenated string
    var list string
    for _, k := range keys {
        list = fmt.Sprintf("%v\n%v", list, m[k])
    return list

use it with:

fmt.Println(plainlist(m, "asc"))
fmt.Println(plainlist(m, "desc"))