4 ms·
The global hasher var makes this code unsafe for concurrent use by multiple goroutines.
by sagichmal 7y ago
The global hasher var makes this code unsafe for concurrent use by multiple goroutines.
- voidlogic 7y agoThis code should just call sha1.New(). If for some reason these allocations are a performance issue it is very simple to fix that will a little pooling: package main import ( "crypto/sha1" "hash" "sync" ) func main() { hasher := getHasher() defer poolHasher(hasher) } var hasherPool sync.Pool = sync.Pool{ New: func() interface{} { return sha1.New() }, } func getHasher() hash.Hash { return hasherPool.Get().(hash.Hash) } func poolHasher(hasher hash.Hash) { hasher.Reset() hasherPool.Put(hasher) }
- dchest 7y agoOr just use sha1.Sum https://golang.org/pkg/crypto/sha1/#Sum https://golang.org/pkg/crypto/sha1/#Sum