To add and use HTML5 video and audio elements in Go web pages, you can follow these steps:
import (
"html/template"
"net/http"
)
type PageData struct {
Title string
FilePath string
}
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)
}
}
<!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.
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Note: Ensure that your web server is configured to serve the video and audio files correctly.