Template Error, Code Panics, Error Not Captured

package main

import (
“net/http”
github.com/satori/go.uuid
“html/template”
“fmt”
)

type user struct{
Email string
First string
Last string
}

var tpl *template.Template
var dbSessions = map[string]string{} // dbSessions is associated with a session id and an Email
var dbUsers = map[string]user{} // dbUsers is associated with a Email and a user’s info

func init(){
tpl = template.Must(template.ParseGlob(“templates/*.gohtml”))
}

func main(){
http.HandleFunc("/", index)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.ListenAndServe(":8080", nil)
}

func index (w http.ResponseWriter, req *http.Request){

//LOOK FOR A COOKIE, IF THERE IS NOT ONE ALREADY THERE,
//CREATE A COOKIE

cookie, err := req.Cookie("session")
if err != nil{
	id, _ := uuid.NewV4()
	cookie := &http.Cookie{
		Name: "session",
		Value: id.String(),
		HttpOnly: true,
		Path: "/",
	}
	http.SetCookie(w, cookie)
}

//CREATE A USER VARIABLE

var u user

//CHECK IF THE SESSION ID (OUR COOKIE'S VALUE) IS ALREADY ASSOCIATED WITH AN EMAIL
//IF THE EMAIL DOES IN FACT ALREADY EXIST
//WE PULL OUT THE USER'S INFORMATION

if thisVariableCouldBeAnything, ok := dbSessions[cookie.Value]; ok{
	u = dbUsers[thisVariableCouldBeAnything]
}


//PROCESS THE FORM SUBMISSION AND CREATE A USER WITH OUR FORM VARIABLES
//MAKE THE SESSION ID ASSOCIATED WITH THIS EMAIL
//MAKE THE EMAIL ASSOCIATED WITH THIS USER

if req.Method == http.MethodPost{
	e := req.FormValue("email")
	f := req.FormValue("first")
	l := req.FormValue("last")
	u = user{e, f, l }

	dbSessions[cookie.Value] = e
	dbUsers[e] = u
}

//EXECUTE OUR TEMPLATE WITH THE USER AS OUR DATA

tpl.ExecuteTemplate(w, "index.gohtml", u)
fmt.Println(u)

}

gurantee you’re passing in and accessing data in your template that doesn’t exist. ExecuteTemplate returns an error you should check it.

hmmm interesting here’s what my template looks like…

 <!doctype html>
INDEX PAGE


{{if .First}}
Email: {{.Email}}

First: {{.First}}

Last: {{.Last}}

{{end}}

I tried checking my error and im still getting the same result :confused:

Reading your code, if you have as your first interaction a get request to the server your user object will not have been created. This would mean that all of your referenced template fields are empty data.

You could solve that by splitting the functionality for creating a user into another handler that only does POST requests, and by checking that there is data in the user object before you pass it into the template.

that worked BEAUTIFULLY! you’re the best thanks so much.

Here’s the final code for the main.go file (didn’t have to make any changes to index.gohtml)…

package main

import (
“net/http”
github.com/satori/go.uuid
“html/template”
“fmt”
)

type user struct{
Email string
First string
Last string
}

var tpl *template.Template
var dbSessions = map[string]string{} // dbSessions is associated with a session id and an Email
var dbUsers = map[string]user{} // dbUsers is associated with a Email and a user’s info

func init(){
tpl = template.Must(template.ParseGlob(“templates/*”))
}

func main(){
http.HandleFunc("/", index)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.ListenAndServe(":8080", nil)
}

func index (w http.ResponseWriter, req *http.Request){

cookie, err := req.Cookie(“session”)
if err != nil{
id, _ := uuid.NewV4()
cookie := &http.Cookie{
Name: “session”,
Value: id.String(),
HttpOnly: true,
Path: “/”,
}
http.SetCookie(w, cookie)
}

u := userData(w, req, cookie)

tpl.ExecuteTemplate(w, “index.gohtml”, u)
fmt.Println(u)
}

func userData (w http.ResponseWriter, req *http.Request, cookie *http.Cookie) user{
var u user

if thisVariableCouldBeAnything, ok := dbSessions[cookie.Value]; ok{
	u = dbUsers[thisVariableCouldBeAnything]
}

if req.Method == http.MethodPost{
	e := req.FormValue("email")
	f := req.FormValue("first")
	l := req.FormValue("last")
	u = user{e, f, l }

	dbSessions[cookie.Value] = e
	dbUsers[e] = u
}
return u

}

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