find the last modified date of a csv file in go

Here's an example code snippet for finding the last modified date of a CSV file in Go:

main.go
package main

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

func main() {

	// Open the file
	file, err := os.Open("data.csv")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()

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

	// Get the modified time of the file
	modifiedTime := stat.ModTime()

	// Format and print the modified time
	fmt.Println("Last modified:", modifiedTime.Format(time.RFC3339))

	// Parse the CSV file
	reader := csv.NewReader(file)
	records, err := reader.ReadAll()
	if err != nil {
		fmt.Println(err)
		return
	}

	// Print the CSV records
	fmt.Println("CSV records:", records)
}
684 chars
44 lines

In this example, we first open the CSV file and get its file information using the os package. We then extract the modified time of the file from the file information, format it using the time package, and print it to the console.

We also use the encoding/csv package to read the contents of the CSV file, and print the records to the console as well.

related categories

gistlibby LogSnag