find the last modified date of a file in go

To get the last modified date of a file in Go, you can use the os.Stat function which returns a FileInfo variable. The FileInfo variable has a method named ModTime() which returns the last modified time of the file as a time.Time type. Here's an example code snippet:

main.go
package main

import (
	"fmt"
	"log"
	"os"
)

func main() {
	file := "example.txt"

	// Get file information
	fileInfo, err := os.Stat(file)
	if err != nil {
		log.Fatal(err)
	}

	// Get last modified time
	modTime := fileInfo.ModTime()

	fmt.Printf("Last modified time : %v\n", modTime)
}
290 chars
23 lines

In this code, we first get the file information using the os.Stat function which returns a FileInfo variable. We then check for any errors that might occur during the file info retrieval. Finally, we use the ModTime method of the FileInfo variable to get the last modified time of the file.

related categories

gistlibby LogSnag