How can I write a Go test that writes to stdin?

前端 未结 2 1346
礼貌的吻别
礼貌的吻别 2021-01-13 00:34

Say that I have a simple application that reads lines from stdin and simply echoes it back to stdout. For example:

package main

import (
    \"bufio\"
    \         


        
2条回答
  •  死守一世寂寞
    2021-01-13 00:48

    Instead of doing everything in main with stdin and stdout, you can define a function that takes an io.Reader and an io.Writer as parameters and does whatever you want it to do. main could then call that function and your test function could test that function directly.

提交回复
热议问题