You can use Buffer .
, io.Reader io.Writer (Buffer ) IO. , / ( , , ...) , , , . .
:
:
package mypkg
import (
"bufio"
"io"
"strings"
)
func MyFunction(in io.Reader, out io.Writer) {
rd := bufio.NewReader(in)
str, _ := rd.ReadString('\n')
io.WriteString(out, strings.TrimSuffix(str, "\n")+" was input\n")
}
:
package main
import (
"mypkg"
"os"
)
func main() {
mypkg.MyFunction(os.Stdin, os.Stdout)
}
:
package mypkg
import (
"bytes"
"testing"
)
func TestMyFunction(t *testing.T) {
ibuf := bytes.NewBufferString("hello\n")
obuf := bytes.NewBufferString("")
MyFunction(ibuf, obuf)
if obuf.String() != "hello was input\n" {
t.Fail()
}
}