go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

w http.ResponseWriter cannot be casted to http.Flusher

Open zouxiangxiong002 opened this issue 2 years ago • 3 comments
trafficstars

gozero的Api的Handler层w http.ResponseWriter需要使用flush(),使用强制转换 f, ok := w.(http.Flusher) 没有用 但是使用http原生的http.ResponseWriter进行类型转换却可以转换w.(http.Flusher)

zouxiangxiong002 avatar May 30 '23 06:05 zouxiangxiong002

Which version are you using?

kevwan avatar May 30 '23 16:05 kevwan

My test code is:

package main

import (
	"io"
	"net/http"

	"github.com/zeromicro/go-zero/rest"
)

func main() {
	srv := rest.MustNewServer(rest.RestConf{
		Host: "localhost",
		Port: 8888,
	})
	srv.Use(func(next http.HandlerFunc) http.HandlerFunc {
		return func(w http.ResponseWriter, r *http.Request) {
			io.WriteString(w, "from middleware\n")
			next(w, r)
		}
	})
	srv.AddRoute(rest.Route{
		Method: http.MethodGet,
		Path:   "/hello",
		Handler: func(w http.ResponseWriter, r *http.Request) {
			io.WriteString(w, "hello, world!")
			f := w.(http.Flusher)
			f.Flush()
		},
	})
	defer srv.Stop()
	srv.Start()
}

kevwan avatar May 30 '23 16:05 kevwan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jun 06 '24 01:06 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Sep 05 '24 01:09 github-actions[bot]