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
+21.8k Golang : How to reverse slice or array elements order
+10.1k Golang : Compare files modify date example
+5.7k Fix yum-complete-transaction error
+32.1k Golang : Validate email address with regular expression
+7.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+29.4k Golang : Login(Authenticate) with Facebook example
+16.8k Golang : Read integer from file into array
+15.2k Golang : How to check if IP address is in range
+13k Golang : Calculate elapsed years or months since a date
+5.4k Javascript : How to loop over and parse JSON data?
+9.7k Golang : Populate slice with sequential integers example