Help fix a server error (I'm a beginner)


Who is learning or has worked with this book : ? While trying out the first code sample for the web app development, everything looks good when I put (localhost:9090) in my web browser, but when I add “\login” to it, I get the error in my screenshot [].

I also do not understand what the extension .gtpl mean on that tutorial for the UI.

[The error on my server]

Can you please post the code you used to add “/login” route?

.gptl file is nothing but a template file and can have any extension. be sure there is in proper location and have proper syntax.

Your template is nil, this is usually because you didn’t check the error from template.Parse(“templatename”)


Here is the code…

package main

func sayHelloName(writer http.ResponseWriter, request *http.Request) {
fmt.Println(“path”, request.URL.Path)
fmt.Println(“scheme”, request.URL.Scheme)
for k, v := range request.Form{
fmt.Println(“key:”, k)
fmt.Println(“val:”, strings.Join(v,""))
fmt.Fprintf(writer,“Hello Danny”)

func login(writer http.ResponseWriter, request *http.Request){
fmt.Println(“method: “, request.Method)
if request.Method == “GET” {
t, _ := template.ParseFiles(“login.html”)// gets the html files parsed and ready to be served
t.Execute(writer, nil)
//logic part of login
fmt.Println(“username:”, request.Form[“username”])
fmt.Println(“password:”, request.Form[“password”])
func main() {
http.HandleFunc(”/”, sayHelloName)
http.HandleFunc("/login", login)
err := http.ListenAndServe(":9090", nil)
if err != nil{
log.Fatal("ListenAndServe: ", err)

Username: Password:

From the code in Saini’s reply, I have this : t, _ := template.ParseFiles(“login.html”)

How do I check the error?

probably your code should look like:

t, err := template.ParseFiles("login.html")
if err != nil {
	//handle the error here

take a look in the documentation for the syntax.

1 Like

It’s always important to check the error value, let it be general rule for you :wink:

1 Like

Please how do I go about that… Kindly help with a code sample. Can you see my code? Thanks

It’s the same code @geosoft1 paste.

Most functions return an error value, so before using the returned types check if the error is not nil


res, err := http.Get()
If err != nil {
 // Do something here like Logging the error or reporting a failed request

// Use the request to read the body

As mentioned, @geosoft1 already gave you the exact answer.
You replace this line in your code:

with these:

What’s happening:
Remember in golang functions can return more than one value. That is generally used to return / handle errors. Basically (read: oversimplified) errors are “just another value” you return, which is nil if everything was fine and something else if something went wrong. So, as already mentioned, it is always important to check these values, which you handle with the very common snippet of code

if err != nil {
   	//handle the error here

after each function that returns an error value. You’d handle the error either by doing something useful with it and recovering / stopping it from breaking everything or just log.Fatal( err ) which crashes but at least gives you a better idea to what happened.

Now, if you

you’ll see that template.ParseFiles()returns two values:
the first one being a *Template (which you use for your site in your example)
and the second one being an error value (which you simply dismissed by using _).

I recommend you reading this and the Go Tour on errors.
Keep in mind I’m not a veteran on this, and this is just what I’ve learned as being good practice so far, so if anyone corrects me on this, listen to them, not me.

My two cents would be that you created the login.gtpl which, as mentioned, is just the template to use by template.ParseFiles() but then tell it to look for login.html which obviously then wouldn’t exist and since you don’t check for an error there it tries anyway and crashes.


Thanks man… I got it working.

1 Like

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