doufen3134 2018-11-12 17:07
浏览 149
已采纳

在Go中获取文件夹的更改日期

I want to get the date when a folder was renamed, from terminal it can be done using the stat command, e.g.:

> stat -x folders/folder1
File: "folders/folder1"
Size: 64           FileType: Directory
Mode: (0755/drwxr-xr-x)         Uid: (2006390509/username)  Gid: 
(296108113/EMEA\Domain Users)
Device: 1,4   Inode: 2599274    Links: 2
Access: Mon Nov 12 17:59:57 2018
Modify: Mon Nov 12 14:12:20 2018
Change: Mon Nov 12 17:28:01 2018 

The change date is the last date the folder's metadata was changed, which includes renaming.

Is there any way to get it with Go without using os.exec and parsing the output? os.Stat seems to provide only the last modification date which doesn't change when the folder is renamed.

  • 写回答

2条回答 默认 最新

  • 普通网友 2018-11-12 17:40
    关注

    Change time is not accessible in os.FileInfo but can be get via os.FileInfo.Sys() which stores that data.

    You can get it by

    package main
    
    import (
        "fmt"
        "log"
        "os"
        "syscall"
        "time"
    )
    
    func main() {
        f, err := os.Stat("your/dir")
        if err != nil {
            log.Fatalf("err reading: %v", err)
        }
    
        //access change time saved in os.FileInfo.Sys()
        changeTime := f.Sys().(*syscall.Stat_t).Ctim
    
        fmt.Print(time.Unix(changeTime.Unix()).String())
    }
    

    Ofcourse you need to check if f.Sys() it's proper type, but yeah syscall.Stat_t.Ctim is probably what you wanted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化