I try out example that go office show about http2 push, but i never work

the code goes like following

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        pusher, ok := w.(http.Pusher)
        if ok {
            // Push the resource to the client
            if err := pusher.Push("/styles.css", nil); err != nil {
                log.Printf("Failed to push: %v", err)
            }
            if err := pusher.Push("/app.js", nil); err != nil {
                log.Printf("Failed to push: %v", err)
            }
        }

        // Return HTML response
        w.Header().Set("Content-Type", "text/html; charset=utf-8")
        fmt.Fprintf(w, "<html><head><title>HTTP/2 Push Example</title><link rel=\"stylesheet\" href=\"/styles.css\"><script src=\"/app.js\"></script></head><body><h1>Hello, HTTP/2 Push!</h1></body></html>")
    })

    log.Println("HTTP server listening on :8080")
    log.Fatal(http.ListenAndServeTLS(":8080", "server.crt", "server.key", nil))
}

And the app.js is console.log("Hello http2!")

But when I use chrome to visit https://localhost:8080
what was shown in the app.js file is something like following:
<html><head><title>HTTP/2 Push Example</title><link rel=\"stylesheet\" href=\"/styles.css\"><script src=\"/app.js\"></script></head><body><h1>Hello, HTTP/2 Push!</h1></body></html>

So what’s going on?

And more interesting, if I just use the gin’s example of http2 push, it just work as it should be.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.