Wish to extract file properties for files within one go package which can be used in both linux and windows.
Using go, Within windows i can successfully get the properties of a file:
path = Filename, i.e. c:\123.txt
fileinfo, _ := os.Stat(path)
stat := fileinfo.Sys().(*syscall.Win32FileAttributeData)
aTimeSince = time.Since(time.Unix(0, stat.LastAccessTime.Nanoseconds()))
cTimeSince = time.Since(time.Unix(0, stat.CreationTime.Nanoseconds()))
mTimeSince = time.Since(time.Unix(0, stat.LastWriteTime.Nanoseconds()))
Likewise i can also get the same info within Linux:
fileinfo, _ := os.Stat(path)
aTime = fileinfo.Sys().(*syscall.Stat_t).Atim
cTime = fileinfo.Sys().(*syscall.Stat_t).Ctim
mTime = fileinfo.Sys().(*syscall.Stat_t).Mtim
aTimeSince = time.Since(time.Unix(aTime.Sec, aTime.Nsec))
cTimeSince = time.Since(time.Unix(cTime.Sec, cTime.Nsec))
mTimeSince = time.Since(time.Unix(mTime.Sec, mTime.Nsec))
However when i merge the two statements into one go file, Linux rejects the windows code, and windows rejects the Linux code.
Reading the go manual, states i need to specify the $GOOS to the operating system, but unsure how to do this and unable to find it anywhere.
A complete sample of code with the if runtime.GOOS statement:
if runtime.GOOS == "windows" {
fileinfo, _ := os.Stat(path)
stat := fileinfo.Sys().(*syscall.Win32FileAttributeData)
aTimeSince = time.Since(time.Unix(0, stat.LastAccessTime.Nanoseconds()))
cTimeSince = time.Since(time.Unix(0, stat.CreationTime.Nanoseconds()))
mTimeSince = time.Since(time.Unix(0, stat.LastWriteTime.Nanoseconds()))
} else {
fileinfo, _ := os.Stat(path)
aTime = fileinfo.Sys().(*syscall.Stat_t).Atim
cTime = fileinfo.Sys().(*syscall.Stat_t).Ctim
mTime = fileinfo.Sys().(*syscall.Stat_t).Mtim
aTimeSince = time.Since(time.Unix(aTime.Sec, aTime.Nsec))
cTimeSince = time.Since(time.Unix(cTime.Sec, cTime.Nsec))
mTimeSince = time.Since(time.Unix(mTime.Sec, mTime.Nsec))
}
I also realise that fileinfo.ModTime() will give me the modified date\time for both operating systems, but this is not reflected correctly within Linux, so if say a file is moved, the cTime is updated, not the modified time and i need to check for when a last file has been modified,changed, accessed etc.
Any help would be greatly appreciated, thank you.