FAQ
To be honest, I was thinking the same thing.

The only people likely to use the new Error() method could just as easily
check for a return value in a new Flush() method.

And, as you say, I can't see any scenarios where adding a return value
would break existing code.


On Tue, Dec 11, 2012 at 5:10 PM, Brad Fitzpatrick (thinking aloud)
Could we "break" the Go 1 API contract and just an error return value to
Flush?

I can't imagine what that would break? Maybe people's mock/testing code,
with a mock interface with a void Flush() function?


On Tue, Dec 11, 2012 at 5:49 AM, wrote:

Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go/


Description:
encoding/csv: add Error method to Writer

Fixed issue 3931

Please review this at https://codereview.appspot.**com/6923049/<https://codereview.appspot.com/6923049/>

Affected files:
M src/pkg/encoding/csv/writer.go
M src/pkg/encoding/csv/writer_**test.go


Index: src/pkg/encoding/csv/writer.go
==============================**==============================**=======
--- a/src/pkg/encoding/csv/writer.**go
+++ b/src/pkg/encoding/csv/writer.**go
@@ -92,10 +92,17 @@
}

// Flush writes any buffered data to the underlying io.Writer.
+// To check if an error occured during the Flush, call Error
func (w *Writer) Flush() {
w.w.Flush()
}

+// Error can be called after Flush for clients who want to be careful
+func (w *Writer) Error() error {
+ _, err := w.w.Write(nil)
+ return err
+}
+
// WriteAll writes multiple CSV records to w using Write and then calls
Flush.
func (w *Writer) WriteAll(records [][]string) (err error) {
for _, record := range records {
Index: src/pkg/encoding/csv/writer_**test.go
==============================**==============================**=======
--- a/src/pkg/encoding/csv/writer_**test.go
+++ b/src/pkg/encoding/csv/writer_**test.go
@@ -6,6 +6,7 @@

import (
"bytes"
+ "errors"
"testing"
)

@@ -42,3 +43,30 @@
}
}
}
+
+type errorWriter struct{}
+
+func (e errorWriter) Write(b []byte) (int, error) {
+ return 0, errors.New("Test")
+}
+
+func TestError(t *testing.T) {
+ b := &bytes.Buffer{}
+ f := NewWriter(b)
+ f.Write([]string{"abc"})
+ f.Flush()
+ err := f.Error()
+
+ if err != nil {
+ t.Errorf("Unexpected error: %s\n", err)
+ }
+
+ f = NewWriter(errorWriter{})
+ f.Write([]string{"abc"})
+ f.Flush()
+ err = f.Error()
+
+ if err == nil {
+ t.Error("Error should not be nil")
+ }
+}

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 11 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 11, '12 at 2:53p
activeDec 11, '12 at 7:37p
posts11
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase