find the last accessed date of a json file in go

You can use the os.Stat() function to get the FileInfo structure that contains the information about the file, including the last accessed time.

main.go
package main

import (
    "fmt"
    "os"
)

func main() {
    fileInfo, err := os.Stat("example.json")

    if err != nil {
        panic(err)
    }

    lastAccessTime := fileInfo.ModTime()

    fmt.Println("Last accessed time:", lastAccessTime)
}
250 chars
19 lines

In the above code, we first use os.Stat() function to get the fileInfo structure. Then we use ModTime() method to get the last accessed time of the file. Finally, we simply print the last accessed time to the console.

related categories

gistlibby LogSnag