To generate HTML meta tags for SEO optimization in Go, you can make use of the html/template
package. Here's an example implementation:
import (
"html/template"
"os"
)
type Metadata struct {
Title string
Description string
Keywords []string
}
func GenerateMetaTags(metadata Metadata) string {
t := template.Must(template.New("meta").Parse(`
<meta name="title" content="{{.Title}}">
<meta name="description" content="{{.Description}}">
{{range .Keywords}}
<meta name="keywords" content="{{.}}">
{{end}}
`))
var result strings.Builder
err := t.Execute(&result, metadata)
if err != nil {
// Handle error
return ""
}
return result.String()
}
func main() {
metadata := Metadata{
Title: "Example Page",
Description: "This is an example page.",
Keywords: []string{"example", "page", "SEO"},
}
metaTags := GenerateMetaTags(metadata)
fmt.Println(metaTags)
}
This implementation uses the html/template
package to define a template for the meta tags. The GenerateMetaTags
function takes the metadata as input, executes the template, and returns the generated HTML meta tags as a string. You can customize the template to include any additional meta tags or attributes as per your requirements.