package main
import (
"html/template"
"net/http"
)
type PageVariables struct {
Title string
Description string
}
func main() {
http.HandleFunc("/", HomePage)
http.ListenAndServe(":8080", nil)
}
func HomePage(w http.ResponseWriter, r *http.Request) {
pageVariables := PageVariables{
Title: "Hello, Golang!",
Description: "This is a simple Go web application.",
}
tmpl, err := template.New("index").Parse(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Title}}</h1>
<p>{{.Description}}</p>
</body>
</html>
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, pageVariables)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}