英文:
How to write to already opened FD in golang
问题
我有以下打开的文件描述符(lsof输出):
auth 11780 root 5w FIFO 0,10 0t0 72061824 pipe
我需要在Go语言中向文件描述符5(FIFO)写入内容。在C语言中,可以使用syscall write()函数来实现:
19270 write(5, "*************", 12 <unfinished ...>
提前谢谢!
英文:
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!
答案1
得分: 8
使用os.NewFile
通过文件描述符来“打开”一个已存在的文件:
> func NewFile(fd uintptr, name string) *File
> NewFile函数返回一个具有给定文件描述符和名称的新文件。
file := os.NewFile(5, "pipe")
_, err := file.Write([]byte(`my data`))
if err != nil {
panic(err)
}
英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论