Index: src/0dev.org/ioutil/ioutil.go ================================================================== --- src/0dev.org/ioutil/ioutil.go +++ src/0dev.org/ioutil/ioutil.go @@ -18,10 +18,20 @@ // Delegates the call to the WriterFunc while implementing io.Reader. func (r ReaderFunc) Read(b []byte) (int, error) { return r(b) } + +// Reads a single byte from the provided io.Reader +func ReadByte(reader io.Reader) (byte, error) { + var ( + arr [1]byte + err error + ) + _, err = reader.Read(arr[:]) + return arr[0], err +} // Returns a writer that delegates calls to Write(...) while ensuring // that it is never called with less bytes than the specified amount. // // Calls with fewer bytes are buffered while a call with a nil slice Index: src/0dev.org/ioutil/ioutil_test.go ================================================================== --- src/0dev.org/ioutil/ioutil_test.go +++ src/0dev.org/ioutil/ioutil_test.go @@ -45,10 +45,30 @@ func(i, j int) bool { return input[i] == output[j] }}) if len(delta.Added) > 0 || len(delta.Removed) > 0 { t.Error("Differences detected ", delta) } } + +func TestReadByte(t *testing.T) { + var ( + input []byte = []byte{255} + reader *bytes.Reader = bytes.NewReader(input) + ) + + result, err := ReadByte(reader) + if result != input[0] { + t.Error("Unexpected read result from ReadByte", result) + } + if err != nil { + t.Error("Unexpected error from ReadByte", err) + } + + result, err = ReadByte(reader) + if err != io.EOF { + t.Error("Unexpected nil error from ReadByte, read value:", result) + } +} func TestSizedWriter(t *testing.T) { var ( buffer bytes.Buffer writer io.Writer = SizedWriter(&buffer, 4)