find the last modified date of a json file in go

You can find the last modified date of a JSON file in Go by using the os and time packages. Here's an example code snippet that you can use:

main.go
package main

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

func main() {
	file, err := os.Stat("data.json")
	if err != nil {
		fmt.Println(err)
		return
	}

	modTime := file.ModTime()
	fmt.Println("Last modified time:", modTime.Format(time.RFC3339))

	// You can then read the contents of the JSON file using the os package
	// and unmarshal it into a struct using the encoding/json package
}
398 chars
23 lines

In the example code above, we use the os.Stat function to get information about the file. The ModTime() method of the returned FileInfo object gives us the last modified time of the file. Finally, we use the time.RFC3339 format to format the time as a string.

related categories

gistlibby LogSnag