diff --git a/doc/Makefile b/doc/Makefile index 9a52b257a3..e834316b9c 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -2,12 +2,6 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../src/Make.inc - -TARG=tmpltohtml -GOFILES=\ - tmpltohtml.go\ - HTML=\ articles/defer_panic_recover.html\ articles/error_handling.html\ @@ -18,7 +12,8 @@ HTML=\ all: tmpltohtml $(HTML) +tmpltohtml: tmpltohtml.go + go build tmpltohtml.go + %.html: %.tmpl tmpltohtml ./makehtml $*.tmpl - -include ../src/Make.cmd diff --git a/doc/go1.html b/doc/go1.html index ae91caac61..95953dc79b 100644 --- a/doc/go1.html +++ b/doc/go1.html @@ -863,6 +863,22 @@ few programs beyond the need to run go fix. This category includes packages that are new in Go 1.

+

The bufio package

+ +

+In Go 1, bufio.NewReaderSize +and +bufio.NewWriterSize +functions no longer return an error for invalid sizes. +If the argument size is too small or invalid, it is adjusted. +

+ +

+Updating: +These functions are rarely used, and code must be updated by hand. +All that is required is to drop the second return value. +

+

The crypto/elliptic package

diff --git a/doc/go1.tmpl b/doc/go1.tmpl index cf93181119..5400198291 100644 --- a/doc/go1.tmpl +++ b/doc/go1.tmpl @@ -767,6 +767,22 @@ few programs beyond the need to run go fix. This category includes packages that are new in Go 1.

+

The bufio package

+ +

+In Go 1, bufio.NewReaderSize +and +bufio.NewWriterSize +functions no longer return an error for invalid sizes. +If the argument size is too small or invalid, it is adjusted. +

+ +

+Updating: +These functions are rarely used, and code must be updated by hand. +All that is required is to drop the second return value. +

+

The crypto/elliptic package

diff --git a/src/pkg/bufio/bufio.go b/src/pkg/bufio/bufio.go index d421152f67..156dddfcf0 100644 --- a/src/pkg/bufio/bufio.go +++ b/src/pkg/bufio/bufio.go @@ -11,7 +11,6 @@ import ( "bytes" "errors" "io" - "strconv" "unicode/utf8" ) @@ -27,13 +26,6 @@ var ( errInternal = errors.New("bufio: internal error") ) -// BufSizeError is the error representing an invalid buffer size. -type BufSizeError int - -func (b BufSizeError) Error() string { - return "bufio: bad buffer size " + strconv.Itoa(int(b)) -} - // Buffered input. // Reader implements buffering for an io.Reader object. @@ -48,35 +40,29 @@ type Reader struct { const minReadBufferSize = 16 -// NewReaderSize creates a new Reader whose buffer has the specified size, -// which must be at least 16 bytes. If the argument io.Reader is already a -// Reader with large enough size, it returns the underlying Reader. -// It returns the Reader and any error. -func NewReaderSize(rd io.Reader, size int) (*Reader, error) { - if size < minReadBufferSize { - return nil, BufSizeError(size) - } +// NewReaderSize returns a new Reader whose buffer has at least the specified +// size. If the argument io.Reader is already a Reader with large enough +// size, it returns the underlying Reader. +func NewReaderSize(rd io.Reader, size int) *Reader { // Is it already a Reader? b, ok := rd.(*Reader) if ok && len(b.buf) >= size { - return b, nil + return b + } + if size < minReadBufferSize { + size = minReadBufferSize + } + return &Reader{ + buf: make([]byte, size), + rd: rd, + lastByte: -1, + lastRuneSize: -1, } - b = new(Reader) - b.buf = make([]byte, size) - b.rd = rd - b.lastByte = -1 - b.lastRuneSize = -1 - return b, nil } // NewReader returns a new Reader whose buffer has the default size. func NewReader(rd io.Reader) *Reader { - b, err := NewReaderSize(rd, defaultBufSize) - if err != nil { - // cannot happen - defaultBufSize is a valid size - panic(err) - } - return b + return NewReaderSize(rd, defaultBufSize) } // fill reads a new chunk into the buffer. @@ -396,33 +382,27 @@ type Writer struct { wr io.Writer } -// NewWriterSize creates a new Writer whose buffer has the specified size, -// which must be greater than zero. If the argument io.Writer is already a -// Writer with large enough size, it returns the underlying Writer. -// It returns the Writer and any error. -func NewWriterSize(wr io.Writer, size int) (*Writer, error) { - if size <= 0 { - return nil, BufSizeError(size) - } +// NewWriterSize returns a new Writer whose buffer has at least the specified +// size. If the argument io.Writer is already a Writer with large enough +// size, it returns the underlying Writer. +func NewWriterSize(wr io.Writer, size int) *Writer { // Is it already a Writer? b, ok := wr.(*Writer) if ok && len(b.buf) >= size { - return b, nil + return b + } + if size <= 0 { + size = defaultBufSize } b = new(Writer) b.buf = make([]byte, size) b.wr = wr - return b, nil + return b } // NewWriter returns a new Writer whose buffer has the default size. func NewWriter(wr io.Writer) *Writer { - b, err := NewWriterSize(wr, defaultBufSize) - if err != nil { - // cannot happen - defaultBufSize is valid size - panic(err) - } - return b + return NewWriterSize(wr, defaultBufSize) } // Flush writes any buffered data to the underlying io.Writer. diff --git a/src/pkg/bufio/bufio_test.go b/src/pkg/bufio/bufio_test.go index a2ee849f58..9aec61ec42 100644 --- a/src/pkg/bufio/bufio_test.go +++ b/src/pkg/bufio/bufio_test.go @@ -161,7 +161,7 @@ func TestReader(t *testing.T) { bufreader := bufreaders[j] bufsize := bufsizes[k] read := readmaker.fn(bytes.NewBufferString(text)) - buf, _ := NewReaderSize(read, bufsize) + buf := NewReaderSize(read, bufsize) s := bufreader.fn(buf) if s != text { t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q", @@ -379,18 +379,14 @@ func TestWriter(t *testing.T) { // and that the data is correct. w.Reset() - buf, e := NewWriterSize(w, bs) + buf := NewWriterSize(w, bs) context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs) - if e != nil { - t.Errorf("%s: NewWriterSize %d: %v", context, bs, e) - continue - } n, e1 := buf.Write(data[0:nwrite]) if e1 != nil || n != nwrite { t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1) continue } - if e = buf.Flush(); e != nil { + if e := buf.Flush(); e != nil { t.Errorf("%s: buf.Flush = %v", context, e) } @@ -447,23 +443,14 @@ func TestWriteErrors(t *testing.T) { func TestNewReaderSizeIdempotent(t *testing.T) { const BufSize = 1000 - b, err := NewReaderSize(bytes.NewBufferString("hello world"), BufSize) - if err != nil { - t.Error("NewReaderSize create fail", err) - } + b := NewReaderSize(bytes.NewBufferString("hello world"), BufSize) // Does it recognize itself? - b1, err2 := NewReaderSize(b, BufSize) - if err2 != nil { - t.Error("NewReaderSize #2 create fail", err2) - } + b1 := NewReaderSize(b, BufSize) if b1 != b { t.Error("NewReaderSize did not detect underlying Reader") } // Does it wrap if existing buffer is too small? - b2, err3 := NewReaderSize(b, 2*BufSize) - if err3 != nil { - t.Error("NewReaderSize #3 create fail", err3) - } + b2 := NewReaderSize(b, 2*BufSize) if b2 == b { t.Error("NewReaderSize did not enlarge buffer") } @@ -471,23 +458,14 @@ func TestNewReaderSizeIdempotent(t *testing.T) { func TestNewWriterSizeIdempotent(t *testing.T) { const BufSize = 1000 - b, err := NewWriterSize(new(bytes.Buffer), BufSize) - if err != nil { - t.Error("NewWriterSize create fail", err) - } + b := NewWriterSize(new(bytes.Buffer), BufSize) // Does it recognize itself? - b1, err2 := NewWriterSize(b, BufSize) - if err2 != nil { - t.Error("NewWriterSize #2 create fail", err2) - } + b1 := NewWriterSize(b, BufSize) if b1 != b { t.Error("NewWriterSize did not detect underlying Writer") } // Does it wrap if existing buffer is too small? - b2, err3 := NewWriterSize(b, 2*BufSize) - if err3 != nil { - t.Error("NewWriterSize #3 create fail", err3) - } + b2 := NewWriterSize(b, 2*BufSize) if b2 == b { t.Error("NewWriterSize did not enlarge buffer") } @@ -496,10 +474,7 @@ func TestNewWriterSizeIdempotent(t *testing.T) { func TestWriteString(t *testing.T) { const BufSize = 8 buf := new(bytes.Buffer) - b, err := NewWriterSize(buf, BufSize) - if err != nil { - t.Error("NewWriterSize create fail", err) - } + b := NewWriterSize(buf, BufSize) b.WriteString("0") // easy b.WriteString("123456") // still easy b.WriteString("7890") // easy after flush @@ -516,10 +491,7 @@ func TestWriteString(t *testing.T) { func TestBufferFull(t *testing.T) { const longString = "And now, hello, world! It is the time for all good men to come to the aid of their party" - buf, err := NewReaderSize(strings.NewReader(longString), minReadBufferSize) - if err != nil { - t.Fatal("NewReaderSize:", err) - } + buf := NewReaderSize(strings.NewReader(longString), minReadBufferSize) line, err := buf.ReadSlice('!') if string(line) != "And now, hello, " || err != ErrBufferFull { t.Errorf("first ReadSlice(,) = %q, %v", line, err) @@ -533,7 +505,7 @@ func TestBufferFull(t *testing.T) { func TestPeek(t *testing.T) { p := make([]byte, 10) // string is 16 (minReadBufferSize) long. - buf, _ := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize) + buf := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize) if s, err := buf.Peek(1); string(s) != "a" || err != nil { t.Fatalf("want %q got %q, err=%v", "a", string(s), err) } @@ -609,7 +581,7 @@ func testReadLine(t *testing.T, input []byte) { for stride := 1; stride < 2; stride++ { done := 0 reader := testReader{input, stride} - l, _ := NewReaderSize(&reader, len(input)+1) + l := NewReaderSize(&reader, len(input)+1) for { line, isPrefix, err := l.ReadLine() if len(line) > 0 && err != nil { @@ -646,7 +618,7 @@ func TestLineTooLong(t *testing.T) { data = append(data, '0'+byte(i%10)) } buf := bytes.NewBuffer(data) - l, _ := NewReaderSize(buf, minReadBufferSize) + l := NewReaderSize(buf, minReadBufferSize) line, isPrefix, err := l.ReadLine() if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { t.Errorf("bad result for first line: got %q want %q %v", line, data[:minReadBufferSize], err) @@ -673,7 +645,7 @@ func TestReadAfterLines(t *testing.T) { inbuf := bytes.NewBuffer([]byte(line1 + "\n" + restData)) outbuf := new(bytes.Buffer) maxLineLength := len(line1) + len(restData)/2 - l, _ := NewReaderSize(inbuf, maxLineLength) + l := NewReaderSize(inbuf, maxLineLength) line, isPrefix, err := l.ReadLine() if isPrefix || err != nil || string(line) != line1 { t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line)) @@ -688,7 +660,7 @@ func TestReadAfterLines(t *testing.T) { } func TestReadEmptyBuffer(t *testing.T) { - l, _ := NewReaderSize(new(bytes.Buffer), minReadBufferSize) + l := NewReaderSize(new(bytes.Buffer), minReadBufferSize) line, isPrefix, err := l.ReadLine() if err != io.EOF { t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) @@ -696,7 +668,7 @@ func TestReadEmptyBuffer(t *testing.T) { } func TestLinesAfterRead(t *testing.T) { - l, _ := NewReaderSize(bytes.NewBuffer([]byte("foo")), minReadBufferSize) + l := NewReaderSize(bytes.NewBuffer([]byte("foo")), minReadBufferSize) _, err := ioutil.ReadAll(l) if err != nil { t.Error(err) @@ -752,10 +724,7 @@ func TestReadLineNewlines(t *testing.T) { } func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) { - b, err := NewReaderSize(strings.NewReader(input), minReadBufferSize) - if err != nil { - t.Fatal(err) - } + b := NewReaderSize(strings.NewReader(input), minReadBufferSize) for i, e := range expect { line, isPrefix, err := b.ReadLine() if bytes.Compare(line, e.line) != 0 { diff --git a/src/pkg/image/png/writer.go b/src/pkg/image/png/writer.go index 641eae1bb8..286a3bc15d 100644 --- a/src/pkg/image/png/writer.go +++ b/src/pkg/image/png/writer.go @@ -408,10 +408,7 @@ func (e *encoder) writeIDATs() { return } var bw *bufio.Writer - bw, e.err = bufio.NewWriterSize(e, 1<<15) - if e.err != nil { - return - } + bw = bufio.NewWriterSize(e, 1<<15) e.err = writeImage(bw, e.m, e.cb) if e.err != nil { return diff --git a/src/pkg/net/http/cgi/host.go b/src/pkg/net/http/cgi/host.go index 73a9b6ea68..d27cc4dc9a 100644 --- a/src/pkg/net/http/cgi/host.go +++ b/src/pkg/net/http/cgi/host.go @@ -217,7 +217,7 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { defer cmd.Wait() defer stdoutRead.Close() - linebody, _ := bufio.NewReaderSize(stdoutRead, 1024) + linebody := bufio.NewReaderSize(stdoutRead, 1024) headers := make(http.Header) statusCode := 0 for { diff --git a/src/pkg/net/http/fcgi/fcgi.go b/src/pkg/net/http/fcgi/fcgi.go index d35aa84d22..06bba0488a 100644 --- a/src/pkg/net/http/fcgi/fcgi.go +++ b/src/pkg/net/http/fcgi/fcgi.go @@ -240,7 +240,7 @@ func (w *bufWriter) Close() error { func newWriter(c *conn, recType recType, reqId uint16) *bufWriter { s := &streamWriter{c: c, recType: recType, reqId: reqId} - w, _ := bufio.NewWriterSize(s, maxWrite) + w := bufio.NewWriterSize(s, maxWrite) return &bufWriter{s, w} }