Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 转到Fprint的多个response.WriteHeader调用_Http_Go - Fatal编程技术网

Http 转到Fprint的多个response.WriteHeader调用

Http 转到Fprint的多个response.WriteHeader调用,http,go,Http,Go,我想先把文字信息打印出来,然后在文字下面,再显示图像。 但我收到http:multiple response.WriteHeader调用错误 如何在一个页面中使用一个hadler提供图像和文本 func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, world!") fp := path.Join("images", "gopher.png") http.ServeFile(w, r,

我想先把文字信息打印出来,然后在文字下面,再显示图像。 但我收到http:multiple response.WriteHeader调用错误

如何在一个页面中使用一个hadler提供图像和文本

func handler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "Hello, world!")
  fp := path.Join("images", "gopher.png")
  http.ServeFile(w, r, fp)
}

func main() {
  http.HandleFunc("/", handler)
  http.ListenAndServe(":3000", nil)
}

您不能写入文本,然后调用ServeFile在文本后输出二进制图片

如果要为图像提供文本,请使用html,设置静态文件处理程序并使用html:

var tmpl = `<!doctype html>
<html>
    <head>
        <title>%s</title>
    </head>
    <body>
    <h1>%s</h1>
    <div><img src="images/%s"></div>
    </body>
</html>
`

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, tmpl, "Hello, world!", "Hello, world!", "gopher.png")
}

func main() {
    http.HandleFunc("/", handler)
    http.Handle("/images/", http.FileServer(http.Dir("images/")))
    http.ListenAndServe(":3000", nil)
}

是否有其他方法可以通过图像提供文本?@EPSILONsdfsdfdsf使用html。这适用于静态文件,但当我提供时,服务器找不到file@EPSILONsdfsdfdsf确实为文件服务器设置了正确的路径吗?您也可以将图像作为数据URI嵌入base64编码中,但我不知道具体如何实现。