我一直在编写一个Go服务器,它充当Node.js块的子进程。
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
)
// IPC delimiter
const EOT byte = 3
func main() {
// Listen on stdin for messages sent from the parent process.
reader := bufio.NewReader(os.Stdin)
for {
input, err := reader.ReadString(EOT)
if err != nil {
fmt.Printf("sockets: failed to read from stdin: %v", err)
if err == io.EOF {
return
}
continue
}
// Strip EOT bye
input = input[:len(input) - 1]
var payload Payload
if err := json.Unmarshal([]byte(input), &payload); err != nil {
fmt.Printf("sockets: failed to read from stdin: %v", err)
continue
}
}
}但是,像这样使用stdin/stdout会阻止这段代码记录到控制台,因为父进程正在使用stdouts句柄。理想情况下,我会使用文件描述符4来利用Node如何使用它,唯一的问题是我对Windows的细节一无所知。如何在Windows上使用/dev/fd/4 for IPC的等价物(如果有的话)?
PS:如果有更好的方法来处理从stdin读取数据,那也会对我有很大的帮助。
发布于 2016-11-25 18:14:58
你可以试试os.NewFile
f := os.NewFile(4, "my_fd_4")https://stackoverflow.com/questions/40800343
复制相似问题