Menu Close

Go – How to get unix file descriptor in go

Here, We will learn how to get unix file descriptor in go. We can do it by using fd() method of file in os package in go.

Function prototype:

func (f *File) Fd() uintptr

Return value:

fd() method in file in os package returns 
the integer Unix file descriptor referencing 
the open file.

Example with code:

package main

import (



func main() {

  f, err := os.Open("golang.txt")

  if err != nil {



  // Get unix file descriptor

  fd := uintptr(f.Fd())

  fmt.Println("File descriptor: ", fd)

  // Get file name

  name := f.Name()

  fmt.Println("File name: ", name)


File descriptor: 3

File name: golang.txt

To learn more about golang, Please refer given below link:

Posted in golang, os

Leave a Reply

Your email address will not be published. Required fields are marked *