doumang20060820 2018-12-24 16:14
浏览 116

将stdin复制到缓冲区

I want to copy a os.Stdin string to a buffer, to check for a user inputted text (e.g. "hibye") and put an if statement against it.

My current code just handles simple stdin stdiout copy operations (no buffer):

func interact(c net.Conn) {
    // Read from Reader and write to Writer until EOF()
    copy := func(r io.ReadCloser, w io.WriteCloser) {
        defer func() {
            r.Close()
            w.Close()
        }()
        n, err := io.Copy(w, r)
        if err != nil {
            log.Printf("[%s]: ERROR: %s
", c.RemoteAddr(), err)
            log.Println(n)
        }
    }

    go copy(c, os.Stdout)
    go copy(os.Stdin, c)
}

Question: What is the most efficient way to implement a use of a buffer to have control over the passed strings?

bad example (failed attempt):

buf := make([]byte, 1024)
go copy (os.Stdin, []byte(buf))
if buf == "hibye" {
do stuff
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制