Home > Article > Backend Development > How to Retrieve a File\'s Hard Link Count in Go?
Accessing File's Hard Link Count in Go
Question:
The Go implementation of FileInfo provides extensive information about a file's properties. However, it does not include the number of hard links pointing to the file. How can we retrieve this information using the Go standard library?
Answer:
The number of hard links to a file is stored in the st_nlink field of the stat structure defined in
In this example, we demonstrate how to retrieve the hard link count on a Linux system:
<code class="go">package main import ( "fmt" "os" "syscall" ) func main() { fi, err := os.Stat("filename") if err != nil { fmt.Println(err) return } var nlink uint64 // Retrieve the system-specific data if sys := fi.Sys(); sys != nil { // Cast the system-specific data to a *syscall.Stat_t if stat, ok := sys.(*syscall.Stat_t); ok { nlink = uint64(stat.Nlink) } } fmt.Println(nlink) }</code>
The above is the detailed content of How to Retrieve a File\'s Hard Link Count in Go?. For more information, please follow other related articles on the PHP Chinese website!