我想将日志(log)信息发送到套接字,要实现这一点,我需要先发送capture the os.stdout.我知道我可以使用os.pip重定向os.stdout.但是,有没有一种方法可以直接使用bufio.NewReader或bufio.NewScanner从os.stdout读取内容呢?
func Start() {
//dataChan := make(chan string)
outC := make(chan string, 3)
defer close(outC)
conn, err := net.Dial("tcp", "localhost:9090")
if err != nil {
log.Fatal(err)
}
fmt.Println("first line!")
fmt.Println("second line!")
fmt.Println("third line!")
// write to channel
go func() {
scanner := bufio.NewScanner(os.Stdout)
for scanner.Scan() {
outC <- scanner.Text() + "\n"
err = scanner.Err()
if err != nil {
log.Fatal(err)
}
}
}()
// read from channel and print to connection
go func() {
out := <-outC
for {
conn.Write([]byte(out + "\n"))
}
}()
}