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
+7k Nginx : How to block user agent ?
+8.1k Golang : Metaprogramming example of wrapping a function
+12.1k Golang : Simple client-server HMAC authentication without SSL example
+32.9k Golang : How to check if a date is within certain range?
+7.4k Golang : How to stop user from directly running an executable file?
+11.4k Golang : Change date format to yyyy-mm-dd
+13.9k Golang : concatenate(combine) strings
+5.3k Golang *File points to a file or directory ?
+14.8k Golang : Basic authentication with .htpasswd file