title: How to format a duration
body: <p>I have the following code which I can't run on play because I use the gin framework and filewalk.</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "os"
      "path/filepath"
      "time"
      "regexp"
      "github.com/gin-gonic/gin"
      "sort"
      "strings"
      "github.com/davidscholberg/go-durationfmt"
  )
  
  var filext = regexp.MustCompile(`\.[mM][pP]4|\.[mM]4[vV]|\.jpg|\.[hH]264|\.go`)
  var m map[int]string
  var keys []int
  
  func main() {
  
      if gin.IsDebugging() {
          fmt.Print("This progamm shows only the path and the age of a file in 'STARTDIR'\n")
          fmt.Print("only the following files will be handled '.[mM][pP]4|.[mM]4[vV$|.[hH]264|.go'\n")
          fmt.Print("The git repository: https://gitlab.com/aleks001/show-files-age \n")
      }
  
      if len(os.Getenv("LISTEN_IP_PORT")) == 0 {
          fmt.Print("I need a ip and port on which I should listen.\n")
          os.Exit(1)
      }
      router := gin.Default()
  
      gin.DisableConsoleColor()
  
      router.GET("/videoinfo",getInfo)
  
      router.Run(os.Getenv("LISTEN_IP_PORT"))
  }
  
  func getInfo(c *gin.Context)  {
  
      loc, _ := time.LoadLocation("Europe/Vienna")
      var startdir = ""
  
      if os.Getenv("STARTDIR") != "" {
          startdir = os.Getenv("STARTDIR")
      } else if c.GetHeader("STARTDIR") != "" {
          startdir = c.GetHeader("STARTDIR")
      } else {
          c.String(404,"Startdir not found &lt;br&gt;\n")
          return
      }
  
      m = make(map[int]string)
      keys = nil
  
      filepath.Walk(startdir,walkpath)
  
      for k := range m {
          keys = append(keys, k)
      }
      sort.Ints(keys)
  
      for _, k := range keys {
          t := time.Date(time.Now().Year(),time.Now().Month(),time.Now().Day(),time.Now().Hour(),k,time.Now().Second(),time.Now().Nanosecond(),loc)
          durStr, err := durationfmt.Format(time.Since(t), "%h:%m")
  
          if err != nil {
              fmt.Println(err)
          } else {
              //fmt.Println(durStr)
              fmt.Printf("Key: %s Value: %s\n", durStr , m[k])
              c.String(200,"Minutes: %s File: %s\n", durStr, m[k])
          }
      }
  }
  
  func walkpath(path string, f os.FileInfo, err error) error {
  
    if err != nil {
          fmt.Println(err)
      } else {
          if filext.MatchString(path) {
              age := time.Now().Sub(f.ModTime())
              path_new := strings.Replace(path,"/videos/","",1)
              // path_new := strings.Replace(path,"..\\","",1)
              /*
              fmt.Printf("Path: %s, ModTime: %s, Age: %s &lt;br&gt;\n", walker.Path(), walker.Stat().ModTime(), age)
              c.String(200,"Path: %s, ModTime: %s, Age: %s &lt;br&gt;\n", walker.Path(), walker.Stat().ModTime(), age)
              */
              fmt.Printf("Path: %s, Age: %d age minutes %0.2f  &lt;br&gt;\n", path_new, age, age.Minutes())
              m[int(age.Minutes())]=path_new
              //c.String(200,"Path: %s, Age: %0.2f &lt;br&gt;\n", path, age.Minutes())
          }
      //fmt.Printf("%s with %d bytes at motime %s\n", path,f.Size(), f.ModTime())
  }
      return nil
  }
  </code></pre>
  
  <p>What I want to do is a sorted output of files based on <code>filext</code> als filter and the modtime as sort criteria.</p>
  
  <p>I was able to fulfil the most part of the request but the output looks ugly as you can see below.</p>
  
  <p>I have used <a href="https://github.com/davidscholberg/go-durationfmt" rel="noreferrer">https://github.com/davidscholberg/go-durationfmt</a> to format the duration but the output looks ugly or I missus the library.</p>
  
  <pre><code>Minutes: 0:6 File: upload/dir003/file1.m4v
  Minutes: 0:5 File: transfer/dir5/file2.jpg
  Minutes: -5:-48 File: transfer/dir001/file.mp4
  Minutes: -6:-21 File: transfer/03.jpg
  Minutes: -6:-22 File: transfer/02.mp4
  </code></pre>