在Linux中,stat
结构包含文件的UID和GID。
是否可以使用Go(lang)获取文件的相同信息(UID和GID)?
答案 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()
}