AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
## 7.8\. 处理不存在的页面 当你访问/view/APageThatDoesntExist的时候会发生什么?程序将会崩溃。因为我们忽略了loadPage返回的错误。请求页不存在的时候,应该重定向客户端到编辑页,这样新的页面将会创建。 ``` func viewHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } renderTemplate(w, "view", p) } ``` 函数http.Redirect添加HTTP状态码http.StatusFound (302)和报头Location到HTTP响应。