find the last modified date of a directory in go

To find the last modified date of a directory in Go, we first need to obtain a handle to the directory using the os package. Then, we can use the Stat method to obtain metadata about the directory, including its modification time. Here's an example:

main.go
package main

import (
    "fmt"
    "os"
)

func main() {
    dirPath := "/path/to/directory"

    fi, err := os.Stat(dirPath)
    if err != nil {
        fmt.Println(err)
        return
    }

    modTime := fi.ModTime()
    fmt.Println("Last modified:", modTime)
}
268 chars
20 lines

In this example, we're using the os.Stat method to get information about the directory located at /path/to/directory. This returns a FileInfo object, which we can use to obtain the modification time using the ModTime method.

Note that the ModTime method returns a time.Time object, which represents the time that the file or directory was last modified. You can use the methods of this object to format the time in any way you need.

gistlibby LogSnag