Skip to content

Instantly share code, notes, and snippets.

@aceakash
Created July 29, 2021 15:28
Show Gist options
  • Save aceakash/b577caeeeb36b20e499f8c3f70748077 to your computer and use it in GitHub Desktop.
Save aceakash/b577caeeeb36b20e499f8c3f70748077 to your computer and use it in GitHub Desktop.
Playing with io - multireader
package main
import (
"github.com/matryer/is"
"io"
"io/ioutil"
"strings"
"testing"
)
func TestMultireadCanReadFromOneSource(t *testing.T) {
i := is.New(t)
reader1 := strings.NewReader("akash")
out, err := Multiread(reader1)
i.NoErr(err)
i.Equal(string(out), "akash")
}
func TestMultireadCanReadFromTwoSources(t *testing.T) {
i := is.New(t)
reader1 := strings.NewReader("akash")
reader2 := strings.NewReader("einar")
out, err := Multiread(reader1, reader2)
i.NoErr(err)
i.Equal(string(out), "akasheinar")
}
func Multiread(readers ...io.Reader) ([]byte, error) {
var out []byte = make([]byte, 0)
for _, reader := range readers {
bytes, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out = append(out, bytes...)
}
return out, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment