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

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功