How can I get a file inode in Go?
I already can print it like this:
file := "/tmp/system.log"
fileinfo, _ := os.Stat(file)
fmt.Println(fileinfo.Sys())
fmt.Println(fileinfo)
Looking at Go implementation it was obvious looking for some stat
method, but I still did not manage to find the structure definition for a Unix system.
How can I get the inode value directly?
Which file/s in the source code define the structure of Sys()
?
You can use a type assertion to get the underlying syscall.Stat_t
from the fileinfo like this
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
file := "/etc/passwd"
fileinfo, _ := os.Stat(file)
fmt.Printf("fileinfo.Sys() = %#v\n", fileinfo.Sys())
fmt.Printf("fileinfo = %#v\n", fileinfo)
stat, ok := fileinfo.Sys().(*syscall.Stat_t)
if !ok {
fmt.Printf("Not a syscall.Stat_t")
return
}
fmt.Printf("stat = %#v\n", stat)
fmt.Printf("stat.Ino = %#v\n", stat.Ino)
}