find the last accessed date of a csv file in go

To find the last accessed date of a csv file in Go, you can make use of the os.Stat function to get the file information, which includes the file's modification time and access time. Here is an example implementation:

main.go
package main

import (
    "encoding/csv"
    "fmt"
    "os"
    "time"
)

func main() {
    file, err := os.Open("example.csv")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    // Get the file information
    fileInfo, err := file.Stat()
    if err != nil {
        fmt.Println(err)
        return
    }

    // Get the last access time of the file
    lastAccessTime := fileInfo.ModTime()

    fmt.Println("Last accessed:", lastAccessTime.Format(time.RFC3339))

    // You can also parse the csv file using the csv package here
    reader := csv.NewReader(file)
    rows, err := reader.ReadAll()
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("CSV rows:", rows)
}
747 chars
40 lines

In this example, we first open the csv file using the os.Open function to obtain a file pointer. We then use the file.Stat method to obtain the file information which includes the last modification and access times of the file. Finally, we print out the last accessed time of the file in RFC3339 format.

Optionally, you can use the encoding/csv package to read the contents of the csv file as well.

related categories

gistlibby LogSnag