net/http 如何自定义404和500

6b79 · · 5677 次点击
参考net.http包里的Error的实现: func Error(w ResponseWriter, error string, code int) { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(code) fmt.Fprintln(w, error) } 先把自定义的页面render成string,调用上面的Error函数应该就可以了吧
#3
更多评论
Unknwon
Only those who attempt the absurd can achieve the impossible.
http://gowalker.org/net/http#Error
#1
Unknwon
Only those who attempt the absurd can achieve the impossible.
好像不太对,你说要自定义页面,貌似你得全部自己接受请求,然后自己判断页面是否存在。。然后输出相关页面,注意不要忘记设置状态码。。w.WriteHeader(code)
#2