Search code examples
htmlhttpgogo-html-template

Html template use on golang


Sorry im beginner and i read golang.docs but didnt understand well. i`ve : index.html:

<html>
<head>
</head>
<body>
<form action type="checkbox" name="test" value="A" {{.checked}}>
<input type="submit" value="save">
</body>
</html>

in main.go if user click save button then check checkbox redirect that page and show checkbox checked


Solution

  • You could send variables in map. For example:

    package main
    
    import (
        "bytes"
        "fmt"
        "text/template"
    )
    
    func main() {
        t, _ := template.New("hi").Parse("Hi {{.name}}")
        var doc bytes.Buffer
        t.Execute(&doc, map[string]string{"name": "Peter"})
        fmt.Println(doc.String()) //Hi Peter
    }