использовать bytes.Buffer и обернуть его хэш в то же время, что-то вроде:
type HashedBuffer struct {
h hash.Hash
b bytes.Buffer
}
func NewHashedBuffer(h hash.Hash) *HashedBuffer {
return &HashedBuffer{h: h}
}
func (h *HashedBuffer) Write(p []byte) (n int, err error) {
n, err = h.b.Write(p)
h.h.Write(p)
return
}
func (h *HashedBuffer) Output(w http.ResponseWriter) {
w.Header().Set("ETag", hex.EncodeToString(h.h.Sum(nil)))
h.b.WriteTo(w)
}
//handler
func Handler(w http.ResponseWriter, r *http.Request) {
hb := NewHashedBuffer(sha256.New())
hb.Write([]byte("stuff"))
hb.Output(w)
}
По состоянию на данный момент, вы не можете установить заголовки прицепа, есть открытая issue об этом.
Там обходной путь, угон соединение (из приведенного выше вопроса):
// TODO: There's no way yet for the server to set trailers
// without hijacking, so do that for now, just to test the client.
// Later, in Go 1.4, it should be be implicit that any mutations
// to w.Header() after the initial write are the trailers to be
// sent, if and only if they were previously declared with
// w.Header().Set("Trailer", ..keys..)
w.(Flusher).Flush()
conn, buf, _ := w.(Hijacker).Hijack()
t := Header{}
t.Set("Server-Trailer-A", "valuea")
t.Set("Server-Trailer-C", "valuec") // skipping B
buf.WriteString("0\r\n") // eof
t.Write(buf)
buf.WriteString("\r\n") // end of trailers
buf.Flush()
conn.Close()
Вы не можете отправить заголовки после того, как что-либо писать в ответ, так что единственный выход либо буфер в памяти или хранить предварительно просчитал хэш. – OneOfOne
@ NelsonSimmons проверяет https://code.google.com/p/go/issues/detail?id=7759, а сейчас заголовки трейлеров не поддерживаются в http.Server. – OneOfOne
@NelsonSimmons запрос и клиент поддерживают заголовки трейлеров, но не отвечают на них. – OneOfOne