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 (

  "fmt"
  "os"

)

func main() {

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

  if err != nil {

    panic(err)

  }

  // Get unix file descriptor

  fd := uintptr(f.Fd())

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

  // Get file name

  name := f.Name()

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

Output:

File descriptor: 3

File name: golang.txt

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

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, os

Leave a Reply

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