1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:04:41 -07:00

encoding/binary: add Size, to replace the functionality of the old TotalSize

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5644063
This commit is contained in:
Rob Pike 2012-02-09 11:26:03 +11:00
parent fedc277013
commit 8c4a2ca83b
3 changed files with 12 additions and 2 deletions

View File

@ -931,7 +931,8 @@ No changes will be needed.
<h3 id="encoding_binary">The encoding/binary package</h3>
<p>
In Go 1, the <code>binary.TotalSize</code></a> function is no longer exported.
In Go 1, the <code>binary.TotalSize</code> function is renamed
<a href="/pkg/encoding/binary/#Size"><code>Size</code></a>.
</p>
<p>

View File

@ -835,7 +835,10 @@ No changes will be needed.
<h3 id="encoding_binary">The encoding/binary package</h3>
<p>
In Go 1, the <code>binary.TotalSize</code></a> function is no longer exported.
In Go 1, the <code>binary.TotalSize</code> function has been replaced by
<a href="/pkg/encoding/binary/#Size"><code>Size</code></a>,
which takes an <code>interface{}</code> argument rather than
a <code>reflect.Value</code>.
</p>
<p>

View File

@ -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