[{"data":1,"prerenderedAt":2177},["ShallowReactive",2],{"blog-defer-in-golang":3,"blog-all-for-related":1721},{"id":4,"title":5,"author":6,"body":7,"cardAlt":5,"categories":1706,"description":1709,"draft":1710,"extension":1711,"headerImage":1712,"keywords":1713,"meta":1714,"name":1715,"navigation":73,"path":1716,"seo":1717,"series":6,"stem":1718,"updated":6,"year":1719,"__hash__":1720},"blog\u002Fblog\u002Fdefer-in-golang.md","Understanding Defer In Golang",null,{"type":8,"value":9,"toc":1696},"minimark",[10,14,19,40,46,151,154,193,1692],[11,12,13],"p",{},"In this article, we will be exploring what is defer in Golang, its uses, and how to implement it to avoid bugs. The most common use case of defer is for cleaning up resources before a function exits.",[15,16,18],"h2",{"id":17},"what-is-defer","What is defer?",[20,21,22,30,37],"ul",{},[23,24,25,29],"li",{},[26,27,28],"em",{},"Defer"," is used to ensure that a function call is performed later in a program’s execution before the function returns.",[23,31,32,36],{},[33,34,35],"code",{},"defer"," makes our code cleaner and less error-prone by keeping the calls to close the file\u002Fresource in proximity to the open call.",[23,38,39],{},"Cleaning up resources, such as open files, network connections, and database handlers.",[11,41,42,43,45],{},"Here is a simple example using the ",[33,44,35],{}," keyword.",[47,48,53],"pre",{"className":49,"code":50,"language":51,"meta":52,"style":52},"language-go shiki shiki-themes github-light github-dark","package main\n\nimport \"fmt\"\n\nfunc main() {\n    defer fmt.Println(\"Defer executes before the function returns!\")\n    fmt.Println(\"Hello! In main function\")\n}\n","go","",[33,54,55,68,75,91,96,109,130,145],{"__ignoreMap":52},[56,57,60,64],"span",{"class":58,"line":59},"line",1,[56,61,63],{"class":62},"szBVR","package",[56,65,67],{"class":66},"sScJk"," main\n",[56,69,71],{"class":58,"line":70},2,[56,72,74],{"emptyLinePlaceholder":73},true,"\n",[56,76,78,81,85,88],{"class":58,"line":77},3,[56,79,80],{"class":62},"import",[56,82,84],{"class":83},"sZZnC"," \"",[56,86,87],{"class":66},"fmt",[56,89,90],{"class":83},"\"\n",[56,92,94],{"class":58,"line":93},4,[56,95,74],{"emptyLinePlaceholder":73},[56,97,99,102,105],{"class":58,"line":98},5,[56,100,101],{"class":62},"func",[56,103,104],{"class":66}," main",[56,106,108],{"class":107},"sVt8B","() {\n",[56,110,112,115,118,121,124,127],{"class":58,"line":111},6,[56,113,114],{"class":62},"    defer",[56,116,117],{"class":107}," fmt.",[56,119,120],{"class":66},"Println",[56,122,123],{"class":107},"(",[56,125,126],{"class":83},"\"Defer executes before the function returns!\"",[56,128,129],{"class":107},")\n",[56,131,133,136,138,140,143],{"class":58,"line":132},7,[56,134,135],{"class":107},"    fmt.",[56,137,120],{"class":66},[56,139,123],{"class":107},[56,141,142],{"class":83},"\"Hello! In main function\"",[56,144,129],{"class":107},[56,146,148],{"class":58,"line":147},8,[56,149,150],{"class":107},"}\n",[11,152,153],{},"Output for the above code.",[47,155,159],{"className":156,"code":157,"language":158,"meta":52,"style":52},"language-bash shiki shiki-themes github-light github-dark","Hello! In main function\nDefer executes before the function returns!\n","bash",[33,160,161,174],{"__ignoreMap":52},[56,162,163,166,169,171],{"class":58,"line":59},[56,164,165],{"class":66},"Hello!",[56,167,168],{"class":83}," In",[56,170,104],{"class":83},[56,172,173],{"class":83}," function\n",[56,175,176,178,181,184,187,190],{"class":58,"line":70},[56,177,28],{"class":66},[56,179,180],{"class":83}," executes",[56,182,183],{"class":83}," before",[56,185,186],{"class":83}," the",[56,188,189],{"class":83}," function",[56,191,192],{"class":83}," returns!\n",[194,195,196,199,203,206,209,212,468,475,485,710,712,717,1597,1599,1603,1619,1623,1646,1650,1673,1675,1678,1687],"ads",{},[197,198],"hr",{},[15,200,202],{"id":201},"understanding-defer-with-example","Understanding Defer with Example",[11,204,205],{},"Let's take an example where you are accessing the file system i.e performing some operations on file like read\u002Fwrite. We need to load and read the contents of the file.",[11,207,208],{},"As mentioned before, we would want to ensure that resource cleanup is performed before we exit the method.",[11,210,211],{},"Let's write a function that creates a file and writes a string to it.",[47,213,215],{"className":49,"code":214,"language":51,"meta":52,"style":52},"package main\n\nimport (\n  \"fmt\"\n  \"os\"\n)\n\nfunc writeToFile(filename string) error {\n  fmt.Println(\"Open the file\")\n  file, err := os.Open(filename)\n  if err != nil {\n      return err\n  }\n\n  fmt.Println(\"Write to file\")\n  _, err := f.WriteString(\"writes\\n\")\n  if err != nil {\n      return err\n  }\n  return nil\n}\n\nfunc main() {\n  writeToFile(\"hello.txt\")\n}\n",[33,216,217,223,227,234,243,252,256,260,285,300,318,336,345,351,356,370,397,410,417,422,431,436,441,450,463],{"__ignoreMap":52},[56,218,219,221],{"class":58,"line":59},[56,220,63],{"class":62},[56,222,67],{"class":66},[56,224,225],{"class":58,"line":70},[56,226,74],{"emptyLinePlaceholder":73},[56,228,229,231],{"class":58,"line":77},[56,230,80],{"class":62},[56,232,233],{"class":107}," (\n",[56,235,236,239,241],{"class":58,"line":93},[56,237,238],{"class":83},"  \"",[56,240,87],{"class":66},[56,242,90],{"class":83},[56,244,245,247,250],{"class":58,"line":98},[56,246,238],{"class":83},[56,248,249],{"class":66},"os",[56,251,90],{"class":83},[56,253,254],{"class":58,"line":111},[56,255,129],{"class":107},[56,257,258],{"class":58,"line":132},[56,259,74],{"emptyLinePlaceholder":73},[56,261,262,264,267,269,273,276,279,282],{"class":58,"line":147},[56,263,101],{"class":62},[56,265,266],{"class":66}," writeToFile",[56,268,123],{"class":107},[56,270,272],{"class":271},"s4XuR","filename",[56,274,275],{"class":62}," string",[56,277,278],{"class":107},") ",[56,280,281],{"class":62},"error",[56,283,284],{"class":107}," {\n",[56,286,288,291,293,295,298],{"class":58,"line":287},9,[56,289,290],{"class":107},"  fmt.",[56,292,120],{"class":66},[56,294,123],{"class":107},[56,296,297],{"class":83},"\"Open the file\"",[56,299,129],{"class":107},[56,301,303,306,309,312,315],{"class":58,"line":302},10,[56,304,305],{"class":107},"  file, err ",[56,307,308],{"class":62},":=",[56,310,311],{"class":107}," os.",[56,313,314],{"class":66},"Open",[56,316,317],{"class":107},"(filename)\n",[56,319,321,324,327,330,334],{"class":58,"line":320},11,[56,322,323],{"class":62},"  if",[56,325,326],{"class":107}," err ",[56,328,329],{"class":62},"!=",[56,331,333],{"class":332},"sj4cs"," nil",[56,335,284],{"class":107},[56,337,339,342],{"class":58,"line":338},12,[56,340,341],{"class":62},"      return",[56,343,344],{"class":107}," err\n",[56,346,348],{"class":58,"line":347},13,[56,349,350],{"class":107},"  }\n",[56,352,354],{"class":58,"line":353},14,[56,355,74],{"emptyLinePlaceholder":73},[56,357,359,361,363,365,368],{"class":58,"line":358},15,[56,360,290],{"class":107},[56,362,120],{"class":66},[56,364,123],{"class":107},[56,366,367],{"class":83},"\"Write to file\"",[56,369,129],{"class":107},[56,371,373,376,378,381,384,386,389,392,395],{"class":58,"line":372},16,[56,374,375],{"class":107},"  _, err ",[56,377,308],{"class":62},[56,379,380],{"class":107}," f.",[56,382,383],{"class":66},"WriteString",[56,385,123],{"class":107},[56,387,388],{"class":83},"\"writes",[56,390,391],{"class":332},"\\n",[56,393,394],{"class":83},"\"",[56,396,129],{"class":107},[56,398,400,402,404,406,408],{"class":58,"line":399},17,[56,401,323],{"class":62},[56,403,326],{"class":107},[56,405,329],{"class":62},[56,407,333],{"class":332},[56,409,284],{"class":107},[56,411,413,415],{"class":58,"line":412},18,[56,414,341],{"class":62},[56,416,344],{"class":107},[56,418,420],{"class":58,"line":419},19,[56,421,350],{"class":107},[56,423,425,428],{"class":58,"line":424},20,[56,426,427],{"class":62},"  return",[56,429,430],{"class":332}," nil\n",[56,432,434],{"class":58,"line":433},21,[56,435,150],{"class":107},[56,437,439],{"class":58,"line":438},22,[56,440,74],{"emptyLinePlaceholder":73},[56,442,444,446,448],{"class":58,"line":443},23,[56,445,101],{"class":62},[56,447,104],{"class":66},[56,449,108],{"class":107},[56,451,453,456,458,461],{"class":58,"line":452},24,[56,454,455],{"class":66},"  writeToFile",[56,457,123],{"class":107},[56,459,460],{"class":83},"\"hello.txt\"",[56,462,129],{"class":107},[56,464,466],{"class":58,"line":465},25,[56,467,150],{"class":107},[11,469,470,471,474],{},"The code above works as expected but there is a bug. Can you spot it? What if the ",[33,472,473],{},"f.WriteString()"," fails and returns an error. Our function returns error but the source file resource is not closed.",[11,476,477,478,480,481,484],{},"This can be solved with the help of ",[33,479,35],{},". We add a defer on ",[33,482,483],{},"file.Close()"," to close source file even if the function returns an error.",[47,486,488],{"className":49,"code":487,"language":51,"meta":52,"style":52},"package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nfunc writeToFile(filename string) error {\n    fmt.Println(\"Open the file\")\n    file, err := os.Open(filename)\n    if err != nil {\n        return err\n    }\n    defer file.Close()\n\n    fmt.Println(\"Write to file\")\n    _, err := f.WriteString(\"writes\\n\")\n    if err != nil {\n        return err\n    }\n    return nil\n}\n\nfunc main() {\n    writeToFile(\"hello.txt\")\n}\n",[33,489,490,496,500,506,515,523,527,531,549,561,574,587,594,599,612,616,628,649,661,667,671,678,682,686,694,705],{"__ignoreMap":52},[56,491,492,494],{"class":58,"line":59},[56,493,63],{"class":62},[56,495,67],{"class":66},[56,497,498],{"class":58,"line":70},[56,499,74],{"emptyLinePlaceholder":73},[56,501,502,504],{"class":58,"line":77},[56,503,80],{"class":62},[56,505,233],{"class":107},[56,507,508,511,513],{"class":58,"line":93},[56,509,510],{"class":83},"    \"",[56,512,87],{"class":66},[56,514,90],{"class":83},[56,516,517,519,521],{"class":58,"line":98},[56,518,510],{"class":83},[56,520,249],{"class":66},[56,522,90],{"class":83},[56,524,525],{"class":58,"line":111},[56,526,129],{"class":107},[56,528,529],{"class":58,"line":132},[56,530,74],{"emptyLinePlaceholder":73},[56,532,533,535,537,539,541,543,545,547],{"class":58,"line":147},[56,534,101],{"class":62},[56,536,266],{"class":66},[56,538,123],{"class":107},[56,540,272],{"class":271},[56,542,275],{"class":62},[56,544,278],{"class":107},[56,546,281],{"class":62},[56,548,284],{"class":107},[56,550,551,553,555,557,559],{"class":58,"line":287},[56,552,135],{"class":107},[56,554,120],{"class":66},[56,556,123],{"class":107},[56,558,297],{"class":83},[56,560,129],{"class":107},[56,562,563,566,568,570,572],{"class":58,"line":302},[56,564,565],{"class":107},"    file, err ",[56,567,308],{"class":62},[56,569,311],{"class":107},[56,571,314],{"class":66},[56,573,317],{"class":107},[56,575,576,579,581,583,585],{"class":58,"line":320},[56,577,578],{"class":62},"    if",[56,580,326],{"class":107},[56,582,329],{"class":62},[56,584,333],{"class":332},[56,586,284],{"class":107},[56,588,589,592],{"class":58,"line":338},[56,590,591],{"class":62},"        return",[56,593,344],{"class":107},[56,595,596],{"class":58,"line":347},[56,597,598],{"class":107},"    }\n",[56,600,601,603,606,609],{"class":58,"line":353},[56,602,114],{"class":62},[56,604,605],{"class":107}," file.",[56,607,608],{"class":66},"Close",[56,610,611],{"class":107},"()\n",[56,613,614],{"class":58,"line":358},[56,615,74],{"emptyLinePlaceholder":73},[56,617,618,620,622,624,626],{"class":58,"line":372},[56,619,135],{"class":107},[56,621,120],{"class":66},[56,623,123],{"class":107},[56,625,367],{"class":83},[56,627,129],{"class":107},[56,629,630,633,635,637,639,641,643,645,647],{"class":58,"line":399},[56,631,632],{"class":107},"    _, err ",[56,634,308],{"class":62},[56,636,380],{"class":107},[56,638,383],{"class":66},[56,640,123],{"class":107},[56,642,388],{"class":83},[56,644,391],{"class":332},[56,646,394],{"class":83},[56,648,129],{"class":107},[56,650,651,653,655,657,659],{"class":58,"line":412},[56,652,578],{"class":62},[56,654,326],{"class":107},[56,656,329],{"class":62},[56,658,333],{"class":332},[56,660,284],{"class":107},[56,662,663,665],{"class":58,"line":419},[56,664,591],{"class":62},[56,666,344],{"class":107},[56,668,669],{"class":58,"line":424},[56,670,598],{"class":107},[56,672,673,676],{"class":58,"line":433},[56,674,675],{"class":62},"    return",[56,677,430],{"class":332},[56,679,680],{"class":58,"line":438},[56,681,150],{"class":107},[56,683,684],{"class":58,"line":443},[56,685,74],{"emptyLinePlaceholder":73},[56,687,688,690,692],{"class":58,"line":452},[56,689,101],{"class":62},[56,691,104],{"class":66},[56,693,108],{"class":107},[56,695,696,699,701,703],{"class":58,"line":465},[56,697,698],{"class":66},"    writeToFile",[56,700,123],{"class":107},[56,702,460],{"class":83},[56,704,129],{"class":107},[56,706,708],{"class":58,"line":707},26,[56,709,150],{"class":107},[197,711],{},[713,714,716],"h3",{"id":715},"important-points-to-remember","Important Points to Remember",[20,718,719,844,847,1039,1299,1383],{},[23,720,721,722,725,726,733,734,820,823,824],{},"Defer executes in ",[33,723,724],{},"LIFO"," - ",[727,728,732],"a",{"href":729,"rel":730},"https:\u002F\u002Fwww.geeksforgeeks.org\u002Flifo-last-in-first-out-approach-in-programming\u002F",[731],"nofollow","Last in first out"," order. This can be verified using this example.",[47,735,737],{"className":49,"code":736,"language":51,"meta":52,"style":52},"package main\n\nimport \"fmt\"\n\nfunc main() {\n    defer fmt.Println(\"First\")\n    defer fmt.Println(\"Second\")\n    defer fmt.Println(\"Third\")\n}\n",[33,738,739,745,749,759,763,771,786,801,816],{"__ignoreMap":52},[56,740,741,743],{"class":58,"line":59},[56,742,63],{"class":62},[56,744,67],{"class":66},[56,746,747],{"class":58,"line":70},[56,748,74],{"emptyLinePlaceholder":73},[56,750,751,753,755,757],{"class":58,"line":77},[56,752,80],{"class":62},[56,754,84],{"class":83},[56,756,87],{"class":66},[56,758,90],{"class":83},[56,760,761],{"class":58,"line":93},[56,762,74],{"emptyLinePlaceholder":73},[56,764,765,767,769],{"class":58,"line":98},[56,766,101],{"class":62},[56,768,104],{"class":66},[56,770,108],{"class":107},[56,772,773,775,777,779,781,784],{"class":58,"line":111},[56,774,114],{"class":62},[56,776,117],{"class":107},[56,778,120],{"class":66},[56,780,123],{"class":107},[56,782,783],{"class":83},"\"First\"",[56,785,129],{"class":107},[56,787,788,790,792,794,796,799],{"class":58,"line":132},[56,789,114],{"class":62},[56,791,117],{"class":107},[56,793,120],{"class":66},[56,795,123],{"class":107},[56,797,798],{"class":83},"\"Second\"",[56,800,129],{"class":107},[56,802,803,805,807,809,811,814],{"class":58,"line":147},[56,804,114],{"class":62},[56,806,117],{"class":107},[56,808,120],{"class":66},[56,810,123],{"class":107},[56,812,813],{"class":83},"\"Third\"",[56,815,129],{"class":107},[56,817,818],{"class":58,"line":287},[56,819,150],{"class":107},[821,822],"br",{},"Since all three print statements are with defer. Execution takes place in LIFO order and the following output is generated.",[47,825,827],{"className":156,"code":826,"language":158,"meta":52,"style":52},"Third\nSecond\nFirst\n",[33,828,829,834,839],{"__ignoreMap":52},[56,830,831],{"class":58,"line":59},[56,832,833],{"class":66},"Third\n",[56,835,836],{"class":58,"line":70},[56,837,838],{"class":66},"Second\n",[56,840,841],{"class":58,"line":77},[56,842,843],{"class":66},"First\n",[23,845,846],{},"Multiple defer statements can be used to release different resources used in a function. The point to remember is that the order of execution will be the opposite of what they are mentioned.",[23,848,849,851,852,934,936,937,1016,1018,1019],{},[33,850,35],{}," can be placed anywhere within the function since they are executed before the function returns.",[47,853,855],{"className":49,"code":854,"language":51,"meta":52,"style":52},"package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Start\")\n    defer fmt.Println(\"Defer\")\n    fmt.Println(\"End\")\n}\n",[33,856,857,863,867,877,881,889,902,917,930],{"__ignoreMap":52},[56,858,859,861],{"class":58,"line":59},[56,860,63],{"class":62},[56,862,67],{"class":66},[56,864,865],{"class":58,"line":70},[56,866,74],{"emptyLinePlaceholder":73},[56,868,869,871,873,875],{"class":58,"line":77},[56,870,80],{"class":62},[56,872,84],{"class":83},[56,874,87],{"class":66},[56,876,90],{"class":83},[56,878,879],{"class":58,"line":93},[56,880,74],{"emptyLinePlaceholder":73},[56,882,883,885,887],{"class":58,"line":98},[56,884,101],{"class":62},[56,886,104],{"class":66},[56,888,108],{"class":107},[56,890,891,893,895,897,900],{"class":58,"line":111},[56,892,135],{"class":107},[56,894,120],{"class":66},[56,896,123],{"class":107},[56,898,899],{"class":83},"\"Start\"",[56,901,129],{"class":107},[56,903,904,906,908,910,912,915],{"class":58,"line":132},[56,905,114],{"class":62},[56,907,117],{"class":107},[56,909,120],{"class":66},[56,911,123],{"class":107},[56,913,914],{"class":83},"\"Defer\"",[56,916,129],{"class":107},[56,918,919,921,923,925,928],{"class":58,"line":147},[56,920,135],{"class":107},[56,922,120],{"class":66},[56,924,123],{"class":107},[56,926,927],{"class":83},"\"End\"",[56,929,129],{"class":107},[56,931,932],{"class":58,"line":287},[56,933,150],{"class":107},[821,935],{},"Now let's change the order for defer statement.",[47,938,940],{"className":49,"code":939,"language":51,"meta":52,"style":52},"package main\n\nimport \"fmt\"\n\nfunc main() {\ndefer fmt.Println(\"Defer\")\n    fmt.Println(\"Start\")\n    fmt.Println(\"End\")\n}\n",[33,941,942,948,952,962,966,974,988,1000,1012],{"__ignoreMap":52},[56,943,944,946],{"class":58,"line":59},[56,945,63],{"class":62},[56,947,67],{"class":66},[56,949,950],{"class":58,"line":70},[56,951,74],{"emptyLinePlaceholder":73},[56,953,954,956,958,960],{"class":58,"line":77},[56,955,80],{"class":62},[56,957,84],{"class":83},[56,959,87],{"class":66},[56,961,90],{"class":83},[56,963,964],{"class":58,"line":93},[56,965,74],{"emptyLinePlaceholder":73},[56,967,968,970,972],{"class":58,"line":98},[56,969,101],{"class":62},[56,971,104],{"class":66},[56,973,108],{"class":107},[56,975,976,978,980,982,984,986],{"class":58,"line":111},[56,977,35],{"class":62},[56,979,117],{"class":107},[56,981,120],{"class":66},[56,983,123],{"class":107},[56,985,914],{"class":83},[56,987,129],{"class":107},[56,989,990,992,994,996,998],{"class":58,"line":132},[56,991,135],{"class":107},[56,993,120],{"class":66},[56,995,123],{"class":107},[56,997,899],{"class":83},[56,999,129],{"class":107},[56,1001,1002,1004,1006,1008,1010],{"class":58,"line":147},[56,1003,135],{"class":107},[56,1005,120],{"class":66},[56,1007,123],{"class":107},[56,1009,927],{"class":83},[56,1011,129],{"class":107},[56,1013,1014],{"class":58,"line":287},[56,1015,150],{"class":107},[821,1017],{},"Both code return the following output.",[47,1020,1022],{"className":156,"code":1021,"language":158,"meta":52,"style":52},"Start\nEnd\nDefer\n",[33,1023,1024,1029,1034],{"__ignoreMap":52},[56,1025,1026],{"class":58,"line":59},[56,1027,1028],{"class":66},"Start\n",[56,1030,1031],{"class":58,"line":70},[56,1032,1033],{"class":66},"End\n",[56,1035,1036],{"class":58,"line":77},[56,1037,1038],{"class":66},"Defer\n",[23,1040,1041,1042,1044,1048,1049,1051,1052,1054,1055,1058,1059,1184,1186,1187],{},"Defining defer in correct function scope. Recently at work, I faced an issue due to this. Let's try to explore this using a real world example.",[821,1043],{},[1045,1046,1047],"strong",{},"Real world example:"," Suppose you have a function in which you perform multiple tasks say validating input, some DB operations, publishing logs, etc.",[821,1050],{},"Now here you may want some operations to be asynchronous. You create a Goroutine and pass a context with a defined timeout. If you defer the cancel function of context without the anonymous function your synchronous code gets executed and the goroutine would cancel due to defer call after the original function has executed.",[821,1053],{},"Here ",[33,1056,1057],{},"publishKafka"," would fail due to defer cancelFunc() being in the wrong context.",[47,1060,1062],{"className":49,"code":1061,"language":51,"meta":52,"style":52},"func somefunc() {\n    \u002F\u002F peform some action\n    ...\n    ctx := context.Background()\n    ctx, cancelFunc := context.WithTimeout(ctx, 1 * time.Second)\n    \u002F\u002F This will cancel the goroutine once somefunc() is executed.\n    defer cancelFunc()\n\n    \u002F\u002F publish logs to kafka asynchronously\n    func() {\n        go publishLogs(ctx)\n    }\n    \u002F\u002F continue with other sync execution\n    updateDB()\n}\n",[33,1063,1064,1073,1079,1084,1099,1123,1128,1137,1141,1146,1153,1164,1168,1173,1180],{"__ignoreMap":52},[56,1065,1066,1068,1071],{"class":58,"line":59},[56,1067,101],{"class":62},[56,1069,1070],{"class":66}," somefunc",[56,1072,108],{"class":107},[56,1074,1075],{"class":58,"line":70},[56,1076,1078],{"class":1077},"sJ8bj","    \u002F\u002F peform some action\n",[56,1080,1081],{"class":58,"line":77},[56,1082,1083],{"class":62},"    ...\n",[56,1085,1086,1089,1091,1094,1097],{"class":58,"line":93},[56,1087,1088],{"class":107},"    ctx ",[56,1090,308],{"class":62},[56,1092,1093],{"class":107}," context.",[56,1095,1096],{"class":66},"Background",[56,1098,611],{"class":107},[56,1100,1101,1104,1106,1108,1111,1114,1117,1120],{"class":58,"line":98},[56,1102,1103],{"class":107},"    ctx, cancelFunc ",[56,1105,308],{"class":62},[56,1107,1093],{"class":107},[56,1109,1110],{"class":66},"WithTimeout",[56,1112,1113],{"class":107},"(ctx, ",[56,1115,1116],{"class":332},"1",[56,1118,1119],{"class":62}," *",[56,1121,1122],{"class":107}," time.Second)\n",[56,1124,1125],{"class":58,"line":111},[56,1126,1127],{"class":1077},"    \u002F\u002F This will cancel the goroutine once somefunc() is executed.\n",[56,1129,1130,1132,1135],{"class":58,"line":132},[56,1131,114],{"class":62},[56,1133,1134],{"class":66}," cancelFunc",[56,1136,611],{"class":107},[56,1138,1139],{"class":58,"line":147},[56,1140,74],{"emptyLinePlaceholder":73},[56,1142,1143],{"class":58,"line":287},[56,1144,1145],{"class":1077},"    \u002F\u002F publish logs to kafka asynchronously\n",[56,1147,1148,1151],{"class":58,"line":302},[56,1149,1150],{"class":62},"    func",[56,1152,108],{"class":107},[56,1154,1155,1158,1161],{"class":58,"line":320},[56,1156,1157],{"class":62},"        go",[56,1159,1160],{"class":66}," publishLogs",[56,1162,1163],{"class":107},"(ctx)\n",[56,1165,1166],{"class":58,"line":338},[56,1167,598],{"class":107},[56,1169,1170],{"class":58,"line":347},[56,1171,1172],{"class":1077},"    \u002F\u002F continue with other sync execution\n",[56,1174,1175,1178],{"class":58,"line":353},[56,1176,1177],{"class":66},"    updateDB",[56,1179,611],{"class":107},[56,1181,1182],{"class":58,"line":358},[56,1183,150],{"class":107},[821,1185],{},"To prevent this you can add an anonymous function that allows the goroutine to execute separately and with defer defined in that function's scope this would work as expected. An important takeaway is to place the defer under the right function scope.",[47,1188,1190],{"className":49,"code":1189,"language":51,"meta":52,"style":52},"func somefunc() {\n    \u002F\u002F peform some action\n    ...\n\n    \u002F\u002F publish logs to kafka asynchronously\n    func() {\n        ctx := context.Background()\n        ctx, cancelFunc := context.WithTimeout(ctx, 1 * time.Second)\n        go publishLogs(ctx)\n        \u002F\u002F This will wait for publishLogs to return\n        \u002F\u002F So this runs independent of somefunc() execution\n        defer cancelFunc()\n    }\n    \u002F\u002F continue with other sync execution\n    updateDB()\n}\n",[33,1191,1192,1200,1204,1208,1212,1216,1222,1235,1254,1262,1267,1272,1281,1285,1289,1295],{"__ignoreMap":52},[56,1193,1194,1196,1198],{"class":58,"line":59},[56,1195,101],{"class":62},[56,1197,1070],{"class":66},[56,1199,108],{"class":107},[56,1201,1202],{"class":58,"line":70},[56,1203,1078],{"class":1077},[56,1205,1206],{"class":58,"line":77},[56,1207,1083],{"class":62},[56,1209,1210],{"class":58,"line":93},[56,1211,74],{"emptyLinePlaceholder":73},[56,1213,1214],{"class":58,"line":98},[56,1215,1145],{"class":1077},[56,1217,1218,1220],{"class":58,"line":111},[56,1219,1150],{"class":62},[56,1221,108],{"class":107},[56,1223,1224,1227,1229,1231,1233],{"class":58,"line":132},[56,1225,1226],{"class":107},"        ctx ",[56,1228,308],{"class":62},[56,1230,1093],{"class":107},[56,1232,1096],{"class":66},[56,1234,611],{"class":107},[56,1236,1237,1240,1242,1244,1246,1248,1250,1252],{"class":58,"line":147},[56,1238,1239],{"class":107},"        ctx, cancelFunc ",[56,1241,308],{"class":62},[56,1243,1093],{"class":107},[56,1245,1110],{"class":66},[56,1247,1113],{"class":107},[56,1249,1116],{"class":332},[56,1251,1119],{"class":62},[56,1253,1122],{"class":107},[56,1255,1256,1258,1260],{"class":58,"line":287},[56,1257,1157],{"class":62},[56,1259,1160],{"class":66},[56,1261,1163],{"class":107},[56,1263,1264],{"class":58,"line":302},[56,1265,1266],{"class":1077},"        \u002F\u002F This will wait for publishLogs to return\n",[56,1268,1269],{"class":58,"line":320},[56,1270,1271],{"class":1077},"        \u002F\u002F So this runs independent of somefunc() execution\n",[56,1273,1274,1277,1279],{"class":58,"line":338},[56,1275,1276],{"class":62},"        defer",[56,1278,1134],{"class":66},[56,1280,611],{"class":107},[56,1282,1283],{"class":58,"line":347},[56,1284,598],{"class":107},[56,1286,1287],{"class":58,"line":353},[56,1288,1172],{"class":1077},[56,1290,1291,1293],{"class":58,"line":358},[56,1292,1177],{"class":66},[56,1294,611],{"class":107},[56,1296,1297],{"class":58,"line":372},[56,1298,150],{"class":107},[23,1300,1301,1302],{},"Deferred calls are executed even when the function panics.",[47,1303,1305],{"className":49,"code":1304,"language":51,"meta":52,"style":52},"package main\n\nimport \"fmt\"\n\nfunc main() {\n    defer fmt.Println(\"Defer even works in panic\")\n    panic(\"Stop right now\")\n    fmt.Println(\"Still working\")\n}\n",[33,1306,1307,1313,1317,1327,1331,1339,1354,1366,1379],{"__ignoreMap":52},[56,1308,1309,1311],{"class":58,"line":59},[56,1310,63],{"class":62},[56,1312,67],{"class":66},[56,1314,1315],{"class":58,"line":70},[56,1316,74],{"emptyLinePlaceholder":73},[56,1318,1319,1321,1323,1325],{"class":58,"line":77},[56,1320,80],{"class":62},[56,1322,84],{"class":83},[56,1324,87],{"class":66},[56,1326,90],{"class":83},[56,1328,1329],{"class":58,"line":93},[56,1330,74],{"emptyLinePlaceholder":73},[56,1332,1333,1335,1337],{"class":58,"line":98},[56,1334,101],{"class":62},[56,1336,104],{"class":66},[56,1338,108],{"class":107},[56,1340,1341,1343,1345,1347,1349,1352],{"class":58,"line":111},[56,1342,114],{"class":62},[56,1344,117],{"class":107},[56,1346,120],{"class":66},[56,1348,123],{"class":107},[56,1350,1351],{"class":83},"\"Defer even works in panic\"",[56,1353,129],{"class":107},[56,1355,1356,1359,1361,1364],{"class":58,"line":132},[56,1357,1358],{"class":66},"    panic",[56,1360,123],{"class":107},[56,1362,1363],{"class":83},"\"Stop right now\"",[56,1365,129],{"class":107},[56,1367,1368,1370,1372,1374,1377],{"class":58,"line":147},[56,1369,135],{"class":107},[56,1371,120],{"class":66},[56,1373,123],{"class":107},[56,1375,1376],{"class":83},"\"Still working\"",[56,1378,129],{"class":107},[56,1380,1381],{"class":58,"line":287},[56,1382,150],{"class":107},[23,1384,1385,1386],{},"Recover and catching panic in a goroutine with defer. If a goroutine panics, recover stops the unwinding and returns the argument passed to panic else it simply returns nil.",[47,1387,1389],{"className":49,"code":1388,"language":51,"meta":52,"style":52},"package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    num := panicFun()\n    fmt.Println(\"Returned value: \", num)\n}\n\nfunc panicFun() (num int) {\n    defer func() {\n        if err := recover(); err != nil {\n            fmt.Println(err)\n            num = -1\n        }\n    }()\n    num = 1\n    panic(\"let's panic\")\n    return num\n}\n\n  \u002F\u002F Output\n  \u002F\u002F let's panic\n  \u002F\u002F Returned value: -1\n",[33,1390,1391,1397,1401,1407,1415,1419,1423,1431,1443,1457,1461,1465,1483,1492,1513,1523,1537,1542,1547,1556,1567,1574,1578,1582,1587,1592],{"__ignoreMap":52},[56,1392,1393,1395],{"class":58,"line":59},[56,1394,63],{"class":62},[56,1396,67],{"class":66},[56,1398,1399],{"class":58,"line":70},[56,1400,74],{"emptyLinePlaceholder":73},[56,1402,1403,1405],{"class":58,"line":77},[56,1404,80],{"class":62},[56,1406,233],{"class":107},[56,1408,1409,1411,1413],{"class":58,"line":93},[56,1410,510],{"class":83},[56,1412,87],{"class":66},[56,1414,90],{"class":83},[56,1416,1417],{"class":58,"line":98},[56,1418,129],{"class":107},[56,1420,1421],{"class":58,"line":111},[56,1422,74],{"emptyLinePlaceholder":73},[56,1424,1425,1427,1429],{"class":58,"line":132},[56,1426,101],{"class":62},[56,1428,104],{"class":66},[56,1430,108],{"class":107},[56,1432,1433,1436,1438,1441],{"class":58,"line":147},[56,1434,1435],{"class":107},"    num ",[56,1437,308],{"class":62},[56,1439,1440],{"class":66}," panicFun",[56,1442,611],{"class":107},[56,1444,1445,1447,1449,1451,1454],{"class":58,"line":287},[56,1446,135],{"class":107},[56,1448,120],{"class":66},[56,1450,123],{"class":107},[56,1452,1453],{"class":83},"\"Returned value: \"",[56,1455,1456],{"class":107},", num)\n",[56,1458,1459],{"class":58,"line":302},[56,1460,150],{"class":107},[56,1462,1463],{"class":58,"line":320},[56,1464,74],{"emptyLinePlaceholder":73},[56,1466,1467,1469,1471,1474,1477,1480],{"class":58,"line":338},[56,1468,101],{"class":62},[56,1470,1440],{"class":66},[56,1472,1473],{"class":107},"() (",[56,1475,1476],{"class":271},"num",[56,1478,1479],{"class":62}," int",[56,1481,1482],{"class":107},") {\n",[56,1484,1485,1487,1490],{"class":58,"line":347},[56,1486,114],{"class":62},[56,1488,1489],{"class":62}," func",[56,1491,108],{"class":107},[56,1493,1494,1497,1499,1501,1504,1507,1509,1511],{"class":58,"line":353},[56,1495,1496],{"class":62},"        if",[56,1498,326],{"class":107},[56,1500,308],{"class":62},[56,1502,1503],{"class":66}," recover",[56,1505,1506],{"class":107},"(); err ",[56,1508,329],{"class":62},[56,1510,333],{"class":332},[56,1512,284],{"class":107},[56,1514,1515,1518,1520],{"class":58,"line":358},[56,1516,1517],{"class":107},"            fmt.",[56,1519,120],{"class":66},[56,1521,1522],{"class":107},"(err)\n",[56,1524,1525,1528,1531,1534],{"class":58,"line":372},[56,1526,1527],{"class":107},"            num ",[56,1529,1530],{"class":62},"=",[56,1532,1533],{"class":62}," -",[56,1535,1536],{"class":332},"1\n",[56,1538,1539],{"class":58,"line":399},[56,1540,1541],{"class":107},"        }\n",[56,1543,1544],{"class":58,"line":412},[56,1545,1546],{"class":107},"    }()\n",[56,1548,1549,1551,1553],{"class":58,"line":419},[56,1550,1435],{"class":107},[56,1552,1530],{"class":62},[56,1554,1555],{"class":332}," 1\n",[56,1557,1558,1560,1562,1565],{"class":58,"line":424},[56,1559,1358],{"class":66},[56,1561,123],{"class":107},[56,1563,1564],{"class":83},"\"let's panic\"",[56,1566,129],{"class":107},[56,1568,1569,1571],{"class":58,"line":433},[56,1570,675],{"class":62},[56,1572,1573],{"class":107}," num\n",[56,1575,1576],{"class":58,"line":438},[56,1577,150],{"class":107},[56,1579,1580],{"class":58,"line":443},[56,1581,74],{"emptyLinePlaceholder":73},[56,1583,1584],{"class":58,"line":452},[56,1585,1586],{"class":1077},"  \u002F\u002F Output\n",[56,1588,1589],{"class":58,"line":465},[56,1590,1591],{"class":1077},"  \u002F\u002F let's panic\n",[56,1593,1594],{"class":58,"line":707},[56,1595,1596],{"class":1077},"  \u002F\u002F Returned value: -1\n",[197,1598],{},[15,1600,1602],{"id":1601},"resources","Resources",[20,1604,1605,1612],{},[23,1606,1607],{},[727,1608,1611],{"href":1609,"rel":1610},"https:\u002F\u002Fwww.digitalocean.com\u002Fcommunity\u002Ftutorials\u002Funderstanding-defer-in-go",[731],"Understanding Defer",[23,1613,1614],{},[727,1615,1618],{"href":1616,"rel":1617},"https:\u002F\u002Fblog.golang.org\u002Fdefer-panic-and-recover",[731],"Defer, Panic, and Recover",[713,1620,1622],{"id":1621},"books-to-learn-golang","Books to learn Golang",[20,1624,1625,1632,1639],{},[23,1626,1627],{},[727,1628,1631],{"href":1629,"rel":1630},"https:\u002F\u002Famzn.to\u002F3fYLCqz",[731],"Go Programming Language",[23,1633,1634],{},[727,1635,1638],{"href":1636,"rel":1637},"https:\u002F\u002Famzn.to\u002F3s7fWS3",[731],"Learning Go",[23,1640,1641],{},[727,1642,1645],{"href":1643,"rel":1644},"https:\u002F\u002Famzn.to\u002F3t6PM37",[731],"Go in Action",[713,1647,1649],{"id":1648},"popular-go-articles","Popular Go Articles",[20,1651,1652,1659,1666],{},[23,1653,1654],{},[727,1655,1658],{"href":1656,"rel":1657},"https:\u002F\u002Fwww.mohitkhare.com\u002Fblog\u002Fintroduction-to-goroutines",[731],"Introduction to Goroutines",[23,1660,1661],{},[727,1662,1665],{"href":1663,"rel":1664},"https:\u002F\u002Fwww.mohitkhare.com\u002Fblog\u002Fenvironment-variable-golang\u002F",[731],"Environment Variables in Go",[23,1667,1668],{},[727,1669,1672],{"href":1670,"rel":1671},"https:\u002F\u002Fwww.mohitkhare.com\u002Fblog\u002Fmarshal-structs-golang",[731],"Marshal structs the right way",[197,1674],{},[11,1676,1677],{},"I hope you learned something new. Feel free to suggest improvements ✔️",[11,1679,1680,1681,1686],{},"I share regular updates and resources on ",[727,1682,1685],{"href":1683,"rel":1684},"https:\u002F\u002Ftwitter.com\u002Fmkfeuhrer",[731],"Twitter",". Let’s connect!",[11,1688,1689],{},[1045,1690,1691],{},"Keep exploring 🔎 Keep learning 🚀",[1693,1694,1695],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":52,"searchDepth":70,"depth":70,"links":1697},[1698,1699,1702],{"id":17,"depth":70,"text":18},{"id":201,"depth":70,"text":202,"children":1700},[1701],{"id":715,"depth":77,"text":716},{"id":1601,"depth":70,"text":1602,"children":1703},[1704,1705],{"id":1621,"depth":77,"text":1622},{"id":1648,"depth":77,"text":1649},[1707,1708],"Golang","Programming","Learn about defer keyword in golang and how it can help you avoid panics due to bugs.",false,"md","blog\u002Fdefer-in-golang\u002Fdefer-in-golang-card.webp","defer anonymous function golang, defer golang panic, when to use defer golang",{},"defer-in-golang","\u002Fblog\u002Fdefer-in-golang",{"title":5,"description":1709},"blog\u002Fdefer-in-golang","2021-05-05","aDatKbJ0XyruAGYd9yIieOIREGGthdY4kSW_OI_f-Bc",[1722,1729,1735,1743,1752,1759,1766,1773,1779,1781,1787,1793,1799,1805,1812,1818,1824,1830,1836,1842,1848,1854,1860,1867,1874,1880,1886,1892,1898,1904,1909,1915,1921,1927,1933,1939,1945,1951,1958,1964,1970,1976,1983,1989,1995,2002,2009,2015,2022,2028,2035,2041,2048,2054,2060,2066,2072,2078,2084,2090,2096,2102,2108,2114,2120,2127,2133,2140,2146,2152,2158,2165,2171],{"path":1723,"title":1724,"description":1725,"categories":1726,"draft":73,"year":1728,"series":6},"\u002Fblog\u002Fai-assisted-workstation","Building an AI-Assisted Personal Workstation","A living log of building a practical AI-assisted workstation for software and startup work.",[1727,1708],"Productivity","2026-08-08",{"path":1730,"title":1731,"description":1732,"categories":1733,"draft":1710,"year":1734,"series":6},"\u002Fblog\u002Faws-s3-golang","Uploading images to AWS S3 in Golang","In this tutorial I cover how to upload, fetch and manage other operations for objects on AWS S3 in Golang",[1707,1708],"2020-01-12",{"path":1736,"title":1737,"description":1738,"categories":1739,"draft":1710,"year":1742,"series":6},"\u002Fblog\u002Fbooks-for-engineers","Best Books for Software Engineers","Best Books to help you build you software engineering career - ranging from fundamentats, interview prep to productivity.",[1740,1741],"Books","Life","2021-12-26",{"path":1744,"title":1745,"description":1746,"categories":1747,"draft":1710,"year":1751,"series":6},"\u002Fblog\u002Fbuilding-acrons","Building Acrons 🚀","Building Acrons, a one-stop tool to decode everyday acronyms like DIY, FOMO, and BRB without losing your flow.",[1748,1749,1750],"Side Projects","Web","Tools","2020-05-26",{"path":1753,"title":1754,"description":1755,"categories":1756,"draft":1710,"year":1758,"series":6},"\u002Fblog\u002Fcap-theorem","CAP Theorem Explained","Learn the concept and misconceptions around popular CAP theorem in system design.",[1757,1708],"System Design","2021-07-26",{"path":1760,"title":1761,"description":1762,"categories":1763,"draft":1710,"year":1765,"series":6},"\u002Fblog\u002Fcode-reviews","Code Review Checklist","Why code reviews matter, and best practices that catch bugs early and keep code quality high before merge.",[1764],"Engineering Principles","2020-07-20",{"path":1767,"title":1768,"description":1769,"categories":1770,"draft":1710,"year":1772,"series":6},"\u002Fblog\u002Fdark-mode","Add Dark mode to websites","Dark mode is ❤️️ Add it to your websites with little CSS and JS",[1771,1749],"Javascript","2020-07-04",{"path":1774,"title":1775,"description":1776,"categories":1777,"draft":1710,"year":1778,"series":6},"\u002Fblog\u002Fdate-time-golang","Working with Date and Time in Go","Learn how to use time in Golang - Multiple formats, locations and using date.",[1707,1708],"2020-10-09",{"path":1716,"title":5,"description":1709,"categories":1780,"draft":1710,"year":1719,"series":6},[1707,1708],{"path":1782,"title":1783,"description":1784,"categories":1785,"draft":1710,"year":1786,"series":6},"\u002Fblog\u002Fenums-golang","Implementing Enums in Golang","Enums are a way to defined set of constant values. Learn how to implement enums in Go using iota with examples.",[1707,1708],"2021-12-18",{"path":1788,"title":1789,"description":1790,"categories":1791,"draft":1710,"year":1792,"series":6},"\u002Fblog\u002Fenvironment-variable-golang","Guide to Environment variables in Go","Learn what are environment variables and how to use them in Go",[1707,1708],"2020-08-14",{"path":1794,"title":1795,"description":1796,"categories":1797,"draft":1710,"year":1798,"series":6},"\u002Fblog\u002Ferror-handling-golang","Handle errors the right way — Golang","How to handle errors in golang to make your life easy",[1707,1708],"2020-01-30",{"path":1800,"title":1801,"description":1802,"categories":1803,"draft":1710,"year":1804,"series":6},"\u002Fblog\u002Ffile-upload-golang","Uploading Files in Golang with Multipart Request","Learn how to upload files from your client to server as a multipart request in Golang.",[1707,1708],"2021-03-13",{"path":1806,"title":1807,"description":1808,"categories":1809,"draft":1710,"year":1811,"series":6},"\u002Fblog\u002Fgit-branch","Git Branch 101","Branching is a new path of development that makes it a lot easier to build features. Learn why and how to use branching in git.",[1810,1708],"Git","2022-03-20",{"path":1813,"title":1814,"description":1815,"categories":1816,"draft":1710,"year":1817,"series":6},"\u002Fblog\u002Fgit-pull-request-template","Guide to Pull Request Templates","Improve collaboration and code reviews with pull request templates. Blog include PR template and how to create one on github\u002Fgitlab",[1810,1708],"2023-08-24",{"path":1819,"title":1820,"description":1821,"categories":1822,"draft":1710,"year":1823,"series":6},"\u002Fblog\u002Fgit-remove-sensitive-info","Securing Git: Remove Sensitive Information","Protect your code from security breaches by using BFG tool to remove sensitive information from Git history. Learn how to do it in this comprehensive guide.",[1810,1708],"2023-10-16",{"path":1825,"title":1826,"description":1827,"categories":1828,"draft":1710,"year":1829,"series":6},"\u002Fblog\u002Fgit-tags-explained","Git tags : Explained","What Git tags are, why and when to use them, and how to start tagging releases in your own projects.",[1810,1708],"2020-06-18",{"path":1831,"title":1832,"description":1833,"categories":1834,"draft":1710,"year":1835,"series":6},"\u002Fblog\u002Fgo-dependency-injection","Dependency Injection in Go using Wire","Learn about dependency injection, its benefits and how to implement it in Go services using wire",[1707,1708],"2022-11-26",{"path":1837,"title":1838,"description":1839,"categories":1840,"draft":1710,"year":1841,"series":6},"\u002Fblog\u002Fgo-generics","Introduction to Generics in Go","Learn about generics in Golang, it's benefits and how to implement it in applications.",[1707,1708],"2022-11-27",{"path":1843,"title":1844,"description":1845,"categories":1846,"draft":1710,"year":1847,"series":6},"\u002Fblog\u002Fgo-in-memory-cache","Building In-Memory Cache in Go","Learn about in-memory caching in Golang, it's benefits and how to implement it with and without generics with a TTL(expiry).",[1707,1708],"2024-03-28",{"path":1849,"title":1850,"description":1851,"categories":1852,"draft":1710,"year":1853,"series":6},"\u002Fblog\u002Fgo-makefile","Ultimate Makefile for Golang","Boost your productivity and save time with ultimate Makefile for Golang projects.",[1707,1708],"2024-06-10",{"path":1855,"title":1856,"description":1857,"categories":1858,"draft":1710,"year":1859,"series":6},"\u002Fblog\u002Fgo-middleware","Mastering Middlewares in Golang","Learn about middlewares, their use cases and how to implement them in Golang applications",[1707,1708],"2022-03-13",{"path":1861,"title":1862,"description":1863,"categories":1864,"draft":1710,"year":1866,"series":6},"\u002Fblog\u002Fgo-naming-conventions","Naming Conventions in Golang","Why naming conventions matter in Go, with best practices and practical examples for clear, readable, maintainable code.",[1707,1708,1865],"Code Quality","2023-05-19",{"path":1868,"title":1869,"description":1870,"categories":1871,"draft":1710,"year":1873,"series":6},"\u002Fblog\u002Fgo-with-redis","Go with Redis","A practical introduction to using Redis with Golang to make your applications faster with in-memory data storage.",[1707,1872,1708],"Database","2020-05-03",{"path":1875,"title":1876,"description":1877,"categories":1878,"draft":1710,"year":1879,"series":6},"\u002Fblog\u002Fgolang-maps","Learn Maps in Golang (with examples)","A complete guide to maps in Golang: creating maps, adding and removing items, iterating, and how map equality works.",[1707,1708],"2020-09-10",{"path":1881,"title":1882,"description":1883,"categories":1884,"draft":1710,"year":1885,"series":6},"\u002Fblog\u002Fguide-to-cdn","Guide to Content Delivery Network","CDN is one of the backbone of modern world internet infrastructure. Learn about Content Delivery Network, it's working, types and benefits.",[1757,1708],"2022-01-23",{"path":1887,"title":1888,"description":1889,"categories":1890,"draft":1710,"year":1891,"series":6},"\u002Fblog\u002Fguide-to-rule-engines","Guide to Rule Engines","Rule Engines help in solving changing business requirements with ease. Learn all about Rule engines, it's working, benefits and how to implement one in Golang",[1707,1708],"2022-09-12",{"path":1893,"title":1894,"description":1895,"categories":1896,"draft":1710,"year":1897,"series":6},"\u002Fblog\u002Fhexagonal-architecture","Guide to Hexagonal Architecture","Learn how to design efficient application with hexagonal architecture with practical example",[1757,1708],"2020-12-13",{"path":1899,"title":1900,"description":1901,"categories":1902,"draft":1710,"year":1903,"series":6},"\u002Fblog\u002Fhttp-status-codes","Learn HTTP Status Codes","A tour of HTTP response status codes, what each class means, and when to use which code in your applications.",[1749,1708],"2020-07-30",{"path":1905,"title":1658,"description":1906,"categories":1907,"draft":1710,"year":1908,"series":6},"\u002Fblog\u002Fintroduction-to-goroutines","Understand the basics of concurrency and learn how to work with Goroutines in golang.",[1707,1708],"2021-04-10",{"path":1910,"title":1911,"description":1912,"categories":1913,"draft":1710,"year":1914,"series":6},"\u002Fblog\u002Fjson-in-postgres-with-golang","Storing JSON in Postgres with Golang","Learn how to store JSON objects in Postgres and how to implement it in Golang.",[1708,1872,1707],"2021-02-13",{"path":1916,"title":1917,"description":1918,"categories":1919,"draft":1710,"year":1920,"series":6},"\u002Fblog\u002Fjson-web-token","Complete Guide to JWT","JSON Web Tokens are a very compact way to carry information. Learn about JWTs in depth, from its structure to when to use it.",[1708,1749],"2021-11-20",{"path":1922,"title":1923,"description":1924,"categories":1925,"draft":1710,"year":1926,"series":6},"\u002Fblog\u002Flearn-to-say-no","Learning to say No","Learn why it's important to say 'NO'. How it helps in increasing productivity and explore strategies for saying No",[1741,1740],"2022-04-09",{"path":1928,"title":1929,"description":1930,"categories":1931,"draft":1710,"year":1932,"series":6},"\u002Fblog\u002Flearn-unlearn-relearn","Learn - Unlearn - Relearn","Unlearning things to learn new is the way to grow. Discover how to do that!",[1727,1741],"2020-09-25",{"path":1934,"title":1935,"description":1936,"categories":1937,"draft":1710,"year":1938,"series":6},"\u002Fblog\u002Flinting-in-golang","Introduction to Linting in Go","Introduction to improving code quality using linting. Learn how to add linting in your Go projects.",[1707,1708],"2021-07-07",{"path":1940,"title":1941,"description":1942,"categories":1943,"draft":1710,"year":1944,"series":6},"\u002Fblog\u002Fload-balancing","Load Balancing 101","Complete guide on load balancers explaining the internal working and how does it help in making applications efficient.",[1757,1708],"2021-05-29",{"path":1946,"title":1947,"description":1948,"categories":1949,"draft":1710,"year":1950,"series":6},"\u002Fblog\u002Fmaking-decisions-the-right-way","Making Decisions: The right way","Decisions are hard! Learn how to make the right decisions always",[1727,1741],"2020-06-07",{"path":1952,"title":1953,"description":1954,"categories":1955,"draft":1710,"year":1957,"series":6},"\u002Fblog\u002Fmanage-logs-with-logrotate","Using Logrotate to manage logs","Learn how to use logrotate system utility to manage logs with example",[1956,1708],"Linux","2020-09-08",{"path":1959,"title":1960,"description":1961,"categories":1962,"draft":1710,"year":1963,"series":6},"\u002Fblog\u002Fmarshal-structs-golang","Marshal structs the right way: Golang","Why Golang marshals empty structs into your JSON instead of null, and how to marshal structs the right way.",[1707,1708],"2020-03-31",{"path":1965,"title":1966,"description":1967,"categories":1968,"draft":1710,"year":1969,"series":6},"\u002Fblog\u002Fnotes-almanack-naval","Book Notes : Almanack of Naval Ravikant","My notes\u002Fhighlights from Almanack of Naval Ravikant book by Eric Jorgenson",[1740,1741],"2020-10-17",{"path":1971,"title":1972,"description":1973,"categories":1974,"draft":1710,"year":1975,"series":6},"\u002Fblog\u002Fnotes-anything-you-want","Book Notes : Anything you want","My notes\u002Fhighlights from Anything you want book by Derek Sivers",[1740,1741],"2021-09-25",{"path":1977,"title":1978,"description":1979,"categories":1980,"draft":1710,"year":1982,"series":6},"\u002Fblog\u002Fnotes-getting-real","Book Notes : Getting Real","My notes and highlights from Getting Real by Jason Fried and David Heinemeier Hansson on building web applications.",[1740,1981],"Startups","2020-11-03",{"path":1984,"title":1985,"description":1986,"categories":1987,"draft":1710,"year":1988,"series":6},"\u002Fblog\u002Fnotes-hell-yeah-or-no","Book Notes : Hell Yeah or No","My notes\u002Fhighlights from Hell Yeah or No book by Derek Sivers",[1740,1741],"2020-09-16",{"path":1990,"title":1991,"description":1992,"categories":1993,"draft":1710,"year":1994,"series":6},"\u002Fblog\u002Fnotes-how-to-be-a-capitalist","Book Notes : How to Be a Capitalist Without Any Capital","My notes\u002Fhighlights from How to Be a Capitalist Without Any Capital by Nathan Latka",[1740,1981],"2020-01-07",{"path":1996,"title":1997,"description":1998,"categories":1999,"draft":1710,"year":2001,"series":6},"\u002Fblog\u002Fnotes-lean-b2b","Book Notes : Lean B2B","My notes and highlights from Lean B2B by Étienne Garbugli on assessing markets, building MVPs, and selling to businesses.",[1740,2000],"Entrepreneurship","2022-07-02",{"path":2003,"title":2004,"description":2005,"categories":2006,"draft":1710,"year":2008,"series":6},"\u002Fblog\u002Fnotes-lets-talk-money","Book Notes : Let's Talk Money","My notes and highlights from Let's Talk Money by Monica Halan on managing personal finances and savings.",[1740,2007],"Personal Finance","2021-02-26",{"path":2010,"title":2011,"description":2012,"categories":2013,"draft":1710,"year":2014,"series":6},"\u002Fblog\u002Fnotes-obviously-awesome","Book Notes : Obviously Awesome","My notes and highlights from Obviously Awesome by April Dunford on product positioning and marketing strategy.",[1740,1981],"2020-04-26",{"path":2016,"title":2017,"description":2018,"categories":2019,"draft":1710,"year":2021,"series":6},"\u002Fblog\u002Fnotes-refactoring-ui","Book Notes: Refactoring UI","My notes and highlights from Refactoring UI by Adam Wathan and Steve Schoger, full of practical design tips for engineers.",[1740,2020],"Design","2020-05-09",{"path":2023,"title":2024,"description":2025,"categories":2026,"draft":1710,"year":2027,"series":6},"\u002Fblog\u002Fnotes-rework","Book Notes: Rework","My notes and highlights from Rework by Jason Fried and David Heinemeier Hansson on a better way to succeed in business.",[1740,1981],"2021-04-14",{"path":2029,"title":2030,"description":2031,"categories":2032,"draft":1710,"year":2034,"series":6},"\u002Fblog\u002Fone-on-one-meetings","Guide to Effective 1:1 Meetings","One on One meetings are important for your personal & professional growth. Learn how to master 1:1s with your manager and peers.",[2033,1727],"Career","2023-03-05",{"path":2036,"title":2037,"description":2038,"categories":2039,"draft":1710,"year":2040,"series":6},"\u002Fblog\u002Fpersonal-okrs","Personal OKRs for Success","Why one should set personal OKRs and how to achieve success with them",[1727,1741],"2020-06-27",{"path":2042,"title":2043,"description":2044,"categories":2045,"draft":1710,"year":2047,"series":6},"\u002Fblog\u002Fpoker-tips","Poker Tips: Johns Hopkins Poker Class","Notes from Johns Hopkins poker course. This guide will help you improve in Poker and win some large pots.",[1741,2046],"Poker","2020-06-13",{"path":2049,"title":2050,"description":2051,"categories":2052,"draft":1710,"year":2053,"series":6},"\u002Fblog\u002Fpomodoro","Get work done: The Pomo Way","Learn to focus and get productive by following the Pomodoro Technique",[1727],"2020-10-24",{"path":2055,"title":2056,"description":2057,"categories":2058,"draft":1710,"year":2059,"series":6},"\u002Fblog\u002Fpostgres-constraints","Postgres Constraints","Constraints are line of defense for database. Explore various types of constrainsts in postgres",[1872,1708],"2020-11-07",{"path":2061,"title":2062,"description":2063,"categories":2064,"draft":1710,"year":2065,"series":6},"\u002Fblog\u002Fppf-explained","PPF Explained","Learn basics about Public Provident Fund and why you should invest",[1741,2007],"2020-12-24",{"path":2067,"title":2068,"description":2069,"categories":2070,"draft":1710,"year":2071,"series":6},"\u002Fblog\u002Fproductivity-chrome-extensions","Boost Productivity with Chrome Extensions","Utitizing chrome extensions to boost your productivity!",[1727,1750,1749],"2020-04-24",{"path":2073,"title":2074,"description":2075,"categories":2076,"draft":1710,"year":2077,"series":6},"\u002Fblog\u002Fproductivity-in-vscode","Improve your productivity with VS Code","Key VS Code features like the integrated terminal, shortcuts, and extensions that improved my everyday productivity.",[1727,1708],"2020-03-12",{"path":2079,"title":2080,"description":2081,"categories":2082,"draft":1710,"year":2083,"series":6},"\u002Fblog\u002Freading-101","Improving Reading 101","Why reading matters, how to build a reading habit, and what to read next - from my own journey into books.",[1740,1741],"2020-04-04",{"path":2085,"title":2086,"description":2087,"categories":2088,"draft":1710,"year":2089,"series":6},"\u002Fblog\u002Freverse-proxy","Understanding Reverse Proxy","Learn about reverse proxy, how it is different from forward proxy and explore advantages of using it in system design.",[1757,1708],"2021-10-03",{"path":2091,"title":2092,"description":2093,"categories":2094,"draft":1710,"year":2095,"series":6},"\u002Fblog\u002Frunning-periodic-background-task-golang","Running periodic background tasks in Golang","How to run periodic background tasks in Golang by combining cron-style scheduling with background execution.",[1707,1708],"2019-11-21",{"path":2097,"title":2098,"description":2099,"categories":2100,"draft":1710,"year":2101,"series":6},"\u002Fblog\u002Fsessions-in-golang","Sessions using Golang and Redis","How cookies and sessions work, and how to implement persistent login sessions in Golang using Redis.",[1707,1872,1708],"2020-02-25",{"path":2103,"title":2104,"description":2105,"categories":2106,"draft":1710,"year":2107,"series":6},"\u002Fblog\u002Fsolid-dry-kiss-yagni","Solid Dry Kiss Yagni","An introduction to the SOLID, DRY, KISS, and YAGNI principles and how they help you write cleaner, robust code.",[1764],"2020-07-17",{"path":2109,"title":2110,"description":2111,"categories":2112,"draft":1710,"year":2113,"series":6},"\u002Fblog\u002Fstack-in-golang","Implement Stack in Golang","Learn how to implement stack data structure in Golang (Full code)",[1707,1708],"2020-09-14",{"path":2115,"title":2116,"description":2117,"categories":2118,"draft":1710,"year":2119,"series":6},"\u002Fblog\u002Fstart-oss-today","Start your Open Source journey today","Start your OSS journey with intro to GIT. Participate in Hacktoberfest!",[1810,1708],"2020-09-19",{"path":2121,"title":2122,"description":2123,"categories":2124,"draft":1710,"year":2126,"series":6},"\u002Fblog\u002Fsticky-social-bar","Sticky social share component in HTML","Learn how to build a social media sharing component in HTML and add it to your website.",[1708,2125],"Frontend","2021-02-05",{"path":2128,"title":2129,"description":2130,"categories":2131,"draft":1710,"year":2132,"series":6},"\u002Fblog\u002Fterm-insurance","Term insurance Simplified","Everything you need to know before you buy a term insurance.",[1741,2007],"2020-11-29",{"path":2134,"title":2135,"description":2136,"categories":2137,"draft":1710,"year":2139,"series":6},"\u002Fblog\u002Fterminal-output-with-redirection","Handling terminal outputs with Redirection","Learn how to handle terminal outputs with Redirection and saving it to files",[1956,2138,1708],"Terminal","2020-09-06",{"path":2141,"title":2142,"description":2143,"categories":2144,"draft":1710,"year":2145,"series":6},"\u002Fblog\u002Fthink-and-grow-rich","Book Notes: Think and Grow Rich","My notes and highlights from Think and Grow Rich by Napoleon Hill, covering its process for building wealth.",[1740,1741],"2020-05-19",{"path":2147,"title":2148,"description":2149,"categories":2150,"draft":1710,"year":2151,"series":6},"\u002Fblog\u002Ftransactions-postgres-golang","Transactions on Postgres with Golang","Performing Postgres database operations in Golang with GORM, from basic queries to full transaction support.",[1707,1872,1708],"2020-03-18",{"path":2153,"title":2154,"description":2155,"categories":2156,"draft":1710,"year":2157,"series":6},"\u002Fblog\u002Fwaitgroups-in-golang","WaitGroups in Golang","Learn about WaitGroups in golang and how to use them to perform concurrent operations without blocking the main thread.",[1707,1708],"2022-05-31",{"path":2159,"title":2160,"description":2161,"categories":2162,"draft":1710,"year":2164,"series":6},"\u002Fblog\u002Fwhat-i-use","What I Use: Products and Softwares","List of what I use in my day to day life ranging from terminal, editors, softwares, devices to accessories",[1741,2163],"Tech","2024-06-01",{"path":2166,"title":2167,"description":2168,"categories":2169,"draft":1710,"year":2170,"series":6},"\u002Fblog\u002Fyear-in-review-2020","Year in Review: 2020","It's a wrap! 2020 was a crazy year, See how my year went!",[1741],"2020-12-31",{"path":2172,"title":2173,"description":2174,"categories":2175,"draft":1710,"year":2176,"series":6},"\u002Fblog\u002Fyear-in-review-2021","Year in Review: 2021","It's a wrap! 2021 was a mixed bag, full of ups and downs. Key Goal for 2022: Start now!",[1741],"2021-12-31",1787743428058]