如何在 Go 中获取文件的上次访问日期和时间?
有谁知道如何检查文件访问日期和时间?该函数返回修改后的日期和时间,我需要将访问的日期时间与当前日期和时间进行比较。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有谁知道如何检查文件访问日期和时间?该函数返回修改后的日期和时间,我需要将访问的日期时间与当前日期和时间进行比较。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
您可以使用 os.Stat 来获取 FileInfo 结构体,其中还包含上次访问时间(以及上次修改时间和上次状态更改时间)。
之后,您可以使用 time.Nanoseconds 获取当前时间(也以纳秒为单位) unix 纪元,1970 年 1 月 1 日 00:00:00 UTC)。要获得以纳秒为单位的持续时间,只需减去这两个值:
You can use os.Stat to get a FileInfo struct which also contains the last access time (as well as the last modified and the last status change time).
After that, you can use time.Nanoseconds to get the current time (also in nanoseconds since the unix epoch, January 1, 1970 00:00:00 UTC). To get the duration in nanoseconds, just subtract those two values:
通过将
os.FileInfo
转换为*syscall.Stat_t
:By casting
os.FileInfo
to*syscall.Stat_t
:或者,在 Stat 之后,您也可以执行
此外,您可以在其上使用 Format(),如果您需要它,例如对于网络服务器,
请参阅 https://gist.github.com/alexisrobert/982674
Alternatively, after the Stat you can also do
Also you can use Format() on it, should you need it eg for a webserver
see https://gist.github.com/alexisrobert/982674
对于 Windows
syscall.Win32FileAttributeData
示例
Linux
看到这个 答案
For windows
syscall.Win32FileAttributeData
Example
Linux
see this answer