dpmopn8542 2015-03-15 13:07
浏览 96
已采纳

从Golang的stdin读取

I'm trying to read from Stdin in Golang as I'm trying to implement a driver for Erlang. I have the following code:

package main

import (
    "fmt"
    "os"
    "bufio"
    "time"
)

func main() {
    go func() { 
        stdout := bufio.NewWriter(os.Stdin) 
        p := []byte{121,100,125,'
'}
        stdout.Write(p)
        }()
    stdin := bufio.NewReader(os.Stdin)
    values := make([]byte,4,4)
    for{
        fmt.Println("b")
        if read_exact(stdin) > 0 {
            stdin.Read(values)
            fmt.Println("a")
            give_func_write(values)
        }else{
            continue
        }
    }
}




func read_exact(r *bufio.Reader) int {
    bits := make([]byte,3,3)
    a,_ := r.Read(bits)
    if a > 0 {
        r.Reset(r)
        return 1
    }
    return -1
}

func give_func_write(a []byte) bool {
    fmt.Println("Yahu")
    return true
}

However it seems that the give_func_write is never reached. I tried to start a goroutine to write to standard input after 2 seconds to test this.

What am I missing here? Also the line r.Reset(r). Is this valid in go? What I tried to achieve is simply restart the reading from the beginning of the file. Is there a better way?

EDIT

After having played around I was able to find that the code is stuck at a,_ := r.Read(bits) in the read_exact function

  • 写回答

1条回答 默认 最新

  • duanqu9292 2015-03-16 23:56
    关注

    I guess that I will need to have a protocol in which I send a to make the input work and at the same time discard it when reading it

    No, you don't. Stdin is line-buffered only if it's bound to terminal. You can run your program prog < /dev/zero or cat file | prog.

    bufio.NewWriter(os.Stdin).Write(p)

    You probably don't want to write to stdin. See "Writing to stdin and reading from stdout" for details.

    Well, it's not particular clear for me what you're trying to achieve. I'm assuming, that you just want to read data from stdin by fixed-size chunks. Use io.ReadFull for this. Or if you want to use buffers, you can use Reader.Peek or Scanner to ensure, that specific number of bytes is available. I've changed your program to demonstrate the usage of io.ReadFull:

    package main
    
    import (
        "fmt"
        "io"
        "time"
    )
    
    func main() {
        input, output := io.Pipe()
    
        go func() {
            defer output.Close()
            for _, m := range []byte("123456") {
                output.Write([]byte{m})
                time.Sleep(time.Second)
            }
        }()
    
        message := make([]byte, 3)
        _, err := io.ReadFull(input, message)
        for err == nil {
            fmt.Println(string(message))
            _, err = io.ReadFull(input, message)
        }
        if err != io.EOF {
            panic(err)
        }
    }
    

    You can easily split it in two programs and test it that way. Just change input to os.Stdin.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)