Who is learning or has worked with this book : https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/04.1.html ? 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.
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.