find the creation date of a directory in go

You can use the os.Stat() function to retrieve information about a file or directory. This function returns a FileInfo struct, which contains the file or directory's metadata, including its creation time.

Here's an example code snippet that demonstrates how to retrieve the creation time of a directory in Go:

main.go
package main

import (
    "fmt"
    "os"
)

func main() {
    dirPath := "/path/to/dir"
    
    // Get the FileInfo struct for the directory
    fileInfo, err := os.Stat(dirPath)
    if err != nil {
        fmt.Println(err)
        return
    }
    
    // Extract the creation time from the FileInfo struct
    creationTime := fileInfo.ModTime()
    
    fmt.Println("Directory creation date:", creationTime)
}
414 chars
23 lines

In this example, we use the os.Stat() function to get the FileInfo struct for the directory specified by dirPath. We then extract the creation time from the FileInfo struct using the .ModTime() method. Finally, we print the creation time to the console.

gistlibby LogSnag