Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to already opened FD in golang

Tags:

pipe

go

fifo

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!

like image 998
Zhivko Angelov Avatar asked Jul 03 '17 12:07

Zhivko Angelov


1 Answers

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)
}
like image 186
Tim Cooper Avatar answered Sep 22 '22 17:09

Tim Cooper