Hi gophers,
The documentation for some of the fasthttp functions, such as https://godoc.org/github.com/valyala/fasthttp#RequestHeader.Peek states something like:
func (h *RequestHeader) Peek(key string) []byte
Peek returns header value for the given key.
Returned value is valid until the next call to RequestHeader. Do not store references to returned value. Make copies instead.
I just don't quite grok why this warning exists - surely the []byte array returned by this function is passed by value, not by reference?
评论:
Deathknife:
natbobc:[]byte is a slice rather than an array. As a slice, it acts as a "pointer".
Badu_Ro:They avoid GC by using memory pools and that's the guarantee they provide. As @Deathknife mentioned the underlying data/array isn't a mem copy, only the "shell" which basically includes the len, capacity, and a pointer to the array.
Look at the noCopy struct https://github.com/valyala/fasthttp/blob/master/nocopy.go#L7:6 which points to https://github.com/golang/go/issues/8005#issuecomment-190753527 By the way, that is not a beginner question.
