在Golang中发送接收http头 func main(){ http.HandleFunc(“/”,foo) log.Println(“侦听…”) http.listendServe(“:6001”,无) } func foo(w http.ResponseWriter,r*http.Request){ w、 Header().Set(“成功”,“Go Web服务器”) fi:=path.Join(“templates/VastPlayer”、“TempVide_uu.txt”) tmpl,err:=template.ParseFiles(fi) 如果错误!=零{ w、 Header().Set(“Error”,err.Error()) Error(w,err.Error(),http.StatusInternalServerError) } 如果错误:=tmpl.Execute(w,“”);错误!=nil{ w、 Header().Set(“Error”,err.Error()) Error(w,err.Error(),http.StatusInternalServerError) } }

在Golang中发送接收http头 func main(){ http.HandleFunc(“/”,foo) log.Println(“侦听…”) http.listendServe(“:6001”,无) } func foo(w http.ResponseWriter,r*http.Request){ w、 Header().Set(“成功”,“Go Web服务器”) fi:=path.Join(“templates/VastPlayer”、“TempVide_uu.txt”) tmpl,err:=template.ParseFiles(fi) 如果错误!=零{ w、 Header().Set(“Error”,err.Error()) Error(w,err.Error(),http.StatusInternalServerError) } 如果错误:=tmpl.Execute(w,“”);错误!=nil{ w、 Header().Set(“Error”,err.Error()) Error(w,err.Error(),http.StatusInternalServerError) } },go,Go,如果我给出一个valide模板,我得到了“成功”:“一个Go Web服务器”在标题上,但是如果我没有给出现有的tempalte,我得到了502个坏网关,这个在标题上 HTTP/1.1 502 Bad Gateway Server: nginx/1.8.0 Date: Mon, 06 Jul 2015 15:19:31 GMT Content-Type: text/html Content-Length: 574 Connection: keep-alive 我想知道是否有一种方法可以发送我通过

如果我给出一个valide模板,我得到了“成功”:“一个Go Web服务器”在标题上,但是如果我没有给出现有的tempalte,我得到了502个坏网关,这个在标题上

HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Mon, 06 Jul 2015 15:19:31 GMT
Content-Type: text/html
Content-Length: 574
Connection: keep-alive
我想知道是否有一种方法可以发送我通过标题得到的错误,我的意思是

templates/VastPlayer/TempVide_uuz.txt:没有这样的文件或目录

发送

接受

fmt.Println("response Status:", resp.Header.Get("Status"))
fmt.Println("response Body:", resp.Header.Get("Body"))

在每次调用http.Error后添加一个return语句。如果应用程序在出现解析错误后没有返回,则当为
tmpl
使用nil值时,应用程序将死机。
fmt.Println("response Status:", resp.Header.Get("Status"))
fmt.Println("response Body:", resp.Header.Get("Body"))