Standard library / os

os.File.Fd()

Returns the underlying file descriptor or handle; it is borrowed from the File and may become invalid after closure. Prefer SyscallConn for controlled low-level access. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (f *File) Fd() uintptr

Return Value

uintptr

Returns the underlying file descriptor or handle; it is borrowed from the File and may become invalid after closure.

Working Example

Example Snippetgo
package main
import (  "fmt"  "os")
func must(e error) {  if e != nil {    panic(e)  }}func temp() *os.File { f, e := os.CreateTemp(".", "fixture-"); must(e); return f }func main() {  f := temp()  defer os.Remove(f.Name())  defer f.Close()  fmt.Println(f.Fd() != ^uintptr(0))}
Go Reference