在golang中获取文件的uid和gid

问题描述 投票:2回答:1

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

有没有一种方法可以使用Go(lang)获得文件的相同信息(UID和GID)?

linux go stat
1个回答
0
投票

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

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()
}
© www.soinside.com 2019 - 2024. All rights reserved.