The Go 1 hash.Hash interface has a Sum method:
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
Sum(b []byte) []byte
Individual packages (such as md5) contain Sum functions with very similar signatures but totally different meaning:
// Sum returns the MD5 checksum of the data.
func Sum(data []byte) [Size]byte
This makes (hash.Hash).Sum confusing, as illustrated in the review for http://golang.org/cl/49030.
I can see two possible improvements.
- We could rename the method to
AppendSum (along the lines of the strconv Append functions). Adding such a method to the implementations (but not the Hash interface itself) would be backward-compatible with Go 1.
// AppendSum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
AppendSum(b []byte) []byte
- We could change
Sum to not accept a parameter and always return a new slice. If we pair that with appropriate inlining and devirtualization optimizations, it could theoretically be as efficient as appending to an existing slice.
// Sum returns a new slice containing the current hash.
// It does not change the underlying hash state.
Sum() []byte
I recommend option (1), because I think it would migrate more smoothly in binaries that mix Go 1 and Go 2.
The Go 1
hash.Hashinterface has aSummethod:Individual packages (such as
md5) containSumfunctions with very similar signatures but totally different meaning:This makes
(hash.Hash).Sumconfusing, as illustrated in the review for http://golang.org/cl/49030.I can see two possible improvements.
AppendSum(along the lines of thestrconvAppendfunctions). Adding such a method to the implementations (but not theHashinterface itself) would be backward-compatible with Go 1.Sumto not accept a parameter and always return a new slice. If we pair that with appropriate inlining and devirtualization optimizations, it could theoretically be as efficient as appending to an existing slice.I recommend option (1), because I think it would migrate more smoothly in binaries that mix Go 1 and Go 2.