如何在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.

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)
}