Hi,
I’m trying to display details in this format on my page after getting values from my database.
Name: Danny
Class: GoBeginnerClass
I have a method GetDetails()map[string]string{} that returns a map.
const(
db_path = “root:pass@/trialdb”
//
retrieve_query = “SELECT name, class FROM people”
)
func GetComments()map[string]string{
dbase, err := sql.Open(“mysql”, db_path)
if err != nil{
log.Println("Error @ getDetails dbase: ", err)
}
defer dbase.Close()
query, err := dbase.Query(retrieve_query) if err != nil{ log.Println("Error @ retrieve_query: ", err) }
var name string var class string
students := make(map[string]string)
for query.Next(){
err := query.Scan(&n, &c) if err != nil{ log.Println("Error while scanning name and class:", err) } students[n] = c // saves name and correspondin class in a map } return students
}
- I expect this method to return a map that have names and classes saved in it. I’m also worried the new values will get overwritten as the iteration continues (Not sure tho)
In my main func, I want to serve the details like this.
{{ range $key, $value := .}}
– htag{{ .key}}–htag
–ptag{{ .value}}–ptag
{{ end}}
Note: the --htag and --ptag refers to the html h1 and p tag
So, I save my map in a new variable like this
t, err := template.ParseFiles(“display.html”)
var x map[string]string
x = GetComments()
PageData := &FullValues{x} // FullValues is a struct that has a map field
t.Execute(w, PageData)
There are no compile errors and also no Visile runtime errors. The only thing is that the template is not rendered.
Please if this is not clear enough, kindly ask me.
Thank you Friends…