// main.go
package main
import (
"io"
"log"
"net/http"
)
func main() {
helloHandler := func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Link", "</style.css>; rel=preload; as=style")
w.Header().Add("Link", "</script.js>; rel=preload; as=script")
w.WriteHeader(103)
// do your heavy tasks such as DB or remote APIs calls here
w.WriteHeader(200)
io.WriteString(w, "<!doctype html>\n[... rest of the response body is omitted from the example ...]")
}
http.HandleFunc("/hello", helloHandler)
log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil))
}
How can we perform the same action with fasthttp server?
No this is not possible. Fasthttp was not designed for this. Fasthttp always takes the whole response (headers and body) into memory and only sends it to the client when your hander returns.
In net/http the early hints are solved this way:
How can we perform the same action with fasthttp server?