DEV Community

mohaned mashaly
mohaned mashaly

Posted on • Updated on

GoLang Templating

GoLang is a Language created by Google which is widely used by many Companies Like Uber which used in Go in Implementing GeoService which match Riders with Drivers also used by Google Itself (No kidding Google created Go for a Reason), Medium, Twitch, etc.. and very other successful Start-ups are using Go-Lang to code their service even they're re-writing their current service using Go, the Analogy I am going to Talk about GoLang in General before going to Go-Lang Template Package, Google Engineers when creating Go-Lang they were like the Russian Scientist in the labs trying to create the next Perfect Athlete by increasing it's Growth Hormone and muscle mass, etc.. Go-Lang was created by Google to Make their Life Easier like they were thinking about Language Fast Like Java, C++ with Simple Syntax like Python and scalable, Fast, Concurrent the Last Word what makes GoLang Worth it which make Execution of Program Faster using Idea Concurrency where Program Can Execute Different Functions from the Call Stack once at a time by creating Light weight Threads(Go-Routine) which is stored in a dynamic stack and this set GoLang Apart from other Languages Especially Java Because you Can add Million of Threads without size constraint while in java for example you can only add thousands of thread for more about Go-Routine you can check thisLink

After this Introduction with Go-Lang let's Jump to the GoLang Templating System Go-Lang is a bit different from Languages like Python when dealing with development for example when using Django FrameWork you have to Separate the Logic from the Front-end part where you have the View which is the Logic and Model which is the Database and the Template and the Template is separated from both View and Model but here in GoLang you can create the Logic and Front end Part all together but we will not do that with the Examples let's Say We Want to Create Web Page saying that
GoLang is a one of a Language

Let's Write the Logic Side of the WebPage we have to create a Function


package main

import (
"html/template"
"fmt"
"net/http"
)

func HomeHandler(w http.ResponseWriter, r *http.Request){
t,err := template.ParseFiles("Home.html")
if(err == nil){
t.Execute(w,t)// Execute the Template of GoLang
}

func main (){
http.HandleFunc("/Home", HomeHandler) // Handling the Function of HomeHandler which re-direct us to Home
http.ListenAndServe(":8000", nil) // Hosting the WebPage on port 8000
}

}

and Know for the Html Code and Save it in a File call Home.html

[sourcecode language="html"]

Go Lang is one of a Language


[/sourcecode]

Try run Go on VSC or Atom or any other Idea and It will give you a Screen with GoLang is one of a Language

GoLang also Support Best Practice where you can't import a package or Library without using it, this will cause an error called Declared but not used which urge the Developer to use everything he import

References :-
https://www.gowitek.com/golang/blog/companies-using-golang
https://golangbot.com/goroutines/
https://golang.org/pkg/html/template/

Top comments (5)

Collapse
 
sennasemakula profile image
Senna • Edited

Hi, there are some problems with this code.

  1. If (err == nil)
  2. you're printing out an error but then later proceed to try and execute the template.
  3. When serving the /Home route, you have a typo as the second argument. Needs to be "HomeHandler"
Collapse
 
12mohaned profile image
mohaned mashaly • Edited

Thank you, I fixed them

Collapse
 
sennasemakula profile image
Senna • Edited

Nice. One more thing is that are you sure you want to execute the template when the error is not equal to nil? Shouldn't you move that outside the condition and replace it with a error log

That way you'll only try to execute the template if your html file correctly loaded

Thread Thread
 
12mohaned profile image
mohaned mashaly • Edited

the first version was right I reverted the changes, If your question is how you're not going to handle the exception if there's an error, well It depends, if you're using this in a production environment or a user is going to interact with application you must add an else statement which shows to the user an error happened or output a user friendly message or you will provide a bad user experience for the user, but since the tutorial focused on Golang Templating I didn't bother trying to deal with exceptions, but of-course it would be more convenient to add something like this in case you're dealing with a user
for example you can add log.fatal(err) also this won't be user friendly it will be for you as a developer to know what is the error.
Hope this answers your question

Thread Thread
 
sennasemakula profile image
Senna • Edited

You must have changed the code because I was referring to when you had:

if (err != nil) {
t.Execute(...)
}

I can see you have fixed this now.