Posts

Showing posts from March, 2023

Javascript: call vs bind vs apply

 Hello Everyone,  I came across a snippet where code have .call() method. Thought its been a while since I went through their understanding, via this article I am explaining with examples what call, bind and apply does in javascript. Please read comments for understanding. Hope it helps. var obj = { name : "Raja" , sayHello : function ( args ) { console . log ( `Hello ${this . name } , How are you doing ` ); }, }; // call : Changing the context::this scope to the argument we are passing. // normal obj . sayHello () // Hello Raja, How are you doing ? obj . sayHello . call ({ name : "Rajasekhar" }); // Hello Rajasekhar, How are you doing ? var obj = { name : "Raja" , sayHello : function ( args ) { console . log ( `Hello ${this . name } , How are you doing ? ${ args } ` ); }, }; // apply: same as call, we can also pass arguments as 2nd argument obj . sayHello . apply ({ name : "Rajasekhar" }, [ &

GoLang: Quick gist on Difference between Marshal, Unmarshal, Encoder and Decoder

Summary: Marshal, Unmarshal and Encode, Decode Summary: Marshal, Unmarshal and Encode, Decode Marshal: Convert struct/map interface data into JSON. Unmarshal: Convert JSON data into struct/map If reading from a file involved, then you read it in []byte format. To read from a file using os.Open open the file. then read all the data using io.ReadAll method. Encoder and Decoder kind of do reading/writing internally. More like a simplified version of Marshal and Unmarshal. Encoder converts the struct/map into JSON and automatically writes into io.Writer Decoder reads from io.Writer and convert the JSON data into struct/map interface. If there is a byte slice to work with, use Marshal and Unmarshal. If there is an io.Writer or an io.Reader, use Encode and Decode. Ref: https://blog.devgenius.io/to-unmarshal-or-to-decode-json-processing-in-go-expl

GoLang: How to render a web template using Gin framework and render data from map ?

 I have struggled lot of time, to break this into working modal. So below code, is working example where you can have your data in a slice of URLs and render that data using Go Templates.  I am giving a skeleton HTML template, not adding any thing extra.  I believe the code itself is self explanatory, if any questions, please dont hesitate to ask. main.go package main import ( "log" "net/http" "github.com/gin-gonic/gin" ) type URL struct { Name string Url string } var URLs = []URL{ {Name: "google" , Url: "google.com" }, {Name: "yahoo" , Url: "yahoo.com" }, {Name: "gmail" , Url: "gmail.com" }, } func main () { router := gin. Default () router. LoadHTMLGlob ( "templates/*" ) router. GET ( "/" , bookmarks) router. GET ( "/hello" , func (c *gin.Context) { c. HTML (http.StatusOK, "index.tmpl" , gin