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
+7.6k Golang : Convert(cast) []byte to io.Reader type
+10.1k CodeIgniter : How to check if a session exist in PHP?
+9.5k Golang : How to transmit update file to client by HTTP request example
+8.1k Golang : Play .WAV file from command line
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+5.1k Javascript : Get operating system and browser information
+7.7k Golang : Gonum standard normal random numbers example
+4.2k Javascript : Change page title to get viewer attention
+32k Golang : Converting a negative number to positive number
+7.9k Golang : Take screen shot of browser with JQuery example
+17.4k Unmarshal/Load CSV record into struct in Go