Created
July 29, 2021 15:28
-
-
Save aceakash/b577caeeeb36b20e499f8c3f70748077 to your computer and use it in GitHub Desktop.
Playing with io - multireader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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