From: Rob Pike Date: Thu, 9 Feb 2012 00:26:03 +0000 (+1100) Subject: encoding/binary: add Size, to replace the functionality of the old TotalSize X-Git-Tag: weekly.2012-02-14~197 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8c4a2ca83b5d1ab04361a15d9380f13077b4dda4;p=gostls13.git encoding/binary: add Size, to replace the functionality of the old TotalSize R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5644063 --- diff --git a/doc/go1.html b/doc/go1.html index 28f17f1b5d..8b0b4745e3 100644 --- a/doc/go1.html +++ b/doc/go1.html @@ -931,7 +931,8 @@ No changes will be needed.

The encoding/binary package

-In Go 1, the binary.TotalSize function is no longer exported. +In Go 1, the binary.TotalSize function is renamed +Size.

diff --git a/doc/go1.tmpl b/doc/go1.tmpl index 6375ebcc00..d6803ed189 100644 --- a/doc/go1.tmpl +++ b/doc/go1.tmpl @@ -835,7 +835,10 @@ No changes will be needed.

The encoding/binary package

-In Go 1, the binary.TotalSize function is no longer exported. +In Go 1, the binary.TotalSize function has been replaced by +Size, +which takes an interface{} argument rather than +a reflect.Value.

diff --git a/src/pkg/encoding/binary/binary.go b/src/pkg/encoding/binary/binary.go index 4be83f53bd..7f10d40a11 100644 --- a/src/pkg/encoding/binary/binary.go +++ b/src/pkg/encoding/binary/binary.go @@ -253,6 +253,12 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error { return err } +// Size returns how many bytes Write would generate to encode the value v, assuming +// the Write would succeed. +func Size(v interface{}) int { + return dataSize(reflect.ValueOf(v)) +} + // dataSize returns the number of bytes the actual data represented by v occupies in memory. // For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice // it returns the length of the slice times the element size and does not count the memory