如何将图像插入HTML Golang文件?

package main

import (
    "fmt"
    "net/http"
)

func index_handler(w http.ResponseWriter, r *http.Request) {
    // MAIN SECTION HTML CODE
    fmt.Fprintf(w, "<h1>Whoa, Go is neat!</h1>")
    fmt.Fprintf(w, "<title>Go</title>")
    fmt.Fprintf(w, "<img src='gopher.jpeg' alt='gopher' style='width:235px;height:320px;'>")
}

func about_handler(w http.ResponseWriter, r *http.Request) {
    // ABOUT SECTION HTML CODE
    fmt.Fprintf(w, "<title>Go/about/</title>")
    fmt.Fprintf(w, "Expert web design by -")
}

func main() {
    http.HandleFunc("/", index_handler)
    http.HandleFunc("/about/", about_handler)
    http.ListenAndServe(":8000", nil)
}

In my code I try to put an image on the screen. All this does is use the alt. I have the jpeg file in the same directory as my code. Is there something that i have to add since I am using Go? Thanks for the help.

You just need to use the <img> tag but you have used ' instead of " other than that the code appears fine, however i would consider using CSS instead of html to set image size. its not required but is more common.

Please ignore the above, i was incorrect!

The below code should help, its on here

    func Home(w http.ResponseWriter, r *http.Request) {

          //w.Write([]byte(fmt.Sprintf("Generating QR code
")))

          // generate a random string - preferbly 6 or 8 characters
          randomStr := randStr(6, "alphanum")

         // For Google Authenticator purpose
         // for more details see
         // https://github.com/google/google-authenticator/wiki/Key-Uri-Format
         secret = base32.StdEncoding.EncodeToString([]byte(randomStr))
         //w.Write([]byte(fmt.Sprintf("Secret : %s !
", secret)))

         // authentication link. Remember to replace SocketLoop with yours.
         // for more details see
         // https://github.com/google/google-authenticator/wiki/Key-Uri-Format
         authLink := "otpauth://totp/SocketLoop?secret=" + secret + "&issuer=SocketLoop"

         // Encode authLink to QR codes
         // qr.H = 65% redundant level
         // see https://godoc.org/code.google.com/p/rsc/qr#Level

         code, err := qr.Encode(authLink, qr.H)

         if err != nil {
                 fmt.Println(err)
                 os.Exit(1)
         }

         imgByte := code.PNG()

         // convert byte to image for saving to file
         img, _, _ := image.Decode(bytes.NewReader(imgByte))

         err = imaging.Save(img, "./QRImgGA.png")

         if err != nil {
                 fmt.Println(err)
                 os.Exit(1)
         }

         // in real world application, the QRImgGA.png file should
         // be a temporary file with dynamic name.
         // for this tutorial sake, we keep it as static name.

         w.Write([]byte(fmt.Sprintf("<html><body><h1>QR code for : %s</h1><img src='http://localhost:8080/QRImgGA.png'>", authLink)))
         w.Write([]byte(fmt.Sprintf("<form action='http://localhost:8080/verify' method='post'>Token : <input name='token' id='token'><input type='submit' value='Verify Token'></form></body></html>")))
 }

 func main() {
         http.HandleFunc("/", Home)
         http.HandleFunc("/verify", Verify)

         // this is for displaying the QRImgGA.png from the source directory
         http.Handle("/QRImgGA.png", http.FileServer(http.Dir("./"))) //<---------------- here!

         http.ListenAndServe(":8080", nil)
 }



First I recommend you put your image, css, js, and other assets into a separate folder than where your go app lives, a subfolder is fine as well, just keep them seprate from your go code.

After you've done that you need to tell Go how and from where to serve these files.

Here's an example:

package main

import (
    "fmt"
    "net/http"
)

func index_handler(w http.ResponseWriter, r *http.Request) {
    // MAIN SECTION HTML CODE
    fmt.Fprintf(w, "<h1>Whoa, Go is neat!</h1>")
    fmt.Fprintf(w, "<title>Go</title>")
    fmt.Fprintf(w, "<img src='assets/gopher.jpeg' alt='gopher' style='width:235px;height:320px;'>")
}

func about_handler(w http.ResponseWriter, r *http.Request) {
    // ABOUT SECTION HTML CODE
    fmt.Fprintf(w, "<title>Go/about/</title>")
    fmt.Fprintf(w, "Expert web design by JT Skrivanek")
}

func main() {
    http.HandleFunc("/", index_handler)
    http.HandleFunc("/about/", about_handler)
    http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets"))))
    http.ListenAndServe(":8000", nil)
}

This is assuming that your project structure looks like this:

└── app
    ├── assets
    │   └── gopher.jpeg
    └── main.go

and that you've launched your app from the inside the app folder.

Please also note that you then have to also change the HTML image link to reflect the change. E.g.; instead of <img src='gopher.jpeg' ... you have <img src='assets/gopher.jpeg' ...