Golang : Compare files modify date example
Problem:
You're writing a game that will create multiple save files and you want to compare the save game files modify date for sorting purpose and avoid overwriting older files. You also want the game to load a proper file instead of a directory.
Solution:
Below is a code example on how to compare files modify date and perform simple sanity checks.
Here you go!
package main
import (
"fmt"
"os"
"time"
)
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage : %s <file1> <file2> \n", os.Args[0]) // return the program name back to %s
fmt.Printf("Compare <file1> <file2> modify date \n")
os.Exit(0) // graceful exit
}
file1 := os.Args[1]
file2 := os.Args[2]
// perform sanity checks on file1 and file2
file1Info, err := os.Stat(file1)
if err != nil {
fmt.Printf("%s is not a file. \n", file1)
os.Exit(-1)
}
mode1 := file1Info.Mode()
if mode1.IsDir() {
fmt.Printf("%s is a directory, not a file. \n", file1)
os.Exit(-1)
}
file2Info, err := os.Stat(file2)
if err != nil {
fmt.Printf("%s is not a file. \n", file2)
os.Exit(-1)
}
mode2 := file2Info.Mode()
if mode2.IsDir() {
fmt.Printf("%s is a directory, not a file. \n", file2)
os.Exit(-1)
}
// get last modified date of the files
modTime1 := file1Info.ModTime()
modTime2 := file2Info.ModTime()
diff := modTime1.Sub(modTime2)
if diff == (time.Duration(0) * time.Second) {
fmt.Printf("%s has the same last modified timestamp as %s \n", file1, file2)
}
if diff < (time.Duration(0) * time.Second) {
fmt.Printf("%s is older than %s \n", file1, file2)
}
if diff > (time.Duration(0) * time.Second) {
fmt.Printf("%s is younger than %s \n", file1, file2)
}
fmt.Printf("file1 is [%s] with last modified time of [%s]\n", file1, modTime1)
fmt.Printf("file2 is [%s] with last modified time of [%s]\n", file2, modTime2)
}
Sample output:
./cmpdate writer.dat .atom
.atom is a directory, not a file.
./cmpdate writer.dat writexml.go
writer.dat is older than writexml.go
file1 is [writer.dat] with last modified time of [2015-03-25 09:56:27 +0800 SGT]
file2 is [writexml.go] with last modified time of [2015-04-04 16:10:16 +0800 SGT]
./cmpdate writer.dat writer.dat
writer.dat has the same last modified timestamp as writer.dat
file1 is [writer.dat] with last modified time of [2015-03-25 09:56:27 +0800 SGT]
file2 is [writer.dat] with last modified time of [2015-03-25 09:56:27 +0800 SGT]
References:
https://www.socketloop.com/tutorials/golang-check-to-see-if-file-is-a-file-or-directory
https://github.com/SimonHung/LodeRunner_TotalRecall/blob/master/tools/cmpDate.cpp
https://www.socketloop.com/tutorials/golang-comparing-date-or-timestamp
https://www.socketloop.com/tutorials/golang-calculate-time-different
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
+9.3k Golang : Load ASN1 encoded DSA public key PEM file example
+6.2k Golang : How to validate ISBN?
+7.5k Golang : Scan files for certain pattern and rename part of the files
+18.5k Golang : How to make function callback or pass value from function as parameter?
+5.8k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+8.5k Golang : HTTP Routing with Goji example
+19.7k Golang : How to get struct tag and use field name to retrieve data?
+9.4k Golang : List available AWS regions
+6.4k Android Studio : Hello World example
+11.6k Golang : Split strings into command line arguments
+14.8k Golang : Get query string value on a POST request