To add and format HTML headings, paragraphs, and text in Go, you can use the html/template
package. Here's an example of how you can create a simple HTML template with headings, paragraphs, and text:
package main
import (
"html/template"
"os"
)
type Page struct {
Title string
Headline string
Content string
}
func main() {
page := Page{
Title: "My Page",
Headline: "Welcome to my page!",
Content: "This is the content of my page.",
}
tmpl, err := template.New("page").Parse(`
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Headline}}</h1>
<p>{{.Content}}</p>
</body>
</html>
`)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, page)
if err != nil {
panic(err)
}
}
In the above example, we define a Page
struct with Title
, Headline
, and Content
fields. We then create a new template and parse the HTML code, using the {{.FieldName}}
syntax to insert values from the Page
struct.
Finally, we execute the template by passing in the Page
data to tmpl.Execute()
, which writes the rendered HTML to os.Stdout
. You can also write the output to a file or an HTTP response writer.
This is just a basic example, but you can expand on it and add more complex HTML structures and formatting using the html/template
package in Go.