package main
import (
"fmt"
"log"
"os"
"text/template"
)
var temp *template.Template
func main() {
newFile, err := os.Create("../public/html/index.html")
if err != nil {
fmt.Println("Error Creating File")
}
temp = template.Must(template.ParseFiles("../public/html/home.html"))
fmt.Println(temp)
names := []string{"ram", "anyname", "here", "are", "four"}
err = temp.ExecuteTemplate(newFile, "../public/html/home.html", names)
if err != nil {
log.Fatalln("Error Passing data into file", err)
}
}
and my template looks like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Blog Prototype</title>
<link type="text/css" rel="stylesheet" href="/css/app.css" />
</head>
<body>
{{range $index, $element := .}}
<h1>{{($index+1)}} + {{$element}}</h1>
{{end}}
</body>
</html>