duankeng1911 2015-12-24 11:30
浏览 26

如何在Go中的HTML文件中添加图像

First of all, I created an HTML file using Notepad ++ with this code:

<body>
    <table>
        <tr>
            <td>Jill</td>
            <td>Smith</td>
            <td><img src="test.jpg" border=3 height=100 width=300 /></td>
        </tr>
        <tr>
            <td>Eve</td>
            <td>Jackson</td>
            <td>94</td>
        </tr>
    </table>
</body>

Below is the Go language Code for this :-

// * /
func rootHandler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
        homeHandler(w, r)
    } else {
        log.Printf("rootHandler: Could not forward request for %s any further.", r.RequestURI)

        errNotFound(w, r)
    }
}

I want test.png should be loaded in browser but its not working.

  • 写回答

1条回答 默认 最新

  • dongzhaiqiang6108 2015-12-24 13:05
    关注

    You can add an handler for dealing with function in an img directory, for example.

    Here is one possible way to do it:

    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    func RootHandler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "<h1>Hello</h1><img src='/img/MR.png'/>")
    }
    
    func main() {
        http.HandleFunc("/", RootHandler) // homepage
    
        http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
            http.ServeFile(w, r, r.URL.Path[1:])
        })
    
        http.ListenAndServe(":8080", nil)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)