Zhivko Angelov
Zhivko Angelov

Reputation: 139

How to write to already opened FD in golang

I have the following opened FD (lsof output):

auth    11780 root    5w  FIFO               0,10      0t0  72061824 pipe

I need to write something in FD 5 (FIFO) in go. In C it is performed by the syscall write():

19270 write(5, "*************", 12 <unfinished ...>

Thank you in advance!

Upvotes: 7

Views: 4438

Answers (1)

user142162
user142162

Reputation:

Use os.NewFile to "open" an existing file by its file descriptor:

func NewFile(fd uintptr, name string) *File

NewFile returns a new File with the given file descriptor and name.

file := os.NewFile(5, "pipe")
_, err := file.Write([]byte(`my data`))
if err != nil {
    panic(err)
}

Upvotes: 8

Related Questions