dongyun3897 2015-02-05 08:22
浏览 561
已采纳

在Go中获取文件inode

How can I get a file inode in Go?

I already can print it like this:

file := "/tmp/system.log"
fileinfo, _ := os.Stat(file)
fmt.Println(fileinfo.Sys())
fmt.Println(fileinfo)

Looking at Go implementation it was obvious looking for some stat method, but I still did not manage to find the structure definition for a Unix system.

How can I get the inode value directly?

Which file/s in the source code define the structure of Sys()?

  • 写回答

2条回答 默认 最新

  • doufunuo4787 2015-02-05 08:42
    关注

    You can use a type assertion to get the underlying syscall.Stat_t from the fileinfo like this

    package main
    
    import (
        "fmt"
        "os"
        "syscall"
    )
    
    func main() {
        file := "/etc/passwd"
        fileinfo, _ := os.Stat(file)
        fmt.Printf("fileinfo.Sys() = %#v
    ", fileinfo.Sys())
        fmt.Printf("fileinfo = %#v
    ", fileinfo)
        stat, ok := fileinfo.Sys().(*syscall.Stat_t)
        if !ok {
            fmt.Printf("Not a syscall.Stat_t")
            return
        }
        fmt.Printf("stat = %#v
    ", stat)
        fmt.Printf("stat.Ino = %#v
    ", stat.Ino)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现