编译报错11719 cannot use NewMonitorhttpHandlerFuncfuncw httpResponseWriter r httpRequest … value of type Monitor as type httpServeMux in assignmentpackage mainimport cryptotls flag fmt ioiout
该错误是因为在创建自定义的http.Handler时,使用了NewMonitor函数作为参数,而NewMonitor返回的是*Monitor类型,与http.ServeMux类型不匹配,需要将其转换为http.Handler类型。可以使用类型断言来实现转换,即将NewMonitor(http.HandlerFunc(...))转换为http.Handler(NewMonitor(http.HandlerFunc(...)))。修改如下:
handler := http.Handler(http.DefaultServeMux) if *targetUrl != "" { handler = http.Handler(NewMonitor(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.String() == *targetUrl { // 进行拦截和篡改操作 // ... } http.DefaultServeMux.ServeHTTP(w, r) })))
原文地址: https://www.cveoy.top/t/topic/eDqz 著作权归作者所有。请勿转载和采集!