获取文件的uid和gid

时间:2019-10-01 07:06:40

标签: linux go stat

在Linux中,stat结构包含文件的UID和GID。

是否可以使用Go(lang)获取文件的相同信息(UID和GID)?

1 个答案:

答案 0 :(得分:2)

我想出了一种合理的方法。

import (
    "syscall"
    "os"
)

info, _ := os.Stat("/path/to/the/file")

var UID int
var GID int
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
    UID = int(stat.Uid)
    GID = int(stat.Gid)
} else {
    // we are not in linux, this won't work anyway in windows, 
    // but maybe you want to log warnings
    UID = os.Getuid()
    GID = os.Getgid()
}