如何在Go中获取文件的组ID(GID)?

Stat ()返回了一个 FileInfo 对象,该对象有一个 Sys ()方法,但是该方法返回了一个没有方法的 Interface {}。

尽管我能够通过fmt.Printf()来“查看”GID,但我无法以编程方式访问“GID”。

如何在这里检索文件的“GID”?

file_info, _ := os.Stat(abspath)
file_sys := file_info.Sys()
fmt.Printf("File Sys() is: %+v", file_sys)

Prints:

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()是片状的)。

可能相关:Convert interface{} to map in Golang

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.