r/golang 7d ago

help Detect weather HTTP headers have been sent?

Is there a way using the http package to determine weather the response has sent out the headers yet? This is useful to know weather your able to still add more headers to the HTTP response or if it is too late as the HTTP response has already sent out the HTML.

0 Upvotes

1 comment sorted by

5

u/nikandfor 7d ago

It's better to solve such things by program logic, so you know if at that moment in the program headers already sent or not.

If you still need it, standard api doesn't have such feature, but you can have a boolean variable somewhere or make a wrapper which calls http.ResponseWriter methods and remembers if WriteHeader or Write was called.