Golang : Get file last modified date and time
In this tutorial, we will learn how to get a file last modified date and time with Go. Getting the last modified date and time is fairly straight forward. We will just extract the information via http://golang.org/pkg/os/#FileInfo
We will test the code with a make up binary.file
with the following information
localhost:~ admin$ ls -la binary.file
-rwxr-xr-x 1 admin staff 2206048 Feb 12 22:00 binary.file
and this the Go code to retrieve the last modified date and time information from the binary.file
gettime.go
package main
import (
"fmt"
"os"
)
func main() {
filename := "binary.file"
// get last modified time
file, err := os.Stat(filename)
if err != nil {
fmt.Println(err)
}
modifiedtime := file.ModTime()
fmt.Println("Last modified time : ", modifiedtime)
}
Execute the code above and this will be output :
go run gettime.go
Last modified time : 2014-02-12 22:00:18 +0800 MYT
Hope this tutorial can be useful to you.
See also : Golang : Change a file last modified date and time
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6k Golang : Generate EAN barcode
+6.6k Golang : How to get ECDSA curve and parameters data?
+11.1k Golang : concatenate(combine) strings
+4.4k Golang : Word limiter example
+7.5k Golang : Convert a rune to unicode style string \u
+22.4k Golang : How to verify uploaded file is image or allowed file types
+18.7k Golang : Read directory content with filepath.Walk()
+4.8k Golang : How to convert strange string to JSON with json.MarshalIndent
+9.8k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+4k Golang : Test input string for unicode example
+6k Swift : Convert (cast) Character to Integer?
+4.8k Golang : Ways to recover memory during run time.