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
+13.7k Golang : Check if a file exist or not
+5.7k Golang : Create new color from command line parameters
+10.8k Golang : How to pipe input data to executing child process?
+21.2k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+40.5k Golang : How to check if a string contains another sub-string?
+18k Golang : Example for RSA package functions
+6.4k Golang : Output or print out JSON stream/encoded data
+7.7k How to show different content from website server when AdBlock is detected?
+8.3k Golang : Set or add headers for many or different handlers
+8.6k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+10.9k Golang : Concatenate (combine) buffer data example