How to add and use HTML5 video and audio elements in Go web pages?

To add and use HTML5 video and audio elements in Go web pages, you can follow these steps:

  1. Import the necessary packages in your Go code:
import ( "html/template" "net/http" )
  1. Create a struct to hold the data for your web page (optional):
type PageData struct { Title string FilePath string }
  1. Define the handler function for your web page:
func handler(w http.ResponseWriter, r *http.Request) { data := PageData{ Title: "HTML5 Video and Audio Example", FilePath: "path_to_video_or_audio_file", } tmpl, err := template.ParseFiles("template.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = tmpl.Execute(w, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
  1. Create a template HTML file ("template.html") that uses the HTML5 video and audio elements:
<!doctype html> <html> <head> <title>{{.Title}}</title> </head> <body> <h1>{{.Title}}</h1> <!-- HTML5 video element --> <video width="400" controls> <source src="{{.FilePath}}" type="video/mp4"> Your browser does not support HTML5 video. </video> <!-- HTML5 audio element --> <audio controls> <source src="{{.FilePath}}" type="audio/mpeg"> Your browser does not support HTML5 audio. </audio> </body> </html>

Make sure to replace "path_to_video_or_audio_file" with the actual path to your video or audio file. You can also specify multiple source elements within the video and audio elements to provide fallback options for different file types.

  1. Register the handler function and start the web server:
func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
  1. Run your Go program and access the web page at http://localhost:8080. You should see the HTML5 video and audio elements with the specified file playing or available for playback.

Note: Ensure that your web server is configured to serve the video and audio files correctly.