图像没有在golang上用img显示在html上写

时间:2014-11-19 03:08:46

标签: html go

在html上写时,图像没有显示。

        i can display only image writing on main.go (parsefile).

       but writing on html image didnt show.

我试过html / template。它也没有用。

代码:_base.html

   <!DOCTYPE html>
    <html lang="en">`enter code here`
        <head>
            <title>{{ template "title" . }}</title>
        </head>
        <body>
            <section id="contents">
                {{ template "content" . }}
            </section>
            <footer id="footer">
                My Cool Guestbook 2000 © me forever
            </footer>
        </body>
    </html>

    code:(index.html)

    {{ define "title" }}Guestbook{{ end }}

    {{ define "content" }}
        <h1>Guestbook</h1>

        <p>Hello, and welcome to my guestbook, because it's 1997!</p>

        <ul class="guests">
            <li>...</li>
        </ul>
       <img src="image.png">
    {{ end }}

代码:( main.go some part)

var index = template.Must(template.ParseFiles(
  "templates/_base.html",
  "templates/index.html",
))

func hello(w http.ResponseWriter, req *http.Request) {
  if err := index.Execute(w, nil); err != nil {
      http.Error(w, err.Error(), http.StatusInternalServerError)
  }
}

func main(){etc ...
网络应用工作正常。但是图像没有显示

2 个答案:

答案 0 :(得分:3)

打开您的网络浏览器,看看该图片资源的请求是什么。

它可能会返回404 Not Found,因为它无法找到该文件。

例如在Chrome中可能是:

image resource chrome developer tools

答案 1 :(得分:1)

添加到Agis的答案:

也许您只是为了渲染模板而注册处理程序,而不是提供图像等文件。

您可以使用http.FileServer提供图片。