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
+24.2k Golang : Convert uint value to string type
+10.7k Golang : Concurrency and goroutine example
+3.8k Javascript : Access JSON data example
+5.8k Elasticsearch : Shutdown a local node
+12.5k Golang : reCAPTCHA example
+11.6k Golang : Remove or trim extra comma from CSV
+12.6k Golang : Read XML elements data with xml.CharData example
+5.2k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+12.3k Golang : Linear algebra and matrix calculation example
+18.6k Golang : Measure http.Get() execution time
+6.3k Golang : Array mapping with Interface
+10.9k Golang : Convert(cast) bigint to string