如何在Go中获取文件的组ID(GID)?
os.Stat()
返回 FileInfo
对象,该对象具有Sys()
返回没有任何方法的Interface{}
的方法.
os.Stat()
returns a FileInfo
object, which has a Sys()
method that returns an Interface{}
with no methods.
尽管我可以fmt.Printf()
来看到""Gid",但无法以编程方式访问"Gid".
Though I am able to fmt.Printf()
it to "see" the "Gid", I am unable to access "Gid" programmatically.
如何在此处检索文件的"Gid"?
How do I retrieve the "Gid" of a file here?
file_info, _ := os.Stat(abspath)
file_sys := file_info.Sys()
fmt.Printf("File Sys() is: %+v", file_sys)
打印:
File Sys() is: &{Dev:31 Ino:5031364 Nlink:1 Mode:33060 Uid:1616 Gid:31 X__pad0:0 Rdev:0 Size:32 Blksize:32768 Blocks:0 Atim:{Sec:1564005258 Nsec:862700000} Mtim:{Sec:1563993023 Nsec:892256000} Ctim:{Sec:1563993023 Nsec:893251000} X__unused:[0 0 0]}
注意:我不需要便携式解决方案,它只需要在Linux上工作即可(值得注意的是, Sys()
被认为是片状).
Note: I do not need a portable solution, it just has to work on Linux (notable because Sys()
is known to be flaky).
可能相关:转换界面{}以在Golang中进行映射 >
Possibly related: Convert interface{} to map in Golang
reflect
模块显示Sys()
返回的数据类型为*syscall.Stat_t
,因此这似乎可以将文件的Gid作为字符串获取:
The reflect
module showed that the data type for Sys()
's return is *syscall.Stat_t
, so this seems to work to get the Gid of a file as a string:
file_info, _ := os.Stat(abspath)
file_sys := file_info.Sys()
file_gid := fmt.Sprint(file_sys.(*syscall.Stat_t).Gid)
如果有更好的方法,请告诉我.
Please let me know if there is a better way to do this.