To create and style HTML lists (ordered and unordered) using Go, you can use Go's html/template
package. Here's an example of how you can achieve this:
import (
"html/template"
"os"
)
type ListItem struct {
Text string
}
const listTemplate = `
{{if .IsOrdered}}
<ol>
{{range .Items}}
<li>{{.Text}}</li>
{{end}}
</ol>
{{else}}
<ul>
{{range .Items}}
<li>{{.Text}}</li>
{{end}}
</ul>
{{end}}
`
func main() {
items := []ListItem{
{Text: "Item 1"},
{Text: "Item 2"},
{Text: "Item 3"},
}
data := struct {
Items []ListItem
IsOrdered bool
}{
Items: items,
IsOrdered: true, // Set to false for unordered list
}
tmpl := template.Must(template.New("list-template").Parse(listTemplate))
err := tmpl.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}
In this example, we have defined a template that can render an ordered or unordered list based on the IsOrdered
field. The Items
field contains the list items. The template uses Go's control structures (if
and range
) to loop over the items and generate the list.
You can change the IsOrdered
field to false
if you want to create an unordered list instead.
Running the above code will generate the HTML output for the ordered list:
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
If you set IsOrdered
to false
, it will generate the HTML output for an unordered list:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
You can customize the styling of these lists by adding appropriate CSS classes or inline styles to the generated HTML.