To add images and media to HTML pages served by a Go web server, you can follow these steps:
Create a folder in your Go project directory to store your images and media files. For example, you can create a folder named "static" or "assets". Place your files inside this folder.
Import the "net/http" package in your Go file to handle HTTP requests and responses.
import (
"net/http"
)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
In the example above, any request starting with "/static/" will be handled by the file server.
http.ListenAndServe(":8080", nil)
This will start the server on port 8080, allowing you to access your web page using "http://localhost:8080".
<img src="/static/image.jpg" alt="My Image">
In the example above, the image called "image.jpg" will be displayed on the web page. Since the route is set to "/static/" in the Go code, any file within the "static" folder can be accessed using that route.
By following these steps, you can add images and media to your HTML pages served by a Go web server.