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
+6.9k Golang : Configure Apache and NGINX to access your Go service example
+11.9k Golang : Handle or parse date string with Z suffix(RFC3339) example
+12.1k Golang : Generate Code128 barcode
+7k Golang : Qt splash screen with delay example
+14.2k Golang : Get checkbox or extract multipart form data value example
+3.7k Golang : How to pass data between controllers with JSON Web Token
+15.7k Golang : Find file size(disk usage) with filepath.Walk
+19k Golang : Secure(TLS) connection between server and client
+19.2k Golang : Underscore or snake_case to camel case example
+23.4k Golang : Create PDF file from HTML file
+10.1k Use systeminfo to find out installed Windows Hotfix(s) or updates
+5.5k Android Studio : Hello World example