This question already has an answer here:
How can I get a file's group ID (GID) in Go?

(1 个回答)


1年前关闭。




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

有没有办法使用 Go(lang) 获取文件的相同信息(UID 和 GID)?

最佳答案

我想出了一个合理的方法来做到这一点。

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()
}

关于linux - 获取文件的uid和gid,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58179647/

10-13 04:55