I'm new to Golang and having issues while passing data to a template

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>

What’s your issue?

What is the project structure?
Also what the issue you are facing?

I have figured out issue is in where i’m adding one to index in my HTML…i don’t know how it has to be done index is starting from zero and i want to make it strat from 1.

You could pass “names[1:]” into your template instead of “names”.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.