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条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路