package main
import (
"html/template"
"net/http"
)
// Data struct for template data
type Data struct {
CheckboxValue bool
}
// Handler function to render the template
func handler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("index.html").Parse(`
<!DOCTYPE html>
<html>
<head>
<title>Checkbox Example</title>
</head>
<body>
<form action="/submit" method="post">
<label for="checkbox">Check me:</label>
<input type="checkbox" id="checkbox" name="checkbox" {{if .CheckboxValue}}checked{{end}}>
<input type="submit" value="Submit">
</form>
</body>
</html>
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := Data{CheckboxValue: true}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}