Nic*_*ter 2 webserver templates http go
我正在尝试编译以下代码。我无法成功实现我自己的模板处理程序结构,它会在构建时导致以下错误。
错误:
./main.go:28:46:不能在 http.Handle 的参数中使用 templateHandler 文字(类型 *templateHandler)作为类型 http.Handler:*templateHandler 没有实现 http.Handler(缺少 ServeHTTP 方法)
package main
import (
"html/template"
"log"
"net/http"
"path/filepath"
"sync"
)
// templ represents a single template
type templateHandler struct {
once sync.Once
filename string
templ *template.Template
}
// ServeHTTP handles the HTTP request.
func (t *templateHandler) ServerHTTP(w http.ResponseWriter, r *http.Request) {
t.once.Do(func() {
t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
})
t.templ.Execute(w, nil)
}
func main() {
http.Handle("/", &templateHandler{filename: "chat.html"})
// Start Web Server
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
Run Code Online (Sandbox Code Playgroud)
接口有以下表示。
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
Run Code Online (Sandbox Code Playgroud)
你的名字拼错了。服务器HTTP/服务HTTP。
| 归档时间: |
|
| 查看次数: |
1551 次 |
| 最近记录: |