GoLang Templating
Tags: go, html, http
Published: 2020-04-22 23:11:33

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 this<a href="https://www.geeksforgeeks.org/goroutines-concurrency-in-golang/">Link</a>



<img src="https://media.geeksforgeeks.org/wp-content/uploads/20200330051224/Go_Logo4.png" alt="" width="299" height="169" class="alignnone size-full wp-image-1604348" />




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 

<code>
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
}

}
</code>

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

[sourcecode language="html"]

<html>
<h1>Go Lang is one of a Language </h1>
</html>
[/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/