{"id":13,"date":"2026-05-17T11:43:34","date_gmt":"2026-05-17T11:43:34","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/?p=13"},"modified":"2026-07-14T22:58:47","modified_gmt":"2026-07-14T17:28:47","slug":"go-lang-interview-questions-and-answers-2026","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/","title":{"rendered":"Go Lang Interview Questions and Answers 2026"},"content":{"rendered":"<h2 data-section-id=\"8iui6q\" data-start=\"241\" data-end=\"280\">1. What is Go and why is it popular?<\/h2>\n<p data-start=\"282\" data-end=\"454\">Go is an open-source programming language developed by <span class=\"hover:entity-accent entity-underline inline cursor-pointer align-baseline\"><span class=\"whitespace-normal\">Google<\/span><\/span>. It is designed for simplicity, concurrency, fast compilation, and scalability.<\/p>\n<h3 data-section-id=\"4ybteb\" data-start=\"456\" data-end=\"475\">Key advantages:<\/h3>\n<ul data-start=\"476\" data-end=\"635\">\n<li data-section-id=\"vrqrsf\" data-start=\"476\" data-end=\"491\">Simple syntax<\/li>\n<li data-section-id=\"190ltve\" data-start=\"492\" data-end=\"508\">Fast execution<\/li>\n<li data-section-id=\"jugp7p\" data-start=\"509\" data-end=\"544\">Built-in concurrency (goroutines)<\/li>\n<li data-section-id=\"4wfatn\" data-start=\"545\" data-end=\"565\">Garbage collection<\/li>\n<li data-section-id=\"1u9suzj\" data-start=\"566\" data-end=\"591\">Strong standard library<\/li>\n<li data-section-id=\"m7vxn2\" data-start=\"592\" data-end=\"635\">Great for microservices and cloud systems<\/li>\n<\/ul>\n<hr data-start=\"637\" data-end=\"640\" \/>\n<h2 data-section-id=\"17hphkv\" data-start=\"642\" data-end=\"668\">2. What are goroutines?<\/h2>\n<p data-start=\"670\" data-end=\"731\">Goroutines are lightweight threads managed by the Go runtime.<\/p>\n<p data-start=\"733\" data-end=\"741\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">go<\/span> <span class=\"\u037cm\">myFunction<\/span>()<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Features:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"783\" data-end=\"865\">\n<li data-section-id=\"lpgybk\" data-start=\"783\" data-end=\"806\">Very low memory usage<\/li>\n<li data-section-id=\"1plhxal\" data-start=\"807\" data-end=\"831\">Faster than OS threads<\/li>\n<li data-section-id=\"sc5c6e\" data-start=\"832\" data-end=\"865\">Used for concurrent programming<\/li>\n<\/ul>\n<hr data-start=\"867\" data-end=\"870\" \/>\n<h2 data-section-id=\"t4pbwa\" data-start=\"872\" data-end=\"902\">3. What is a channel in Go?<\/h2>\n<p data-start=\"904\" data-end=\"959\">Channels are used for communication between goroutines.<\/p>\n<p data-start=\"961\" data-end=\"969\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">chan<\/span> <span class=\"\u037cm\">int<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n<span class=\"\u037cg\">go<\/span> <span class=\"\u037cg\">func<\/span>() {\r\n    <span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">&lt;-<\/span> <span class=\"\u037cj\">10<\/span>\r\n}()\r\n\r\n<span class=\"\u037cm\">value<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cg\">&lt;-<\/span><span class=\"\u037cm\">ch<\/span>\r\n<span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037cm\">value<\/span>)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Benefits:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"1080\" data-end=\"1167\">\n<li data-section-id=\"1bv0u49\" data-start=\"1080\" data-end=\"1100\">Safe communication<\/li>\n<li data-section-id=\"rh5v85\" data-start=\"1101\" data-end=\"1137\">Synchronization between goroutines<\/li>\n<li data-section-id=\"clxs5o\" data-start=\"1138\" data-end=\"1167\">Avoids shared memory issues<\/li>\n<\/ul>\n<hr data-start=\"1169\" data-end=\"1172\" \/>\n<h2 data-section-id=\"1fbkigt\" data-start=\"1174\" data-end=\"1232\">4. Difference between buffered and unbuffered channels?<\/h2>\n<h3 data-section-id=\"8dnisf\" data-start=\"1234\" data-end=\"1256\">Unbuffered Channel<\/h3>\n<ul data-start=\"1257\" data-end=\"1301\">\n<li data-section-id=\"x90wtq\" data-start=\"1257\" data-end=\"1301\">Sender waits until receiver receives data.<\/li>\n<\/ul>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">chan<\/span> <span class=\"\u037cm\">int<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<p>\/\/ Create an unbuffered channel of ints<\/p>\n<p>ch := make(chan int)\u00a0\u00a0 \/\/ capacity = 0\u00a0 \u2192 every send blocks until a receive<\/p>\n<p>go func() {<\/p>\n<p>ch &lt;- 42\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ blocks here until the main goroutine receives<\/p>\n<p>}()<\/p>\n<p>v := &lt;-ch\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ unblocks the sender, value is now 42<\/p>\n<p>fmt.Println(v)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Output: 42<\/p>\n<table>\n<thead>\n<tr>\n<td><\/td>\n<td><strong>Unbuffered (make(chan T))<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Capacity<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>Send (ch &lt;- v)<\/td>\n<td><strong>Blocks<\/strong> until some goroutine receives<\/td>\n<\/tr>\n<tr>\n<td>Receive (&lt;-ch)<\/td>\n<td><strong>Blocks<\/strong> until some goroutine sends<\/td>\n<\/tr>\n<tr>\n<td>Typical uses<\/td>\n<td>Hand\u2011off \/ rendezvous, strict sequencing<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><strong>Buffered Channel<\/strong><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"1356\" data-end=\"1395\">\n<li data-section-id=\"ssz59a\" data-start=\"1356\" data-end=\"1395\">Sender can send until buffer is full.<\/li>\n<\/ul>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">chan<\/span> <span class=\"\u037cm\">int<\/span>, <span class=\"\u037cj\">3<\/span>)<\/code><\/div>\n<\/div>\n<div>\n<p>ch &lt;- 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ does not block yet<\/p>\n<p>ch &lt;- 2<\/p>\n<p>ch &lt;- 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ still fine: buffer now full<\/p>\n<p>\/\/ ch &lt;- 4 would block because the buffer is full<\/p>\n<p>fmt.Println(&lt;-ch)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ prints 1, frees one slot<\/p>\n<p>ch &lt;- 4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ now this send succeeds immediately<\/p>\n<p><em>Key points<\/em><\/p>\n<table>\n<thead>\n<tr>\n<td><\/td>\n<td><strong>Buffered (make(chan T, n))<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Capacity<\/td>\n<td>n\u202f&gt;\u202f0<\/td>\n<\/tr>\n<tr>\n<td>Send (ch &lt;- v)<\/td>\n<td>Blocks <strong>only<\/strong> when buffer is full<\/td>\n<\/tr>\n<tr>\n<td>Receive (&lt;-ch)<\/td>\n<td>Blocks <strong>only<\/strong> when buffer is empty<\/td>\n<\/tr>\n<tr>\n<td>Typical uses<\/td>\n<td>Smooth out bursts, worker pools, rate limiting<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Feature<\/strong><\/td>\n<td><strong>Unbuffered Channel<\/strong><\/td>\n<td><strong>Buffered Channel<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Blocking behavior<\/td>\n<td><strong>Sender blocks until receiver receives<\/strong><\/td>\n<td><strong>Sender only blocks when buffer is full<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Synchronization<\/td>\n<td>Provides <strong>direct handoff<\/strong><\/td>\n<td>Acts like a <strong>queue<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Capacity<\/td>\n<td>make(chan T)<\/td>\n<td>make(chan T, N) (N &gt; 0)<\/td>\n<\/tr>\n<tr>\n<td>Common use case<\/td>\n<td>Safe communication &amp; coordination<\/td>\n<td>Asynchronous buffering, pipelines<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"1432\" data-end=\"1435\" \/>\n<h2 data-section-id=\"1eg6mkn\" data-start=\"1437\" data-end=\"1502\">5. What is the difference between concurrency and parallelism?<\/h2>\n<h3 data-section-id=\"8wom6b\" data-start=\"1504\" data-end=\"1519\">Concurrency<\/h3>\n<p data-start=\"1520\" data-end=\"1561\">Handling multiple tasks at the same time.<\/p>\n<h3 data-section-id=\"17xxym2\" data-start=\"1563\" data-end=\"1578\">Parallelism<\/h3>\n<p data-start=\"1579\" data-end=\"1639\">Executing multiple tasks simultaneously using multiple CPUs.<\/p>\n<p data-start=\"1641\" data-end=\"1675\">Go supports both using goroutines.<\/p>\n<hr data-start=\"1677\" data-end=\"1680\" \/>\n<h2 data-section-id=\"1x8ekxf\" data-start=\"1682\" data-end=\"1707\">6. What is GOMAXPROCS?<\/h2>\n<p data-start=\"1709\" data-end=\"1793\"><code data-start=\"1709\" data-end=\"1721\">GOMAXPROCS<\/code> sets the maximum number of OS threads executing Go code simultaneously.<\/p>\n<p data-start=\"1795\" data-end=\"1803\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">runtime<\/span><span class=\"\u037cg\">.<\/span>GOMAXPROCS(<span class=\"\u037cj\">4<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"1837\" data-end=\"1840\" \/>\n<h2 data-section-id=\"1sgqujc\" data-start=\"1842\" data-end=\"1872\">7. What are pointers in Go?<\/h2>\n<p data-start=\"1874\" data-end=\"1906\">Pointers store memory addresses.<\/p>\n<p data-start=\"1908\" data-end=\"1916\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">x<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cj\">10<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">p<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cg\">&amp;<\/span><span class=\"\u037cm\">x<\/span>\r\n\r\n<span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037cg\">*<\/span><span class=\"\u037cm\">p<\/span>)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Notes:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"1972\" data-end=\"2018\">\n<li data-section-id=\"yo3mtw\" data-start=\"1972\" data-end=\"1994\">Go supports pointers<\/li>\n<li data-section-id=\"1xnfe3m\" data-start=\"1995\" data-end=\"2018\">No pointer arithmetic<\/li>\n<\/ul>\n<hr data-start=\"2020\" data-end=\"2023\" \/>\n<h2 data-section-id=\"tend59\" data-start=\"2025\" data-end=\"2083\"><span role=\"text\">8. What is the difference between <code data-start=\"2062\" data-end=\"2069\">new()<\/code> and <code data-start=\"2074\" data-end=\"2082\">make()<\/code>?<\/span><\/h2>\n<h3 data-section-id=\"1ss4m7v\" data-start=\"2085\" data-end=\"2096\"><span role=\"text\"><code data-start=\"2089\" data-end=\"2096\">new()<\/code><\/span><\/h3>\n<p data-start=\"2097\" data-end=\"2134\">Allocates memory and returns pointer.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">p<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">new<\/span>(<span class=\"\u037cm\">int<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><code data-start=\"2165\" data-end=\"2173\">make()<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"2174\" data-end=\"2213\">Initializes slices, maps, and channels.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">m<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">map<\/span>[<span class=\"\u037cm\">string<\/span>]<span class=\"\u037cm\">int<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"2252\" data-end=\"2255\" \/>\n<h2 data-section-id=\"1llonzx\" data-start=\"2257\" data-end=\"2285\">9. What are slices in Go?<\/h2>\n<p data-start=\"2287\" data-end=\"2324\">Slices are dynamic views over arrays.<\/p>\n<p data-start=\"2326\" data-end=\"2334\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">nums<\/span> <span class=\"\u037cg\">:=<\/span> []<span class=\"\u037cm\">int<\/span>{<span class=\"\u037cj\">1<\/span>,<span class=\"\u037cj\">2<\/span>,<span class=\"\u037cj\">3<\/span>}<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Features:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"2381\" data-end=\"2445\">\n<li data-section-id=\"4dicvs\" data-start=\"2381\" data-end=\"2395\">Dynamic size<\/li>\n<li data-section-id=\"17r3j5f\" data-start=\"2396\" data-end=\"2414\">Backed by arrays<\/li>\n<li data-section-id=\"1o91jfs\" data-start=\"2415\" data-end=\"2445\">Contains length and capacity<\/li>\n<\/ul>\n<hr data-start=\"2447\" data-end=\"2450\" \/>\n<h2 data-section-id=\"1nqze22\" data-start=\"2452\" data-end=\"2494\">10. Difference between array and slice?<\/h2>\n<div class=\"TyagGW_tableContainer\">\n<div class=\"group TyagGW_tableWrapper flex flex-col-reverse w-fit\" tabindex=\"-1\">\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"2496\" data-end=\"2618\">\n<thead data-start=\"2496\" data-end=\"2513\">\n<tr data-start=\"2496\" data-end=\"2513\">\n<th class=\"last:pe-10\" data-start=\"2496\" data-end=\"2504\" data-col-size=\"sm\">Array<\/th>\n<th class=\"last:pe-10\" data-start=\"2504\" data-end=\"2513\" data-col-size=\"sm\">Slice<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"2524\" data-end=\"2618\">\n<tr data-start=\"2524\" data-end=\"2553\">\n<td data-start=\"2524\" data-end=\"2537\" data-col-size=\"sm\">Fixed size<\/td>\n<td data-col-size=\"sm\" data-start=\"2537\" data-end=\"2553\">Dynamic size<\/td>\n<\/tr>\n<tr data-start=\"2554\" data-end=\"2585\">\n<td data-start=\"2554\" data-end=\"2567\" data-col-size=\"sm\">Value type<\/td>\n<td data-col-size=\"sm\" data-start=\"2567\" data-end=\"2585\">Reference-like<\/td>\n<\/tr>\n<tr data-start=\"2586\" data-end=\"2618\">\n<td data-start=\"2586\" data-end=\"2606\" data-col-size=\"sm\">Size part of type<\/td>\n<td data-col-size=\"sm\" data-start=\"2606\" data-end=\"2618\">Flexible<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<hr data-start=\"2620\" data-end=\"2623\" \/>\n<h2 data-section-id=\"ogl4e6\" data-start=\"2625\" data-end=\"2675\">11. What is the difference between len and cap?<\/h2>\n<h3 data-section-id=\"7mne3l\" data-start=\"2677\" data-end=\"2686\"><span role=\"text\"><code data-start=\"2681\" data-end=\"2686\">len<\/code><\/span><\/h3>\n<p data-start=\"2687\" data-end=\"2714\">Current number of elements.<\/p>\n<ul>\n<li>The number of elements currently stored in the slice.<\/li>\n<li>It\u2019s the size of the slice you can directly access.<\/li>\n<li>Cannot be negative, always \u2264 capacity.<\/li>\n<\/ul>\n<h3 data-section-id=\"7mf11w\" data-start=\"2716\" data-end=\"2725\"><span role=\"text\"><code data-start=\"2720\" data-end=\"2725\">cap<\/code><\/span><\/h3>\n<p data-start=\"2726\" data-end=\"2763\">Maximum capacity before reallocation.<\/p>\n<ul>\n<li>The total number of elements the slice can hold before needing to allocate more memory.<\/li>\n<li>It\u2019s the maximum size the slice can grow to without reallocating.<\/li>\n<\/ul>\n<ul>\n<li><strong>Length<\/strong> \u2192 how many elements are actually in the slice.<\/li>\n<li><strong>Capacity<\/strong> \u2192 how many elements it can hold before reallocating.<\/li>\n<\/ul>\n<p data-start=\"2765\" data-end=\"2773\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">s<\/span> <span class=\"\u037cg\">:=<\/span> make([]<span class=\"\u037cm\">int<\/span>, <span class=\"\u037cj\">3<\/span>, <span class=\"\u037cj\">5<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n<span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037cm\">len<\/span>(<span class=\"\u037cm\">s<\/span>)) <span class=\"\u037ce\">\/\/ 3<\/span>\r\n<span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037cm\">cap<\/span>(<span class=\"\u037cm\">s<\/span>)) <span class=\"\u037ce\">\/\/ 5<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"2859\" data-end=\"2862\" \/>\n<h2 data-section-id=\"1s0oe2a\" data-start=\"2864\" data-end=\"2897\">12. What are interfaces in Go?<\/h2>\n<p data-start=\"2899\" data-end=\"2926\">Interfaces define behavior.<\/p>\n<p data-start=\"2928\" data-end=\"2936\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">Animal<\/span> <span class=\"\u037cg\">interface<\/span> {<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>    Speak()\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Important:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"3001\" data-end=\"3052\">\n<li data-section-id=\"7v32k5\" data-start=\"3001\" data-end=\"3026\">Implicit implementation<\/li>\n<li data-section-id=\"1ihqgvg\" data-start=\"3027\" data-end=\"3052\">No <code data-start=\"3032\" data-end=\"3044\">implements<\/code> keyword<\/li>\n<\/ul>\n<hr data-start=\"3054\" data-end=\"3057\" \/>\n<h2 data-section-id=\"1p4gid5\" data-start=\"3059\" data-end=\"3093\">13. What is an empty interface?<\/h2>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">interface<\/span>{}<\/code><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">It can hold values of any type.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"3151\" data-end=\"3165\">Equivalent to:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">any<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"3181\" data-end=\"3184\" \/>\n<h2 data-section-id=\"14xous3\" data-start=\"3186\" data-end=\"3216\">14. What is type assertion?<\/h2>\n<p data-start=\"3218\" data-end=\"3264\">Used to extract concrete value from interface.<\/p>\n<p data-start=\"3266\" data-end=\"3274\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">var<\/span> <span class=\"\u037cm\">i<\/span> <span class=\"\u037cg\">interface<\/span>{} <span class=\"\u037cg\">=<\/span> <span class=\"\u037ck\">\"hello\"<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">s<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">i<\/span><span class=\"\u037cg\">.<\/span>(<span class=\"\u037cm\">string<\/span>)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Safe assertion:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">s<\/span>, <span class=\"\u037cm\">ok<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">i<\/span><span class=\"\u037cg\">.<\/span>(<span class=\"\u037cm\">string<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"3378\" data-end=\"3381\" \/>\n<h2 data-section-id=\"13pg4mu\" data-start=\"3383\" data-end=\"3407\">15. What is a struct?<\/h2>\n<p data-start=\"3409\" data-end=\"3443\">Structs group related data fields.<\/p>\n<p data-start=\"3445\" data-end=\"3453\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">User<\/span> <span class=\"\u037cg\">struct<\/span> {<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>    Name <span class=\"\u037cm\">string<\/span>\r\n    Age  <span class=\"\u037cm\">int<\/span>\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"3515\" data-end=\"3518\" \/>\n<h2 data-section-id=\"1ntig5h\" data-start=\"3520\" data-end=\"3555\">16. Does Go support inheritance?<\/h2>\n<p data-start=\"3557\" data-end=\"3599\">Go does not support classical inheritance.<\/p>\n<p data-start=\"3601\" data-end=\"3618\">Instead, it uses:<\/p>\n<ul data-start=\"3619\" data-end=\"3644\">\n<li data-section-id=\"mbgkwg\" data-start=\"3619\" data-end=\"3632\">Composition<\/li>\n<li data-section-id=\"1mqmdyf\" data-start=\"3633\" data-end=\"3644\">Embedding<\/li>\n<\/ul>\n<p data-start=\"3646\" data-end=\"3654\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">Engine<\/span> <span class=\"\u037cg\">struct<\/span> {}<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n<span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">Car<\/span> <span class=\"\u037cg\">struct<\/span> {\r\n    <span class=\"\u037cm\">Engine<\/span>\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"3720\" data-end=\"3723\" \/>\n<h2 data-section-id=\"1ecwzpe\" data-start=\"3725\" data-end=\"3756\">17. What is embedding in Go?<\/h2>\n<p data-start=\"3758\" data-end=\"3821\">Embedding allows one struct to include another struct directly.<\/p>\n<p data-start=\"3823\" data-end=\"3864\">It promotes composition over inheritance.<\/p>\n<hr data-start=\"3866\" data-end=\"3869\" \/>\n<h2 data-section-id=\"1sgtr1\" data-start=\"3871\" data-end=\"3892\">18. What is defer?<\/h2>\n<p data-start=\"3894\" data-end=\"3954\"><code data-start=\"3894\" data-end=\"3901\">defer<\/code> delays execution until surrounding function returns.<\/p>\n<p data-start=\"3956\" data-end=\"3964\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">defer<\/span> <span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037ck\">\"done\"<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Common uses:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"4019\" data-end=\"4070\">\n<li data-section-id=\"198lh8u\" data-start=\"4019\" data-end=\"4034\">Closing files<\/li>\n<li data-section-id=\"1yov3q7\" data-start=\"4035\" data-end=\"4054\">Unlocking mutexes<\/li>\n<li data-section-id=\"1hdlbjq\" data-start=\"4055\" data-end=\"4070\">Cleanup tasks<\/li>\n<\/ul>\n<hr data-start=\"4072\" data-end=\"4075\" \/>\n<h2 data-section-id=\"ghtqso\" data-start=\"4077\" data-end=\"4110\">19. What is panic and recover?<\/h2>\n<h3 data-section-id=\"7xu67n\" data-start=\"4112\" data-end=\"4121\">panic<\/h3>\n<p data-start=\"4122\" data-end=\"4145\">Stops normal execution.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">panic<\/span>(<span class=\"\u037ck\">\"error\"<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">recover<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"4185\" data-end=\"4225\">Catches panic inside deferred functions.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">defer<\/span> <span class=\"\u037cg\">func<\/span>() {\r\n    <span class=\"\u037cm\">recover<\/span>()\r\n}()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"4271\" data-end=\"4274\" \/>\n<h2 data-section-id=\"stkdyi\" data-start=\"4276\" data-end=\"4316\">20. What is garbage collection in Go?<\/h2>\n<p data-start=\"4318\" data-end=\"4380\">Go automatically frees unused memory using garbage collection.<\/p>\n<p data-start=\"4382\" data-end=\"4391\">Benefits:<\/p>\n<ul data-start=\"4392\" data-end=\"4441\">\n<li data-section-id=\"ix5cay\" data-start=\"4392\" data-end=\"4414\">Reduces memory leaks<\/li>\n<li data-section-id=\"1ly21v7\" data-start=\"4415\" data-end=\"4441\">Easier memory management<\/li>\n<\/ul>\n<hr data-start=\"4443\" data-end=\"4446\" \/>\n<h2 data-section-id=\"1xwi14n\" data-start=\"4448\" data-end=\"4471\">21. What is a mutex?<\/h2>\n<p data-start=\"4473\" data-end=\"4504\">Mutex prevents race conditions.<\/p>\n<p data-start=\"4506\" data-end=\"4514\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">var<\/span> <span class=\"\u037cm\">mu<\/span> <span class=\"\u037cm\">sync<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">Mutex<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n<span class=\"\u037cm\">mu<\/span><span class=\"\u037cg\">.<\/span>Lock()\r\n<span class=\"\u037cm\">counter<\/span><span class=\"\u037cg\">++<\/span>\r\n<span class=\"\u037cm\">mu<\/span><span class=\"\u037cg\">.<\/span>Unlock()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"4577\" data-end=\"4580\" \/>\n<h2 data-section-id=\"1ce2htv\" data-start=\"4582\" data-end=\"4614\">22. What is a race condition?<\/h2>\n<p data-start=\"4616\" data-end=\"4703\">When multiple goroutines access shared data simultaneously causing unexpected behavior.<\/p>\n<p data-start=\"4705\" data-end=\"4731\">Go provides race detector:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code>go run <span class=\"\u037cn\">-race<\/span> main.go<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"4766\" data-end=\"4769\" \/>\n<h2 data-section-id=\"18hfdvg\" data-start=\"4771\" data-end=\"4811\">23. What is context package used for?<\/h2>\n<p data-start=\"4813\" data-end=\"4847\">The <code data-start=\"4817\" data-end=\"4826\">context<\/code> package is used for:<\/p>\n<ul data-start=\"4848\" data-end=\"4905\">\n<li data-section-id=\"y9qybj\" data-start=\"4848\" data-end=\"4862\">Cancellation<\/li>\n<li data-section-id=\"cdezk0\" data-start=\"4863\" data-end=\"4873\">Timeouts<\/li>\n<li data-section-id=\"7s9wmr\" data-start=\"4874\" data-end=\"4905\">Passing request-scoped values<\/li>\n<\/ul>\n<p data-start=\"4907\" data-end=\"4915\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">ctx<\/span>, <span class=\"\u037cm\">cancel<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">context<\/span><span class=\"\u037cg\">.<\/span>WithTimeout(<span class=\"\u037cm\">context<\/span><span class=\"\u037cg\">.<\/span>Background(), <span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Second)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">defer<\/span> <span class=\"\u037cm\">cancel<\/span>()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"5012\" data-end=\"5015\" \/>\n<h2 data-section-id=\"10var3j\" data-start=\"5017\" data-end=\"5049\">24. What is select statement?<\/h2>\n<p>In Golang, select is a control statement used to wait on multiple channel operations at the same time.<br \/>\nIt\u2019s like a switch statement, but instead of matching values, it waits until one of the channel operations can proceed.<\/p>\n<p>Key Points<\/p>\n<ul>\n<li>select works only with channels.<\/li>\n<li>It picks one case at random if multiple cases are ready.<\/li>\n<li>Useful for concurrency, timeouts, and non-blocking channel reads\/writes.<\/li>\n<li>It also supports a default case for non-blocking behavior.<\/li>\n<\/ul>\n<p>Basic Syntax<\/p>\n<p>select {<\/p>\n<p>case msg1 := &lt;-ch1:<\/p>\n<p>fmt.Println(&#8220;Received from ch1:&#8221;, msg1)<\/p>\n<p>case msg2 := &lt;-ch2:<\/p>\n<p>fmt.Println(&#8220;Received from ch2:&#8221;, msg2)<\/p>\n<p>case ch3 &lt;- &#8220;data&#8221;:<\/p>\n<p>fmt.Println(&#8220;Sent to ch3&#8221;)<\/p>\n<p>default:<\/p>\n<p>fmt.Println(&#8220;No channel operation ready&#8221;)<\/p>\n<p>}<\/p>\n<p>When to Use select<\/p>\n<ul>\n<li>Waiting for messages from multiple channels.<\/li>\n<li>Implementing timeouts.<\/li>\n<li>Writing non-blocking channel operations.<\/li>\n<li>Coordinating goroutines.<\/li>\n<\/ul>\n<p data-start=\"5051\" data-end=\"5089\"><code data-start=\"5051\" data-end=\"5059\">select<\/code> works with multiple channels.<\/p>\n<p data-start=\"5091\" data-end=\"5099\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">select<\/span> {<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">case<\/span> <span class=\"\u037cm\">msg<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cg\">&lt;-<\/span><span class=\"\u037cm\">ch1<\/span>:\r\n    <span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037cm\">msg<\/span>)\r\n<span class=\"\u037cg\">case<\/span> <span class=\"\u037cg\">&lt;-<\/span><span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>After(<span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Second):\r\n    <span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037ck\">\"timeout\"<\/span>)\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"5221\" data-end=\"5224\" \/>\n<h2 data-section-id=\"1p8pzzd\" data-start=\"5226\" data-end=\"5257\">25. What are packages in Go?<\/h2>\n<p data-start=\"5259\" data-end=\"5291\">Packages organize reusable code.<\/p>\n<p data-start=\"5293\" data-end=\"5319\">Every Go file starts with:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">package<\/span> <span class=\"\u037cm\">main<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"5344\" data-end=\"5347\" \/>\n<h2 data-section-id=\"td8p2b\" data-start=\"5349\" data-end=\"5394\">26. Difference between process and thread?<\/h2>\n<div class=\"TyagGW_tableContainer\">\n<div class=\"group TyagGW_tableWrapper flex flex-col-reverse w-fit\" tabindex=\"-1\">\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"5396\" data-end=\"5566\">\n<thead data-start=\"5396\" data-end=\"5416\">\n<tr data-start=\"5396\" data-end=\"5416\">\n<th class=\"last:pe-10\" data-start=\"5396\" data-end=\"5406\" data-col-size=\"sm\">Process<\/th>\n<th class=\"last:pe-10\" data-start=\"5406\" data-end=\"5416\" data-col-size=\"sm\">Thread<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"5427\" data-end=\"5566\">\n<tr data-start=\"5427\" data-end=\"5486\">\n<td data-start=\"5427\" data-end=\"5456\" data-col-size=\"sm\">Independent execution unit<\/td>\n<td data-col-size=\"sm\" data-start=\"5456\" data-end=\"5486\">Lightweight execution path<\/td>\n<\/tr>\n<tr data-start=\"5487\" data-end=\"5516\">\n<td data-start=\"5487\" data-end=\"5501\" data-col-size=\"sm\">More memory<\/td>\n<td data-start=\"5501\" data-end=\"5516\" data-col-size=\"sm\">Less memory<\/td>\n<\/tr>\n<tr data-start=\"5517\" data-end=\"5566\">\n<td data-start=\"5517\" data-end=\"5541\" data-col-size=\"sm\">Slower context switch<\/td>\n<td data-start=\"5541\" data-end=\"5566\" data-col-size=\"sm\">Faster context switch<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p data-start=\"5568\" data-end=\"5604\">Goroutines are lighter than threads.<\/p>\n<hr data-start=\"5606\" data-end=\"5609\" \/>\n<h2 data-section-id=\"1mib7fx\" data-start=\"5611\" data-end=\"5638\">27. What are maps in Go?<\/h2>\n<p data-start=\"5640\" data-end=\"5667\">Maps store key-value pairs.<\/p>\n<p data-start=\"5669\" data-end=\"5677\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">m<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cg\">map<\/span>[<span class=\"\u037cm\">string<\/span>]<span class=\"\u037cm\">int<\/span>{<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>    <span class=\"\u037ck\">\"a\"<\/span>: <span class=\"\u037cj\">1<\/span>,\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Check existence:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">v<\/span>, <span class=\"\u037cm\">ok<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">m<\/span>[<span class=\"\u037ck\">\"a\"<\/span>]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"5768\" data-end=\"5771\" \/>\n<h2 data-section-id=\"9q99t5\" data-start=\"5773\" data-end=\"5820\">28. Can Go functions return multiple values?<\/h2>\n<p data-start=\"5822\" data-end=\"5826\">Yes.<\/p>\n<p data-start=\"5828\" data-end=\"5836\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">divide<\/span>(<span class=\"\u037cm\">a<\/span>, <span class=\"\u037cm\">b<\/span> <span class=\"\u037cm\">int<\/span>) (<span class=\"\u037cm\">int<\/span>, <span class=\"\u037cm\">error<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"5883\" data-end=\"5886\" \/>\n<h2 data-section-id=\"vbabox\" data-start=\"5888\" data-end=\"5923\">29. What are variadic functions? &amp; how to return multiple variable in variadic function in Golang<\/h2>\n<p data-start=\"5925\" data-end=\"5964\">Functions accepting variable arguments.<\/p>\n<p data-start=\"5966\" data-end=\"5974\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">sum<\/span>(<span class=\"\u037cm\">nums<\/span> ...<span class=\"\u037cm\">int<\/span>)<\/code><\/div>\n<\/div>\n<div>\n<p>In Go you can freely mix <strong>variadic parameters<\/strong> (\u202f&#8230;T\u202f) with <strong>multiple return values<\/strong>.<br \/>\nJust remember:<\/p>\n<ol>\n<li>Only <strong>one<\/strong> parameter\u2014the last one\u2014may be variadic.<\/li>\n<li>Return values are listed in parentheses, and you can optionally give them names.<\/li>\n<li>Callers receive the results as separate variables (not a struct or slice).<\/li>\n<\/ol>\n<p>\/\/ SumAndAvg returns both the sum and the average of any number of ints.<\/p>\n<p>func SumAndAvg(nums &#8230;int) (sum int, avg float64) {<\/p>\n<p>for _, n := range nums {<\/p>\n<p>sum += n<\/p>\n<p>}<\/p>\n<p>if len(nums) &gt; 0 {<\/p>\n<p>avg = float64(sum) \/ float64(len(nums))<\/p>\n<p>}<\/p>\n<p>return \/\/ named returns \u2192 `sum`, `avg` are returned implicitly<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>s, a := SumAndAvg(3, 5, 7, 9)<\/p>\n<p>fmt.Println(&#8220;sum:&#8221;, s, &#8220;avg:&#8221;, a) \/\/ sum: 24 avg: 6<\/p>\n<p>}<\/p>\n<p><strong>Tips &amp; Gotchas<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Topic<\/strong><\/td>\n<td><strong>Note<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Named vs unnamed returns<\/strong><\/td>\n<td>Named results let you return without arguments; unnamed require return v1, v2.<\/td>\n<\/tr>\n<tr>\n<td><strong>Zero arguments<\/strong><\/td>\n<td>A variadic call with no arguments passes an empty slice (len(nums)==0). Handle it if needed.<\/td>\n<\/tr>\n<tr>\n<td><strong>Forwarding<\/strong><\/td>\n<td>Inside another variadic function, forward like OtherFunc(vals&#8230;) \u2014 the trailing &#8230; is required.<\/td>\n<\/tr>\n<tr>\n<td><strong>Performance<\/strong><\/td>\n<td>Variadic parameters are allocated as a slice; if you call in hot loops with only a few items, consider explicit slice arguments instead.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"6008\" data-end=\"6011\" \/>\n<h2 data-section-id=\"1k3lmpg\" data-start=\"6013\" data-end=\"6056\">30. What is dependency management in Go?<\/h2>\n<p data-start=\"6058\" data-end=\"6074\">Go uses modules.<\/p>\n<p data-start=\"6076\" data-end=\"6087\">Initialize:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code>go mod init app<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div><\/div>\n<div class=\"\">Install package:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code>go <span class=\"\u037cl\">get<\/span> package-name<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"6167\" data-end=\"6170\" \/>\n<h1 data-section-id=\"17rt2yo\" data-start=\"6172\" data-end=\"6209\">Advanced Golang Interview Questions<\/h1>\n<h2 data-section-id=\"19ivzqw\" data-start=\"6211\" data-end=\"6242\">31. Explain scheduler in Go.<\/h2>\n<p data-start=\"6244\" data-end=\"6282\">Go scheduler manages goroutines using:<\/p>\n<ul data-start=\"6283\" data-end=\"6330\">\n<li data-section-id=\"1lfyusr\" data-start=\"6283\" data-end=\"6298\">G \u2192 Goroutine<\/li>\n<li data-section-id=\"1sp92n9\" data-start=\"6299\" data-end=\"6314\">M \u2192 OS Thread<\/li>\n<li data-section-id=\"b039ng\" data-start=\"6315\" data-end=\"6330\">P \u2192 Processor<\/li>\n<\/ul>\n<p data-start=\"6332\" data-end=\"6351\">Known as GMP model.<\/p>\n<hr data-start=\"6353\" data-end=\"6356\" \/>\n<h2 data-section-id=\"e43jc9\" data-start=\"6358\" data-end=\"6390\">32. What is channel deadlock?<\/h2>\n<p data-start=\"6392\" data-end=\"6428\">Occurs when goroutines wait forever.<\/p>\n<p data-start=\"6430\" data-end=\"6438\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">chan<\/span> <span class=\"\u037cm\">int<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">&lt;-<\/span> <span class=\"\u037cj\">1<\/span> <span class=\"\u037ce\">\/\/ deadlock<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"6491\" data-end=\"6494\" \/>\n<h2 data-section-id=\"t93p21\" data-start=\"6496\" data-end=\"6526\">33. What is sync.WaitGroup?<\/h2>\n<p data-start=\"6528\" data-end=\"6559\">Waits for goroutines to finish.<\/p>\n<p data-start=\"6561\" data-end=\"6569\">Example:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">var<\/span> <span class=\"\u037cm\">wg<\/span> <span class=\"\u037cm\">sync<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">WaitGroup<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n<span class=\"\u037cm\">wg<\/span><span class=\"\u037cg\">.<\/span>Add(<span class=\"\u037cj\">1<\/span>)\r\n\r\n<span class=\"\u037cg\">go<\/span> <span class=\"\u037cg\">func<\/span>() {\r\n    <span class=\"\u037cg\">defer<\/span> <span class=\"\u037cm\">wg<\/span><span class=\"\u037cg\">.<\/span>Done()\r\n}()\r\n\r\n<span class=\"\u037cm\">wg<\/span><span class=\"\u037cg\">.<\/span>Wait()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"6662\" data-end=\"6665\" \/>\n<h2 data-section-id=\"m1zk9l\" data-start=\"6667\" data-end=\"6702\">34. What are worker pools in Go?<\/h2>\n<p data-start=\"6704\" data-end=\"6774\">Pattern for limiting concurrent goroutines using channels and workers.<\/p>\n<p data-start=\"6776\" data-end=\"6786\">Common in:<\/p>\n<ul data-start=\"6787\" data-end=\"6829\">\n<li data-section-id=\"1j42c77\" data-start=\"6787\" data-end=\"6793\">APIs<\/li>\n<li data-section-id=\"66ew9w\" data-start=\"6794\" data-end=\"6810\">Job processing<\/li>\n<li data-section-id=\"hfwuuw\" data-start=\"6811\" data-end=\"6829\">Background tasks<\/li>\n<\/ul>\n<hr data-start=\"6831\" data-end=\"6834\" \/>\n<h2 data-section-id=\"v0xu37\" data-start=\"6836\" data-end=\"6900\">35. What is the difference between nil slice and empty slice?<\/h2>\n<h3 data-section-id=\"1j5ch25\" data-start=\"6902\" data-end=\"6915\">Nil slice<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">var<\/span> <span class=\"\u037cm\">s<\/span> []<span class=\"\u037cm\">int<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 data-section-id=\"wjc0eb\" data-start=\"6939\" data-end=\"6954\">Empty slice<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">s<\/span> <span class=\"\u037cg\">:=<\/span> []<span class=\"\u037cm\">int<\/span>{}<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"6979\" data-end=\"7026\">Both have length 0, but nil slice equals <code data-start=\"7020\" data-end=\"7025\">nil<\/code>.<\/p>\n<hr data-start=\"7028\" data-end=\"7031\" \/>\n<h2 data-section-id=\"kapkxr\" data-start=\"7033\" data-end=\"7077\">36. What are common Go design principles?<\/h2>\n<ul data-start=\"7079\" data-end=\"7175\">\n<li data-section-id=\"1nnygfh\" data-start=\"7079\" data-end=\"7091\">Simplicity<\/li>\n<li data-section-id=\"1dytcvm\" data-start=\"7092\" data-end=\"7122\">Composition over inheritance<\/li>\n<li data-section-id=\"117bsdl\" data-start=\"7123\" data-end=\"7148\">Explicit error handling<\/li>\n<li data-section-id=\"jc1fvs\" data-start=\"7149\" data-end=\"7175\">Concurrency-first design<\/li>\n<\/ul>\n<hr data-start=\"7177\" data-end=\"7180\" \/>\n<h2><strong>37. what is diff bet timer and ticker in Golang<\/strong><\/h2>\n<p>In Golang, **time.Timer** and **time.Ticker** are both types provided by the time package, but they serve different purposes:<\/p>\n<p>timer := time.NewTimer(5 * time.Second)<\/p>\n<p>&lt;-timer.C<\/p>\n<p>fmt.Println(&#8220;Timer fired after 5 seconds&#8221;)<\/p>\n<ul>\n<li>Fires <strong>only once<\/strong> after 5 seconds.<\/li>\n<li>Useful for <strong>delays<\/strong>, <strong>timeouts<\/strong>, etc.<\/li>\n<\/ul>\n<p><strong>\ud83d\udd01 Resetting:<\/strong><\/p>\n<p>You can reset a timer with .Reset() to reuse it.<\/p>\n<p><strong>Ticker<\/strong><\/p>\n<p>Used to <strong>repeatedly fire events<\/strong> at regular intervals.<\/p>\n<p><strong>\ud83d\udce6 Example:<\/strong><\/p>\n<p>go<\/p>\n<p>CopyEdit<\/p>\n<p>ticker := time.NewTicker(2 * time.Second)<\/p>\n<p>defer ticker.Stop()<\/p>\n<p>for t := range ticker.C {<\/p>\n<p>fmt.Println(&#8220;Tick at&#8221;, t)<\/p>\n<p>}<\/p>\n<ul>\n<li>Fires <strong>every 2 seconds<\/strong> continuously.<\/li>\n<li>Useful for <strong>polling<\/strong>, <strong>periodic tasks<\/strong>, etc.<\/li>\n<\/ul>\n<ul>\n<li><strong>Summary of Differences<\/strong><\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<td><strong>Feature<\/strong><\/td>\n<td><strong>time.Timer<\/strong><\/td>\n<td><strong>time.Ticker<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fires how often<\/td>\n<td>Once after duration<\/td>\n<td>Repeatedly at fixed intervals<\/td>\n<\/tr>\n<tr>\n<td>Channel<\/td>\n<td>&lt;-timer.C<\/td>\n<td>&lt;-ticker.C<\/td>\n<\/tr>\n<tr>\n<td>Use case<\/td>\n<td>Timeouts, delays<\/td>\n<td>Periodic jobs, polling<\/td>\n<\/tr>\n<tr>\n<td>Stop method<\/td>\n<td>timer.Stop()<\/td>\n<td>ticker.Stop()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>38.why use underscore in Golang<\/h2>\n<p>In Go, the <strong>underscore (_)<\/strong> is called the <strong>blank identifier<\/strong>, and it is used as a <strong>&#8220;write-only&#8221; variable<\/strong>. You can assign a value to it, but you <strong>cannot read from it<\/strong>.<\/p>\n<p>When a function returns multiple values and you only care about some of them:<\/p>\n<p>value, _ := someFunction()<\/p>\n<p>Example:<\/p>\n<p>count, _ := fmt.Println(&#8220;Hello&#8221;)<\/p>\n<p>Here, fmt.Println returns (int, error) \u2014 if you don\u2019t care about the error, use _.<\/p>\n<p>If you&#8217;re iterating and don\u2019t need the index or value:<\/p>\n<p>for _, val := range []string{&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;} {<\/p>\n<p>fmt.Println(val)<\/p>\n<p>}<\/p>\n<p><strong>\ud83d\udd12 Why is this useful?<\/strong><\/p>\n<p>Go is <strong>strict<\/strong> \u2014 it does <strong>not allow unused variables or imports<\/strong>, even temporarily.<\/p>\n<p><strong>Summary Table<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Use Case<\/strong><\/td>\n<td><strong>Example<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Ignore extra return values<\/td>\n<td>_, err := someFunc()<\/td>\n<\/tr>\n<tr>\n<td>Import for side effects<\/td>\n<td>import _ &#8220;github.com\/lib\/pq&#8221;<\/td>\n<\/tr>\n<tr>\n<td>Ignore loop index\/value<\/td>\n<td>for _, v := range items<\/td>\n<\/tr>\n<tr>\n<td>Interface compliance check<\/td>\n<td>var _ MyInterface = (*MyType)(nil)<\/td>\n<\/tr>\n<tr>\n<td>Avoid unused variable error<\/td>\n<td>_ = someFunc()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>39.How to define map in Golang<\/h2>\n<p>In Go, a <strong>map<\/strong> is a built-in data type used to store <strong>key-value pairs<\/strong>, similar to dictionaries in Python or objects in JavaScript.<\/p>\n<p>var m map[keyType]valueType<\/p>\n<p>m := make(map[string]int)\u00a0 \/\/ map of string keys to int values<\/p>\n<p>Example:<\/p>\n<p>package main<\/p>\n<p>import &#8220;fmt&#8221;<\/p>\n<p>func main() {<\/p>\n<p>ages := make(map[string]int)<\/p>\n<p>ages[&#8220;Alice&#8221;] = 30<\/p>\n<p>ages[&#8220;Bob&#8221;] = 25<\/p>\n<p>fmt.Println(ages)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\/\/ map[Alice:30 Bob:25]<\/p>\n<p>fmt.Println(ages[&#8220;Bob&#8221;])\u00a0\u00a0 \/\/ 25<\/p>\n<p>}<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Operation<\/strong><\/td>\n<td><strong>Syntax \/ Example<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Declare<\/td>\n<td>var m map[string]int<\/td>\n<\/tr>\n<tr>\n<td>Initialize<\/td>\n<td>m := make(map[string]int)<\/td>\n<\/tr>\n<tr>\n<td>Add value<\/td>\n<td>m[&#8220;key&#8221;] = value<\/td>\n<\/tr>\n<tr>\n<td>Access value<\/td>\n<td>m[&#8220;key&#8221;]<\/td>\n<\/tr>\n<tr>\n<td>Check existence<\/td>\n<td>val, ok := m[&#8220;key&#8221;]<\/td>\n<\/tr>\n<tr>\n<td>Delete key<\/td>\n<td>delete(m, &#8220;key&#8221;)<\/td>\n<\/tr>\n<tr>\n<td>Loop<\/td>\n<td>for k, v := range m { &#8230; }<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>40.How to identify which type of value saving in interface in Golang ex int string<\/h2>\n<h2><strong style=\"font-size: 16px;\">Type Assertion<\/strong><\/h2>\n<p>var i interface{} = &#8220;hello&#8221;<\/p>\n<p>str, ok := i.(string)<\/p>\n<p>if ok {<\/p>\n<p>fmt.Println(&#8220;i is a string:&#8221;, str)<\/p>\n<p>} else {<\/p>\n<p>fmt.Println(&#8220;i is not a string&#8221;)<\/p>\n<p>}<\/p>\n<ul>\n<li>You can also try to assert other types: i.(int), i.(float64), etc.<\/li>\n<li>The ok idiom prevents a panic if the assertion fails.<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<td><strong>Method<\/strong><\/td>\n<td><strong>Use Case<\/strong><\/td>\n<td><strong>Example<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Type assertion<\/td>\n<td>To extract known type<\/td>\n<td>v, ok := i.(string)<\/td>\n<\/tr>\n<tr>\n<td>Type switch<\/td>\n<td>To handle multiple types cleanly<\/td>\n<td>switch v := i.(type) {}<\/td>\n<\/tr>\n<tr>\n<td>reflect.TypeOf()<\/td>\n<td>For logging \/ generic inspection<\/td>\n<td>reflect.TypeOf(i)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>41.what is difference define variable and initialization in Golang<\/h2>\n<p>This means telling the compiler <strong>that a variable exists<\/strong>, along with its type \u2014 <strong>but not giving it a value yet<\/strong>.<\/p>\n<p><strong>\u2705 2. Variable Initialization<\/strong><\/p>\n<p>This means giving the variable a <strong>starting value<\/strong><\/p>\n<h2>42.what is design pattern in Golang<\/h2>\n<p>In Golang, a <strong>design pattern<\/strong> is a <strong>reusable solution to a common software problem or scenario<\/strong>. These patterns are not specific to Go\u2014they come from general software engineering\u2014but they can be implemented in Go idiomatically.<\/p>\n<p>In Golang, a <strong>design pattern<\/strong> is a <strong>reusable solution to a common software problem or scenario<\/strong>. These patterns are not specific to Go\u2014they come from general software engineering\u2014but they can be implemented in Go idiomatically.<\/p>\n<ul>\n<li>Structured ways to solve <strong>recurring problems<\/strong> in software design<\/li>\n<li>Promote <strong>code reusability<\/strong>, <strong>maintainability<\/strong>, and <strong>clarity<\/strong><\/li>\n<li>Common across languages, but Go has its own <strong>minimalistic and idiomatic style<\/strong><\/li>\n<\/ul>\n<ul>\n<li><strong>Categories of Design Patterns<\/strong><\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<td><strong>Category<\/strong><\/td>\n<td><strong>Purpose<\/strong><\/td>\n<td><strong>Common Patterns<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Creational<\/strong><\/td>\n<td>Deal with object creation<\/td>\n<td>Singleton, Factory, Builder, Prototype<\/td>\n<\/tr>\n<tr>\n<td><strong>Structural<\/strong><\/td>\n<td>Deal with composition of objects<\/td>\n<td>Adapter, Decorator, Facade, Proxy<\/td>\n<\/tr>\n<tr>\n<td><strong>Behavioral<\/strong><\/td>\n<td>Deal with communication between objects<\/td>\n<td>Strategy, Observer, Command, State<\/td>\n<\/tr>\n<tr>\n<td><strong>Concurrency<\/strong> (Go-specific)<\/td>\n<td>Handle concurrent behavior<\/td>\n<td>Worker Pool, Pipeline, Fan-In\/Fan-Out<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<ol>\n<li><strong> Singleton Pattern<\/strong><\/li>\n<\/ol>\n<p>Ensures a class has only one instance and provides a global point of access.<\/p>\n<p>package singleton<\/p>\n<p>import &#8220;sync&#8221;<\/p>\n<p>var once sync.Once<\/p>\n<p>var instance *Database<\/p>\n<p>type Database struct {<\/p>\n<p>\/\/ config fields<\/p>\n<p>}<\/p>\n<p>func GetInstance() *Database {<\/p>\n<p>once.Do(func() {<\/p>\n<p>instance = &amp;Database{}<\/p>\n<p>})<\/p>\n<p>return instance<\/p>\n<p>}<\/p>\n<ol start=\"2\">\n<li><strong> Factory Pattern<\/strong><\/li>\n<\/ol>\n<p>Used to create objects without specifying the exact type.<\/p>\n<p>type Shape interface {<\/p>\n<p>Draw()<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>type Circle struct{}<\/p>\n<p>func (c Circle)<\/p>\n<p>Draw()<\/p>\n<p>{<\/p>\n<p>fmt.Println(&#8220;Circle&#8221;)<\/p>\n<p>}<\/p>\n<p>func ShapeFactory(t string) Shape {<\/p>\n<p>switch t {<\/p>\n<p>case &#8220;circle&#8221;:<\/p>\n<p>return Circle{}<\/p>\n<p>}<\/p>\n<p>return nil<\/p>\n<p>}<\/p>\n<ol start=\"3\">\n<li><strong> Strategy Pattern<\/strong><\/li>\n<\/ol>\n<p>Allows switching between algorithms at runtime.<\/p>\n<p>go<\/p>\n<p>CopyEdit<\/p>\n<p>type PaymentStrategy interface {<\/p>\n<p>Pay(amount float64)<\/p>\n<p>}<\/p>\n<p>type CreditCard struct{}<\/p>\n<p>func (cc CreditCard) Pay(amount float64) {<\/p>\n<p>fmt.Println(&#8220;Paid with credit card:&#8221;, amount)<\/p>\n<p>}<\/p>\n<p>type PayPal struct{}<\/p>\n<p>func (pp PayPal) Pay(amount float64) {<\/p>\n<p>fmt.Println(&#8220;Paid with PayPal:&#8221;, amount)<\/p>\n<p>}<\/p>\n<p>func ProcessPayment(strategy PaymentStrategy, amount float64) {<\/p>\n<p>strategy.Pay(amount)<\/p>\n<p>}<\/p>\n<ol start=\"4\">\n<li><strong> Worker Pool (Concurrency Pattern \u2013 Go idiomatic)<\/strong><\/li>\n<\/ol>\n<p>func worker(id int, jobs &lt;-chan int, results chan&lt;- int) {<\/p>\n<p>for j := range jobs {<\/p>\n<p>fmt.Printf(&#8220;worker %d processing job %d\\n&#8221;, id, j)<\/p>\n<p>results &lt;- j * 2<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>\ud83e\udde0 Why Use Design Patterns in Go?<\/strong><\/p>\n<ul>\n<li>Go encourages simplicity and composition over inheritance<\/li>\n<li>Many classic OOP patterns (like inheritance-based ones) are adapted differently in Go<\/li>\n<li>Channels, interfaces, and goroutines provide Go-specific design capabilities<\/li>\n<\/ul>\n<p><strong>\ud83c\udfaf When Should You Use Patterns?<\/strong><\/p>\n<ul>\n<li>When solving a <strong>problem that occurs repeatedly<\/strong><\/li>\n<li>When you want to <strong>structure your code for clarity and reusability<\/strong><\/li>\n<li>When building systems that need <strong>scalability, flexibility, and testability<\/strong><\/li>\n<\/ul>\n<h2>43.How to consider all factors to design new project in Golang like memory management db etc.<\/h2>\n<p><strong>2\u00a0\u202fHigh\u2011Level Architecture<\/strong><\/p>\n<ol>\n<li><strong>Service style<\/strong>\n<ul>\n<li>Monolith, modular monolith, or microservices<\/li>\n<li>Consider <em>Hexagonal \/ Clean Architecture<\/em> for clearer boundaries.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Interface contracts<\/strong>\n<ul>\n<li>gRPC for low\u2011latency internal calls, REST\/JSON for public APIs.<\/li>\n<li>Add OpenAPI or protobuf specs at day\u00a00.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Concurrency model<\/strong>\n<ul>\n<li>Goroutine per request, worker pools, or event loop?<\/li>\n<li>Use context.Context everywhere for cancellation &amp; deadlines.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Data storage decision matrix<\/strong><\/li>\n<\/ol>\n<table>\n<thead>\n<tr>\n<td><strong>Need<\/strong><\/td>\n<td><strong>DB choice<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Strict ACID, complex joins<\/td>\n<td>Postgres\/MySQL<\/td>\n<\/tr>\n<tr>\n<td>Horizontal scale, schema\u2011flexible<\/td>\n<td>MongoDB\/Couchbase<\/td>\n<\/tr>\n<tr>\n<td>Write\u2011heavy, TTL<\/td>\n<td>Redis<\/td>\n<\/tr>\n<tr>\n<td>Analytics<\/td>\n<td>ClickHouse, BigQuery<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>3.1\u00a0\u202fMemory Management &amp; Performance<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Action<\/strong><\/td>\n<td><strong>Detail \/ Tool<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Avoid needless allocations<\/td>\n<td>Reuse buffers (sync.Pool), stream large payloads (io.Reader).<\/td>\n<\/tr>\n<tr>\n<td>Understand escape analysis<\/td>\n<td>go build -gcflags=&#8221;-m&#8221; shows heap vs stack decisions.<\/td>\n<\/tr>\n<tr>\n<td>Profile early<\/td>\n<td>pprof, go tool trace, allocs\/op in benchmarks.<\/td>\n<\/tr>\n<tr>\n<td>GC tuning (rare)<\/td>\n<td>GOGC, SetGCPercent\u2014only after measuring.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>3.2\u00a0\u202fDatabase Handling<\/strong><\/p>\n<ul>\n<li>Use <strong>connection pooling<\/strong> (database\/sql handles this; set MaxOpenConns, MaxIdleConns, ConnMaxLifetime).<\/li>\n<li>Prefer <strong>prepared statements \/ ORM<\/strong> (sqlc, gorm, bun) to prevent SQL injection.<\/li>\n<li><strong>Migrations<\/strong>: golang-migrate, goose.<\/li>\n<li>Wrap DB calls with <strong>context + timeout<\/strong>.<\/li>\n<\/ul>\n<p><strong>3.3\u00a0\u202fError &amp; Panic Strategy<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Layer<\/strong><\/td>\n<td><strong>Approach<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Library<\/td>\n<td>Return error, never panic.<\/td>\n<\/tr>\n<tr>\n<td>Service<\/td>\n<td>Central middleware to translate errors \u2192 HTTP \/ gRPC codes.<\/td>\n<\/tr>\n<tr>\n<td>Logging<\/td>\n<td>Wrap with %w (Go\u00a01.13) for call\u2011site context.<\/td>\n<\/tr>\n<tr>\n<td>Recovery<\/td>\n<td>Gin\/Fiber middlewares or custom defer func() { if r := recover() \u2026 }.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>3.4\u00a0\u202fLogging &amp; Observability<\/strong><\/p>\n<ul>\n<li><strong>Structured logging<\/strong>: uber-go\/zap, rs\/zerolog.<\/li>\n<li><strong>Metrics<\/strong>: prometheus\/client_golang, histogram for latency, counter for errors.<\/li>\n<li><strong>Tracing<\/strong>: OpenTelemetry (go.opentelemetry.io\/otel).<\/li>\n<li><strong>Health checks<\/strong>: \/healthz, \/readyz, plus DB ping.<\/li>\n<\/ul>\n<p><strong>3.5\u00a0\u202fTesting<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Test Type<\/strong><\/td>\n<td><strong>Tool \/ Tip<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Unit<\/td>\n<td>Table\u2011driven tests, go test -run<\/td>\n<\/tr>\n<tr>\n<td>Integration<\/td>\n<td>Spin up real DB with testcontainers\u2011go<\/td>\n<\/tr>\n<tr>\n<td>Benchmarks<\/td>\n<td>go test -bench ., compare with benchstat<\/td>\n<\/tr>\n<tr>\n<td>Fuzzing<\/td>\n<td>go test -fuzz (since Go\u00a01.18)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>4\u00a0\u202fInfrastructure &amp; Deployment<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Area<\/strong><\/td>\n<td><strong>Recommendation<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Dependency management<\/strong><\/td>\n<td>go mod tidy, use semantic\u2011version tags.<\/td>\n<\/tr>\n<tr>\n<td><strong>CI\/CD<\/strong><\/td>\n<td>golangci-lint, go vet, govulncheck -&gt; build -&gt; unit tests -&gt; Docker image.<\/td>\n<\/tr>\n<tr>\n<td><strong>Containerization<\/strong><\/td>\n<td>Multi\u2011stage Dockerfile (scratch or distroless) for tiny images.<\/td>\n<\/tr>\n<tr>\n<td><strong>Runtime configuration<\/strong><\/td>\n<td>12\u2011factor: env vars parsed via spf13\/viper or koanf.<\/td>\n<\/tr>\n<tr>\n<td><strong>Secrets<\/strong><\/td>\n<td>Vault, AWS\u202fSecrets\u202fManager; never bake into images.<\/td>\n<\/tr>\n<tr>\n<td><strong>Kubernetes<\/strong><\/td>\n<td>Readiness\/liveness probes, resource limits, HPA based on custom Prometheus metrics.<\/td>\n<\/tr>\n<tr>\n<td><strong>Rollback<\/strong><\/td>\n<td>Blue\/Green or Canary via Argo\u00a0Rollouts or Flux.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>5\u00a0\u202fSecurity &amp; Compliance<\/strong><\/p>\n<ol>\n<li><strong>Transport security<\/strong> \u2013 TLS everywhere (autocert, ALB termination, or mesh).<\/li>\n<li><strong>AuthN\/Z<\/strong> \u2013 JWT for stateless APIs, OAuth2 for external, mTLS for internal gRPC.<\/li>\n<li><strong>Static analysis<\/strong> \u2013 gosec, staticcheck.<\/li>\n<li><strong>Input validation<\/strong> \u2013 go-playground\/validator, sanitize before DB.<\/li>\n<li><strong>Data retention &amp; encryption<\/strong> \u2013 At\u2011rest (RDS, S3 SSE\u2011KMS) and in\u2011transit.<\/li>\n<\/ol>\n<p><strong>6\u00a0\u202fOperational Excellence<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Concern<\/strong><\/td>\n<td><strong>Go\u2011Friendly Solutions<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Graceful shutdown<\/strong><\/td>\n<td>srv := &amp;http.Server{} + signal.NotifyContext<\/td>\n<\/tr>\n<tr>\n<td><strong>Back\u2011pressure<\/strong><\/td>\n<td>Buffered channels, worker pools, rate limiters (golang.org\/x\/time\/rate).<\/td>\n<\/tr>\n<tr>\n<td><strong>Feature flags<\/strong><\/td>\n<td>go-feature-flag, LaunchDarkly SDK.<\/td>\n<\/tr>\n<tr>\n<td><strong>Alerting<\/strong><\/td>\n<td>Prometheus\u00a0\u2192\u00a0Alertmanager\u00a0\u2192\u00a0PagerDuty \/ Slack.<\/td>\n<\/tr>\n<tr>\n<td><strong>Documentation<\/strong><\/td>\n<td>godoc, OpenAPI\/Swagger UI, ADRs for key decisions.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Starter Skeleton Repo Layout (one suggestion)<\/strong><\/p>\n<p>csharp<\/p>\n<p>CopyEdit<\/p>\n<p>cmd\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ entry points (main packages)<\/p>\n<p>internal\/<\/p>\n<p>app\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ business logic, service layer<\/p>\n<p>db\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ SQL queries or repository layer<\/p>\n<p>transport\/<\/p>\n<p>http\/<\/p>\n<p>grpc\/<\/p>\n<p>pkg\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ reusable libraries<\/p>\n<p>configs\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ YAML\/JSON defaults<\/p>\n<p>scripts\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ CI, dev helpers<\/p>\n<p>deploy\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Helm charts, Terraform<\/p>\n<h2>44. What is diff between select and switch in Golang<\/h2>\n<table>\n<thead>\n<tr>\n<td><strong>Feature<\/strong><\/td>\n<td><strong>switch<\/strong><\/td>\n<td><strong>select<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Used for<\/td>\n<td><strong>Matching values<\/strong><\/td>\n<td><strong>Waiting on multiple channel operations<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Works with<\/td>\n<td>Constants, expressions<\/td>\n<td><strong>Channels only<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Blocks?<\/td>\n<td>\u274c (non-blocking)<\/td>\n<td>\u2705 (blocks if no case is ready)<\/td>\n<\/tr>\n<tr>\n<td>Use case<\/td>\n<td>Control logic, condition handling<\/td>\n<td>Concurrency, goroutine coordination<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>x := 2<\/p>\n<p>switch x {<\/p>\n<p>case 1:<\/p>\n<p>fmt.Println(&#8220;one&#8221;)<\/p>\n<p>case 2:<\/p>\n<p>fmt.Println(&#8220;two&#8221;)<\/p>\n<p>default:<\/p>\n<p>fmt.Println(&#8220;other&#8221;)}<\/p>\n<p><strong>\u2705 select \u2013 Channel-Based Multiplexing<\/strong><\/p>\n<p>Used to <strong>wait on multiple channels<\/strong> at once.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>ch1 := make(chan string)<\/p>\n<p>ch2 := make(chan string)<\/p>\n<p>&nbsp;<\/p>\n<p>go func() { ch1 &lt;- &#8220;from ch1&#8221; }()<\/p>\n<p>go func() { ch2 &lt;- &#8220;from ch2&#8221; }()<\/p>\n<p>&nbsp;<\/p>\n<p>select {<\/p>\n<p>case msg1 := &lt;-ch1:<\/p>\n<p>fmt.Println(&#8220;Received:&#8221;, msg1)<\/p>\n<p>case msg2 := &lt;-ch2:<\/p>\n<p>fmt.Println(&#8220;Received:&#8221;, msg2)<\/p>\n<p>default:<\/p>\n<p>fmt.Println(&#8220;No channel ready&#8221;)<\/p>\n<p>}<\/p>\n<h2>45. what is channels<\/h2>\n<p>In Go, <strong>channels<\/strong> are built-in features that let <strong>goroutines communicate with each other<\/strong> safely and synchronously.<\/p>\n<p>They are a key part of Go\u2019s <strong>concurrency model<\/strong>, enabling you to <strong>pass data between goroutines<\/strong> without using explicit locks or shared memory.<\/p>\n<p><strong>\ud83d\udd01 What is a Channel?<\/strong><\/p>\n<p>A <strong>channel<\/strong> is a pipe through which <strong>one goroutine can send data<\/strong> and <strong>another can receive it<\/strong>.<\/p>\n<p><strong>\ud83d\udce6 Basic Syntax<\/strong><\/p>\n<p>ch := make(chan int) \/\/ creates an unbuffered channel of type int<\/p>\n<p><strong>\ud83e\udde0 How Channels Work<\/strong><\/p>\n<ul>\n<li>ch &lt;- value \u2014 <strong>Send<\/strong> value to channel<\/li>\n<li>value := &lt;-ch \u2014 <strong>Receive<\/strong> value from channel<\/li>\n<li>Communication <strong>blocks<\/strong> until the other side is ready<\/li>\n<\/ul>\n<ul>\n<li><strong>\ud83d\udd00<\/strong><strong> Types of Channels<\/strong><\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<td><strong>Type<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<td><strong>Example<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Unbuffered<\/strong><\/td>\n<td>Sends block until receiver receives<\/td>\n<td>make(chan int)<\/td>\n<\/tr>\n<tr>\n<td><strong>Buffered<\/strong><\/td>\n<td>Allows sending without immediate receive<\/td>\n<td>make(chan int, 5)<\/td>\n<\/tr>\n<tr>\n<td><strong>Receive-only<\/strong><\/td>\n<td>Channel that can only be read<\/td>\n<td>var ch &lt;-chan int<\/td>\n<\/tr>\n<tr>\n<td><strong>Send-only<\/strong><\/td>\n<td>Channel that can only be written to<\/td>\n<td>var ch chan&lt;- int<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>46.Why use mutex<\/h2>\n<p>Because <strong>Go is concurrent<\/strong>, and <strong>shared variables<\/strong> can be accessed <strong>simultaneously<\/strong> by multiple goroutines \u2014 which causes <strong>race conditions<\/strong>, <strong>inconsistent data<\/strong>, or even <strong>crashes<\/strong>.<\/p>\n<p>A <strong>mutex ensures that only one goroutine<\/strong> can access a particular section of code (called a <strong>critical section<\/strong>) at a time.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\ud83d\udd01 When Should You Use a Mutex?<\/strong><\/p>\n<p><strong>Use a mutex when:<\/strong><\/p>\n<ul>\n<li>You are accessing or modifying shared variables (global or heap) from multiple goroutines<\/li>\n<li>You want to synchronize access to a critical section<\/li>\n<\/ul>\n<p><strong>\u26a0\ufe0f What Happens Without a Mutex?<\/strong><\/p>\n<ul>\n<li>Race conditions: Multiple goroutines try to read\/write at the same time<\/li>\n<li>Inconsistent state: One goroutine sees half-written data<\/li>\n<li>Unpredictable bugs: Very hard to reproduce and debug<\/li>\n<\/ul>\n<p><strong>\ud83d\udcda Go Mutex API (in sync package)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Function<\/strong><\/td>\n<td><strong>Purpose<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>mu.Lock()<\/td>\n<td>Blocks if already locked<\/td>\n<\/tr>\n<tr>\n<td>mu.Unlock()<\/td>\n<td>Releases the lock<\/td>\n<\/tr>\n<tr>\n<td>mu.TryLock()<\/td>\n<td>(Go 1.18+) Attempts non-blocking lock<\/td>\n<\/tr>\n<tr>\n<td>sync.RWMutex<\/td>\n<td>Allows multiple readers, one writer<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>47.why use lock and unlock<\/h2>\n<p>Because Go programs are concurrent \u2014 multiple goroutines may try to read\/write shared variables at the same time. Without synchronization, this causes:<\/p>\n<ul>\n<li>Race conditions<\/li>\n<li>Data corruption<\/li>\n<li>Unpredictable bugs<\/li>\n<\/ul>\n<p>Lock() and Unlock() ensure that only one goroutine at a time can access the critical section (the shared resource).<\/p>\n<p>Real Example: Counter Update<\/p>\n<p>\u274c Without Lock (Race Condition)<\/p>\n<p><strong>var count int<\/strong><\/p>\n<p><strong>\u00a0<\/strong><strong>func increment() {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 for i := 0; i &lt; 1000; i++ {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 count++ \/\/ Not thread-safe!<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 }<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>If two goroutines call increment(), they might both read and write count at the same time, leading to wrong results.<\/p>\n<p><strong>\u2705 With Lock (Safe)<\/strong><\/p>\n<p><strong>go<\/strong><\/p>\n<p><strong>CopyEdit<\/strong><\/p>\n<p><strong>var (<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 count int<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 mu\u00a0\u00a0\u00a0 sync.Mutex<\/strong><\/p>\n<p><strong>)<\/strong><\/p>\n<p><strong>\u00a0<\/strong><strong>func increment() {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 for i := 0; i &lt; 1000; i++ {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mu.Lock()\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Acquire lock<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 count++\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Critical section<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mu.Unlock()\u00a0\u00a0\u00a0 \/\/ Release lock<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 }<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<ul>\n<li>mu.Lock() blocks other goroutines until the lock is available.<\/li>\n<li>mu.Unlock() releases the lock so the next goroutine can enter.<\/li>\n<\/ul>\n<p><strong>\u2699\ufe0f When to Use Lock\/Unlock?<\/strong><\/p>\n<p>Use it whenever shared data is being modified or read by multiple goroutines.<\/p>\n<p>Common cases:<\/p>\n<ul>\n<li>Shared counters or state<\/li>\n<li>Writing to a map or slice<\/li>\n<li>Reading &amp; writing to files<\/li>\n<li>Managing shared cache<\/li>\n<\/ul>\n<p><strong>\u26a0\ufe0f Important Notes<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Rule<\/strong><\/td>\n<td><strong>Why it matters<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Always call Unlock() after Lock()<\/td>\n<td>Or you may deadlock the program<\/td>\n<\/tr>\n<tr>\n<td>Use defer mu.Unlock()<\/td>\n<td>Ensures unlock even if a panic or return occurs<\/td>\n<\/tr>\n<tr>\n<td>Only lock what you need<\/td>\n<td>Keep the locked section small for performance<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\ud83e\uddea Example with defer<\/strong><\/p>\n<p><strong>mu.Lock()<\/strong><\/p>\n<p><strong>defer mu.Unlock()<\/strong><\/p>\n<p><strong>\u00a0<\/strong><strong>\/\/ safe code here<\/strong><\/p>\n<p>This ensures Unlock() will always run, even if your function panics or returns early.<\/p>\n<p><strong>\ud83e\udde0 Summary<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Concept<\/strong><\/td>\n<td><strong>Meaning<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Lock()<\/td>\n<td>Waits until it can access safely<\/td>\n<\/tr>\n<tr>\n<td>Unlock()<\/td>\n<td>Releases the lock<\/td>\n<\/tr>\n<tr>\n<td><strong>Race Condition<\/strong><\/td>\n<td><strong>Bug caused by unsynchronized access<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u26a0\ufe0f What Happens If You Use Lock but Not Unlock<\/strong><\/p>\n<p>If you Lock() but forget to Unlock(), your program may:<\/p>\n<ul>\n<li>Deadlock \u2013 the goroutine never releases the lock, and others get stuck waiting forever.<\/li>\n<li>Block indefinitely \u2013 if you&#8217;re using WaitGroup or select, the program may hang.<\/li>\n<\/ul>\n<ul>\n<li>Lock() <strong>acquires the mutex<\/strong>. If another goroutine already holds it, the current one <strong>waits (blocks)<\/strong>.<\/li>\n<li>Unlock() <strong>releases the mutex<\/strong>, allowing another waiting goroutine to proceed.<\/li>\n<\/ul>\n<h2>48. what is race condition<\/h2>\n<p>A race condition in programming occurs when two or more goroutines (or threads) access shared data at the same time, and at least one of them writes to it, leading to unexpected behavior or incorrect results.<\/p>\n<h2>49. what is diff between array and slice<\/h2>\n<table>\n<tbody>\n<tr>\n<td>Array<\/td>\n<td>A fixed-size sequence of elements of the same type. Size is part of its type.<\/td>\n<\/tr>\n<tr>\n<td>Slice<\/td>\n<td>A dynamically-sized, flexible view into an array. Backed by an underlying array.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\ud83d\udd0d Key Differences Between Array and Slice<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Feature<\/strong><\/td>\n<td><strong>Array<\/strong><\/td>\n<td><strong>Slice<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Size<\/td>\n<td>Fixed at compile-time ([3]int)<\/td>\n<td>Dynamic, can grow\/shrink ([]int)<\/td>\n<\/tr>\n<tr>\n<td>Syntax<\/td>\n<td>var a [3]int<\/td>\n<td>var s []int<\/td>\n<\/tr>\n<tr>\n<td>Flexibility<\/td>\n<td>Rigid and less used<\/td>\n<td>Very flexible, commonly used<\/td>\n<\/tr>\n<tr>\n<td>Memory<\/td>\n<td>Stored inline in memory<\/td>\n<td>References an underlying array<\/td>\n<\/tr>\n<tr>\n<td>Passing to func<\/td>\n<td>Passed by value (copy)<\/td>\n<td>Passed by reference (pointer-like)<\/td>\n<\/tr>\n<tr>\n<td>Can grow?<\/td>\n<td>\u274c No<\/td>\n<td>\u2705 Yes (with append())<\/td>\n<\/tr>\n<tr>\n<td>Default value<\/td>\n<td>Zero values<\/td>\n<td>nil slice by default<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>50.what is diff between concurrency and parallelism<\/h2>\n<table>\n<thead>\n<tr>\n<td><strong>Concept<\/strong><\/td>\n<td><strong>Concurrency<\/strong><\/td>\n<td><strong>Parallelism<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Definition<\/strong><\/td>\n<td>Managing <strong>multiple tasks<\/strong> at the same time (can switch between them)<\/td>\n<td><strong>Executing multiple tasks at the same time<\/strong> (on multiple CPUs)<\/td>\n<\/tr>\n<tr>\n<td><strong>Goal<\/strong><\/td>\n<td>Structure &amp; responsiveness<\/td>\n<td>Speed &amp; performance<\/td>\n<\/tr>\n<tr>\n<td><strong>How it works<\/strong><\/td>\n<td>Tasks <strong>take turns<\/strong> (interleaved execution)<\/td>\n<td>Tasks <strong>run truly simultaneously<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>Analogy<\/strong><\/td>\n<td>One chef cooking many dishes by switching tasks<\/td>\n<td>Multiple chefs cooking dishes at once<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uf0b7\u00a0 <strong>Concurrency<\/strong> = multiple <strong>goroutines<\/strong> scheduled by the Go runtime<\/p>\n<p>\uf0b7\u00a0 <strong>Parallelism<\/strong> = goroutines run on <strong>multiple OS threads<\/strong> on multiple CPU cores<\/p>\n<table>\n<thead>\n<tr>\n<td colspan=\"2\"><strong>Feature<\/strong><\/td>\n<td colspan=\"3\"><strong>Description<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td colspan=\"2\">goroutines<\/td>\n<td colspan=\"3\">Enable <strong>concurrency<\/strong><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">GOMAXPROCS<\/td>\n<td colspan=\"3\">Controls number of threads\/CPU cores used (controls parallelism)<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">sync.WaitGroup, channels<\/td>\n<td colspan=\"3\">Help coordinate concurrent tasks<\/td>\n<\/tr>\n<\/tbody>\n<thead>\n<tr>\n<td><strong>Key Point<\/strong><\/td>\n<td colspan=\"2\"><strong>Concurrency<\/strong><\/td>\n<td><strong>Parallelism<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Multiple tasks<\/td>\n<td colspan=\"2\">\u2705<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>At the same <strong>time<\/strong><\/td>\n<td colspan=\"2\">\u274c (interleaved)<\/td>\n<td>\u2705 (truly simultaneous)<\/td>\n<\/tr>\n<tr>\n<td>Needs multiple cores<\/td>\n<td colspan=\"2\">\u274c<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>Achieved with Go<\/td>\n<td colspan=\"2\">\u2705 via goroutines<\/td>\n<td>\u2705 with goroutines + cores<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>51. Difference between rune and string.<\/h2>\n<table>\n<thead>\n<tr>\n<td><strong>Feature<\/strong><\/td>\n<td><strong>string<\/strong><\/td>\n<td><strong>rune<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Type<\/td>\n<td>Immutable []byte (UTF-8)<\/td>\n<td>int32, represents one character<\/td>\n<\/tr>\n<tr>\n<td>Length<\/td>\n<td>Number of bytes (len(str))<\/td>\n<td>Use []rune(str) to get characters<\/td>\n<\/tr>\n<tr>\n<td>Purpose<\/td>\n<td>Full text<\/td>\n<td>Single character \/ Unicode code point<\/td>\n<\/tr>\n<tr>\n<td>Mutable?<\/td>\n<td>\u274c immutable<\/td>\n<td>\u2705 can modify individual runes<\/td>\n<\/tr>\n<tr>\n<td>Useful for<\/td>\n<td>Sentences, names, etc.<\/td>\n<td>Character processing, Unicode logic<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>When to Use What<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Task<\/strong><\/td>\n<td><strong>Use<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Store or pass text<\/td>\n<td>string<\/td>\n<\/tr>\n<tr>\n<td>Work with individual characters<\/td>\n<td>rune<\/td>\n<\/tr>\n<tr>\n<td>Count characters in text<\/td>\n<td>[]rune(str)<\/td>\n<\/tr>\n<tr>\n<td>Manipulate characters (e.g., case)<\/td>\n<td>rune<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>52.What is middleware<\/h2>\n<p>In Golang, particularly when building web applications or APIs (e.g., using frameworks like Fiber, Gin, Echo, or even plain net\/http),<br \/>\na middleware is a function that runs before (or sometimes after) your actual request handler to process or modify the incoming request and\/or outgoing response.<\/p>\n<p><strong>Key Idea<\/strong><\/p>\n<p>Middleware acts like a layer or filter in your application\u2019s request-response cycle.<br \/>\nIt is often used for things like:<\/p>\n<ul>\n<li>Logging requests<\/li>\n<li>Authentication &amp; authorization<\/li>\n<li>Request validation<\/li>\n<li>Adding CORS headers<\/li>\n<li>Rate limiting<\/li>\n<li>Compressing responses<\/li>\n<\/ul>\n<p>Middleware in Go is just a function that wraps another handler, allowing you to intercept requests and responses for things like security, logging, and validation.<\/p>\n<h2><strong>\u00a053. What is wrapper function<\/strong><\/h2>\n<p>In Golang, a <strong>wrapper function<\/strong> is a function that is designed to &#8220;wrap&#8221; another function or piece of logic.<br \/>\nIt\u2019s mainly used to add <strong>extra behavior<\/strong> before, after, or around the execution of the original function \u2014 without modifying that original function\u2019s code.<\/p>\n<p>Go does not have wrapper classes like Java, but we achieve similar functionality using structs, interfaces, and composition. A wrapper in Go typically embeds another type and adds additional behavior such as logging, validation, or transformation. This is similar to the decorator pattern.<\/p>\n<p><strong>Why use wrapper functions?<\/strong><\/p>\n<ul>\n<li><strong>Code reuse<\/strong> \u2192 You can reuse the extra logic across multiple functions.<\/li>\n<li><strong>Separation of concerns<\/strong> \u2192 Keep your core logic clean and put logging, error handling, or pre\/post-processing in the wrapper.<\/li>\n<\/ul>\n<ul>\n<li>Wrapper functions usually <strong>return another function<\/strong>.<\/li>\n<li>Commonly used for <strong>logging<\/strong>, <strong>authentication checks<\/strong>, <strong>timing<\/strong>, or <strong>retries<\/strong>.<\/li>\n<li>In <strong>HTTP servers (like Fiber, Gin)<\/strong>, middleware is often implemented as a type of wrapper function.<\/li>\n<\/ul>\n<h2>54.What is grpc in Golang<\/h2>\n<ul>\n<li><strong>WHY IT IS USED<\/strong><\/li>\n<li>High-performance RPC using HTTP\/2<\/li>\n<li>Uses Protocol Buffers<\/li>\n<li>Use when:<\/li>\n<li>Low latency required<\/li>\n<li>Internal microservices communication.<\/li>\n<\/ul>\n<p>In Go, gRPC is a high-performance, open-source Remote Procedure Call (RPC) framework developed by Google that allows different services (possibly in different languages) to communicate with each other efficiently.<\/p>\n<p>It uses Protocol Buffers (protobuf) as the interface definition language (IDL) for defining services and messages.<\/p>\n<p>Key Features of gRPC in Golang<\/p>\n<ol>\n<li>Language-agnostic \u2013 You can write one service in Go, another in Java, Python, etc., and they can talk to each other.<\/li>\n<li>HTTP\/2 based \u2013 Supports multiplexing, streaming, and better performance than HTTP\/1.1.<\/li>\n<li>Strongly Typed Contracts \u2013 Uses .proto files to define message structures and service methods.<\/li>\n<li>Automatic Code Generation \u2013 You define your service once in .proto, then use protoc to generate Go code for server and client.<\/li>\n<li>Streaming Support \u2013 gRPC supports:\n<ul>\n<li>Unary RPC (single request, single response)<\/li>\n<li>Server streaming (single request, multiple responses)<\/li>\n<li>Client streaming (multiple requests, single response)<\/li>\n<li>Bidirectional streaming (multiple requests &amp; responses)<\/li>\n<\/ul>\n<\/li>\n<li>Efficient Serialization \u2013 Protobuf messages are binary and compact, making them faster than JSON.<\/li>\n<\/ol>\n<h2>55. What is RabbitMQ<\/h2>\n<p>RabbitMQ is an open-source message broker that allows different services, applications, or components to communicate with each other by sending and receiving messages asynchronously.<\/p>\n<p><strong>\ud83d\udccc How It Works<\/strong><\/p>\n<ul>\n<li>Producer \u2192 sends messages to RabbitMQ.<\/li>\n<li>RabbitMQ stores them in a queue.<\/li>\n<li>Consumer \u2192 retrieves and processes messages from the queue.<\/li>\n<\/ul>\n<p>\u26a1 Why Use RabbitMQ?<\/p>\n<ul>\n<li>Decouples services (services don\u2019t call each other directly).<\/li>\n<li>Handles asynchronous processing.<\/li>\n<li>Can buffer requests when consumers are slow.<\/li>\n<li>Provides reliability (messages won\u2019t be lost if a consumer is down).<\/li>\n<li>Supports multiple messaging patterns like Pub\/Sub, Work Queues, Routing, Topics.<\/li>\n<\/ul>\n<p>\ud83d\udee0 Example Use Cases<\/p>\n<ul>\n<li>Sending emails in the background.<\/li>\n<li>Processing heavy tasks (image\/video processing) without blocking API calls.<\/li>\n<li>Microservices communication.<\/li>\n<li>Real-time notifications.<\/li>\n<\/ul>\n<h2>56. If i create 2 reciver channels for diff goroutine is it work?<\/h2>\n<ul>\n<li>Yes, you can create two receiver channels for different goroutines in Go, and it will work \u2014 as long as each channel is being read by only one goroutine at a time.<\/li>\n<li>Here\u2019s the key points you should keep in mind:<\/li>\n<li><\/li>\n<li><strong>1\ufe0f<\/strong><strong>\u20e3<\/strong><strong> Separate channels for different goroutines<\/strong><\/li>\n<li>If you create two different channels and assign each to a separate goroutine, they will work independently without interfering with each other.<br \/>\n<strong>2\ufe0f<\/strong><strong>\u20e3<\/strong><strong> One channel, multiple receivers<\/strong><\/li>\n<li>If you have <strong>one channel<\/strong> and multiple goroutines are reading from it, Go will <strong>distribute messages<\/strong> among them (like load balancing). Only one receiver gets each message.<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a03\ufe0f<\/strong><strong>\u20e3<\/strong><strong> Multiple receivers for same data<\/strong><\/p>\n<p>If you want <strong>both receivers to get the same data<\/strong>, channels won\u2019t do that by default \u2014 you\u2019d need to <strong>fan out<\/strong> the message (send copies) or use something like a <strong>sync.Mutex + shared variable<\/strong>, or a <strong>pub\/sub pattern<\/strong>.<\/p>\n<h2>57. What is Kafka why it is used<\/h2>\n<p>Apache Kafka is an open-source distributed event streaming platform used to handle real-time data pipelines and stream processing at scale.<\/p>\n<p>Think of it like a high-performance postal service for data \u2014 you have producers sending messages, Kafka delivering them reliably, and consumers picking them up whenever they\u2019re ready.<\/p>\n<p><strong>Why Kafka is Used<\/strong><\/p>\n<p>Kafka is mainly used when you need high-throughput, fault-tolerant, and scalable systems for handling streams of data.<\/p>\n<p>Key Uses<\/p>\n<ol>\n<li>Real-time data streaming\n<ul>\n<li>Streaming logs, metrics, transactions, or sensor data in real-time.<\/li>\n<\/ul>\n<\/li>\n<li>Event-driven architecture\n<ul>\n<li>Microservices can communicate asynchronously via events.<\/li>\n<\/ul>\n<\/li>\n<li>Data integration\n<ul>\n<li>Acts as a bridge between different systems (e.g., MySQL \u2192 Kafka \u2192 Elasticsearch).<\/li>\n<\/ul>\n<\/li>\n<li>Log aggregation\n<ul>\n<li>Centralizes logs from different applications\/servers.<\/li>\n<\/ul>\n<\/li>\n<li>Analytics and monitoring\n<ul>\n<li>Processes and analyzes data as it arrives.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>How Kafka Works<\/strong><\/p>\n<ul>\n<li>Producer \u2192 sends messages to Kafka.<\/li>\n<li>Kafka broker \u2192 stores messages in topics.<\/li>\n<li>Consumer \u2192 reads messages from topics.<\/li>\n<li>Messages are stored in partitions for scalability and parallel processing.<\/li>\n<li>Kafka retains messages for a configurable time, so consumers can replay them.<\/li>\n<\/ul>\n<p>Apache Kafka is an open-source distributed event streaming platform used to handle real-time data pipelines and stream processing at scale.<\/p>\n<p>Think of it like a high-performance postal service for data \u2014 you have producers sending messages, Kafka delivering them reliably, and consumers picking them up whenever they\u2019re ready.<\/p>\n<p><strong>Why Kafka is Used<\/strong><\/p>\n<p>Kafka is mainly used when you need high-throughput, fault-tolerant, and scalable systems for handling streams of data.<\/p>\n<p>Key Uses<\/p>\n<ol>\n<li>Real-time data streaming\n<ul>\n<li>Streaming logs, metrics, transactions, or sensor data in real-time.<\/li>\n<\/ul>\n<\/li>\n<li>Event-driven architecture\n<ul>\n<li>Microservices can communicate asynchronously via events.<\/li>\n<\/ul>\n<\/li>\n<li>Data integration\n<ul>\n<li>Acts as a bridge between different systems (e.g., MySQL \u2192 Kafka \u2192 Elasticsearch).<\/li>\n<\/ul>\n<\/li>\n<li>Log aggregation\n<ul>\n<li>Centralizes logs from different applications\/servers.<\/li>\n<\/ul>\n<\/li>\n<li>Analytics and monitoring\n<ul>\n<li>Processes and analyzes data as it arrives.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>How Kafka Works<\/p>\n<ul>\n<li>Producer \u2192 sends messages to Kafka.<\/li>\n<li>Kafka broker \u2192 stores messages in topics.<\/li>\n<li>Consumer \u2192 reads messages from topics.<\/li>\n<li>Messages are stored in partitions for scalability and parallel processing.<\/li>\n<li>Kafka retains messages for a configurable time, so consumers can replay them.<\/li>\n<\/ul>\n<h2>58. what is cors<\/h2>\n<p>CORS stands for Cross-Origin Resource Sharing \u2014 it\u2019s a security mechanism built into web browsers that controls whether a web page can request resources (like APIs, images, fonts) from a different origin than the one it was loaded from.<\/p>\n<p>Key Idea<\/p>\n<ul>\n<li>Origin = combination of protocol, domain, and port.<br \/>\nExample:<\/li>\n<\/ul>\n<ul>\n<li>By default, browsers block cross-origin requests to protect users from malicious sites.<\/li>\n<li>CORS is a way for the server to tell the browser,<br \/>\n\u201cIt\u2019s okay to load this resource from another origin.\u201d<\/li>\n<\/ul>\n<p>How It Works<\/p>\n<ol>\n<li>Browser sends a request (sometimes a \u201cpreflight\u201d OPTIONS request first) to the server.<\/li>\n<li>Server responds with special HTTP headers like:<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Access-Control-Allow-Origin: https:\/\/myfrontend.com<\/p>\n<p>Access-Control-Allow-Methods: GET, POST<\/p>\n<p>Access-Control-Allow-Headers: Content-Type<\/p>\n<ol>\n<li>If the headers match the browser\u2019s expectations, the browser allows the request.<\/li>\n<\/ol>\n<p>Why It\u2019s Used<\/p>\n<ul>\n<li>To allow safe communication between frontend (on one domain) and backend (on another domain).<\/li>\n<li>To prevent malicious websites from making unauthorized requests on behalf of a user.<\/li>\n<\/ul>\n<h2>59.What we used security purpose for rest Api<\/h2>\n<p>For securing a REST API in Golang (or any backend), you typically combine multiple security mechanisms, since there\u2019s no single \u201cone-size-fits-all\u201d method.<br \/>\nHere\u2019s a structured breakdown:<\/p>\n<ol>\n<li>Authentication (Who you are)<\/li>\n<\/ol>\n<ul>\n<li>JWT (JSON Web Token) \u2014 Common for stateless authentication.<\/li>\n<li>OAuth 2.0 \u2014 Used when integrating with third-party logins (Google, GitHub).<\/li>\n<li>API Keys \u2014 For simple service-to-service authentication.<\/li>\n<li>Basic Auth \u2014 Very simple, but generally avoided for production unless over HTTPS.<\/li>\n<\/ul>\n<ol start=\"2\">\n<li>Authorization (What you can do)<\/li>\n<\/ol>\n<ul>\n<li>Role-Based Access Control (RBAC) \u2014 Example: Admin, User, Assessor, Requestor.<\/li>\n<li>Attribute-Based Access Control (ABAC) \u2014 More fine-grained based on user attributes.<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>Transport Security<\/li>\n<\/ol>\n<ul>\n<li>HTTPS\/TLS \u2014 Encrypts data in transit to prevent sniffing or MITM attacks.<\/li>\n<\/ul>\n<ol start=\"4\">\n<li>Input Validation &amp; Sanitization<\/li>\n<\/ol>\n<ul>\n<li>Validate request body, query params, headers, and URL inputs.<\/li>\n<li>Use proper types, regex, or libraries to prevent SQL injection and XSS.<\/li>\n<\/ul>\n<ol start=\"5\">\n<li>CORS Protection<\/li>\n<\/ol>\n<ul>\n<li>Configure allowed origins, methods, and headers to prevent unauthorized cross-domain requests.<\/li>\n<\/ul>\n<ol start=\"6\">\n<li>Rate Limiting &amp; Throttling<\/li>\n<\/ol>\n<ul>\n<li>Prevents brute force attacks and API abuse.<\/li>\n<li>Example libraries: golang.org\/x\/time\/rate, or reverse proxy level with Nginx\/Cloudflare.<\/li>\n<\/ul>\n<ol start=\"7\">\n<li>Logging &amp; Monitoring<\/li>\n<\/ol>\n<ul>\n<li>Log requests, authentication failures, and unusual activity.<\/li>\n<li>Use tools like Prometheus, Grafana, or ELK stack for monitoring.<\/li>\n<\/ul>\n<ol start=\"8\">\n<li>Data Protection<\/li>\n<\/ol>\n<ul>\n<li>Store passwords hashed with bcrypt\/argon2.<\/li>\n<li>Avoid storing sensitive data in plaintext (e.g., use encryption at rest).<\/li>\n<\/ul>\n<ol start=\"9\">\n<li>Middleware-Based Security<\/li>\n<\/ol>\n<p>In Gin or Fiber, you can add middleware for:<\/p>\n<ul>\n<li>Authentication check before hitting the main handler.<\/li>\n<li>Request validation.<\/li>\n<li>Error recovery to avoid leaking stack traces.<\/li>\n<\/ul>\n<p>Example (Gin JWT middleware):<\/p>\n<p>func AuthMiddleware() gin.HandlerFunc {<\/p>\n<p>return func(c *gin.Context) {<\/p>\n<p>token := c.GetHeader(&#8220;Authorization&#8221;)<\/p>\n<p>if !ValidateJWT(token) {<\/p>\n<p>c.JSON(http.StatusUnauthorized, gin.H{&#8220;error&#8221;: &#8220;Unauthorized&#8221;})<\/p>\n<p>c.Abort()<\/p>\n<p>return<\/p>\n<p>}<\/p>\n<p>c.Next()<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><strong>\u2705<\/strong><strong> Best Practice \u2192 Use multiple layers together:<br \/>\nHTTPS + JWT\/OAuth + RBAC + Rate Limiting + Input Validation + Logging.<\/strong><\/p>\n<h2>60.what is monolithic and microservice application what is diff between them.<\/h2>\n<p>A monolithic application and a microservice application are two different ways to structure software architecture.<\/p>\n<p><strong>Monolithic Application<\/strong><\/p>\n<ul>\n<li>Definition:<br \/>\nA single, unified application where all components (UI, business logic, database access, etc.) are packaged and deployed together.<\/li>\n<li>Example:<\/li>\n<\/ul>\n<ul>\n<li>A single .jar, .war, or binary file containing the whole system.<\/li>\n<li>Characteristics:\n<ul>\n<li>One codebase, one deployment unit.<\/li>\n<li>All modules share the same memory and database.<\/li>\n<li>Scaling requires deploying the whole app again.<\/li>\n<li>Tight coupling between components.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Microservice Application<\/strong><\/p>\n<ul>\n<li>Definition:<br \/>\nAn application is split into multiple small, independent services, each responsible for a specific business function.<\/li>\n<li>Example:<br \/>\nAn e-commerce site where &#8220;Order Service,&#8221; &#8220;Payment Service,&#8221; and &#8220;Inventory Service&#8221; are separate deployable services.<\/li>\n<li>Characteristics:\n<ul>\n<li>Each service has its own codebase, database, and deployment.<\/li>\n<li>Services communicate via APIs (REST, gRPC, Kafka, RabbitMQ, etc.).<\/li>\n<li>Easy to scale individual services.<\/li>\n<li>Loose coupling and independent deployment.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Key Differences Between Monolithic and Microservices<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td>Aspect<\/td>\n<td>Monolithic<\/td>\n<td>Microservices<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Codebase<\/td>\n<td>Single<\/td>\n<td>Multiple (per service)<\/td>\n<\/tr>\n<tr>\n<td>Deployment<\/td>\n<td>One deployment for entire app<\/td>\n<td>Independent deployment per service<\/td>\n<\/tr>\n<tr>\n<td>Scalability<\/td>\n<td>Whole app scales together<\/td>\n<td>Scale individual services<\/td>\n<\/tr>\n<tr>\n<td>Database<\/td>\n<td>Usually one shared database<\/td>\n<td>Each service can have its own DB<\/td>\n<\/tr>\n<tr>\n<td>Coupling<\/td>\n<td>Tightly coupled<\/td>\n<td>Loosely coupled<\/td>\n<\/tr>\n<tr>\n<td>Technology Stack<\/td>\n<td>Usually one<\/td>\n<td>Can be different for each service<\/td>\n<\/tr>\n<tr>\n<td>Fault Isolation<\/td>\n<td>A bug can crash the whole app<\/td>\n<td>A bug affects only that service<\/td>\n<\/tr>\n<tr>\n<td>Development Speed<\/td>\n<td>Slower for large teams<\/td>\n<td>Faster with small teams per service<\/td>\n<\/tr>\n<tr>\n<td>Complexity<\/td>\n<td>Simple to start but harder to maintain at scale<\/td>\n<td>More complex to start but easier to maintain at scale<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>61. What is callback function<\/h2>\n<p>A callback function is a function that is passed as an argument to another function and is intended to be executed later, usually after some task is completed.<\/p>\n<p>It\u2019s a way to tell a function,<\/p>\n<p>\u201cWhen you\u2019re done, call <em>this<\/em> function for me.\u201d<\/p>\n<p>\ud83d\udd39 In Golang<\/p>\n<p>Go supports callbacks by passing function variables around.<\/p>\n<p>Example:<\/p>\n<p>package main<\/p>\n<p>import &#8220;fmt&#8221;<\/p>\n<p>\/\/ This function takes another function as an argument<\/p>\n<p>func processData(data string, callback func(string)) {<\/p>\n<p>fmt.Println(&#8220;Processing:&#8221;, data)<\/p>\n<p>callback(data) \/\/ Call the callback after processing<\/p>\n<p>}<\/p>\n<p>\/\/ Callback function<\/p>\n<p>func printResult(result string) {<\/p>\n<p>fmt.Println(&#8220;Processed result:&#8221;, result)<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>processData(&#8220;Hello Golang&#8221;, printResult)<\/p>\n<p>}<\/p>\n<p><strong>\ud83d\udccc<\/strong><strong> Why use callbacks?<\/strong><\/p>\n<ol>\n<li>Asynchronous handling \u2013 e.g., run a function after data is fetched or a task finishes.<\/li>\n<li>Code flexibility \u2013 behavior can change depending on which callback is passed.<\/li>\n<li>Separation of concerns \u2013 processing logic and \u201cwhat to do next\u201d are separated<strong>.<\/strong><\/li>\n<\/ol>\n<h2>62.What is polymorphism how to achieve in Golang using interface<\/h2>\n<p>Polymorphism is an object-oriented programming concept where different types can be accessed through the same interface, and each type can provide its own implementation of the behavior.<\/p>\n<p>In simple terms:<\/p>\n<p>The same action (method call) can behave differently depending on the object that is calling it.<\/p>\n<p><strong>\ud83d\udd39<\/strong><strong> How Polymorphism Works in Go<\/strong><\/p>\n<p>Go doesn\u2019t have classes, but it supports polymorphism through interfaces.<br \/>\nIf a type implements all the methods of an interface, it implicitly satisfies that interface \u2014 no explicit \u201cimplements\u201d keyword is needed.<\/p>\n<p>Key Points in Go\u2019s Polymorphism:<\/p>\n<ol>\n<li>No inheritance \u2014 just interfaces and struct methods.<\/li>\n<li>Implicit implementation \u2014 no implements keyword.<\/li>\n<\/ol>\n<p>Dynamic behavior \u2014 method execution depends on the actual type at runtime.<\/p>\n<h2>63.What is sweep &amp; mark in garbage collection in Golang<\/h2>\n<p>Go\u2019s GC is mark-and-sweep based.<\/p>\n<ul>\n<li>Mark phase \u2192 The GC finds all objects that are still <em>reachable<\/em> (in use) and marks them.<\/li>\n<li>Sweep phase \u2192 The GC goes through the heap and frees memory occupied by unmarked (unreachable) objects, making that memory available for future allocations.<\/li>\n<\/ul>\n<p>Think of it like cleaning your desk:<\/p>\n<ol>\n<li>Mark: Tag the papers you still need.<\/li>\n<li>Sweep: Throw away the untagged ones.<\/li>\n<\/ol>\n<p>\ud83d\udccc In Go:<\/p>\n<ul>\n<li>Sweep doesn\u2019t <em>compact<\/em> memory (no moving objects around).<\/li>\n<li>Freed memory is put back into Go\u2019s internal memory allocator for reuse.<\/li>\n<\/ul>\n<p>2\ufe0f\u20e3 make in Go<\/p>\n<p>make is not part of garbage collection \u2014 it\u2019s a built-in function used to allocate and initialize certain built-in data types:<\/p>\n<ul>\n<li>slices<\/li>\n<li>maps<\/li>\n<li>channels<\/li>\n<\/ul>\n<p>Unlike new (which only allocates memory), make also:<\/p>\n<ul>\n<li>Sets up internal data structures (like slice headers, hash tables for maps, or channel buffers).<\/li>\n<li>Returns a value ready to use.<\/li>\n<\/ul>\n<p>How They Relate<\/p>\n<ul>\n<li>Sweep \u2192 Happens automatically in GC to reclaim unused memory.<\/li>\n<li>make \u2192 Explicitly called by your code to allocate and initialize special Go data structures.<\/li>\n<li>After you stop using what you made (e.g., slice, map, channel) and no references remain, GC will mark it unused and sweep it away in future cycles.<\/li>\n<\/ul>\n<h2>64. What is a Closure Function in Go?<\/h2>\n<p>A closure is a function that <em>captures<\/em> variables from the scope where it was defined, and keeps them alive even after that scope has ended.<\/p>\n<p>It\u2019s basically:<\/p>\n<p>\u201cA function with a memory \u2014 it remembers the variables from the place it was born.\u201d<\/p>\n<p><strong>Example of a Closure<\/strong><\/p>\n<p><strong>package main<\/strong><\/p>\n<p><strong>\u00a0<\/strong><strong>import &#8220;fmt&#8221;<\/strong><\/p>\n<p><strong>\u00a0<\/strong><strong>func counter() func() int {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 count := 0<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 return func() int {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 count++<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return count<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 }<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p><strong>\u00a0<\/strong><strong>func main() {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 c1 := counter()<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 fmt.Println(c1()) \/\/ 1<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 fmt.Println(c1()) \/\/ 2<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 fmt.Println(c1()) \/\/ 3<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 c2 := counter()<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0 fmt.Println(c2()) \/\/ 1\u00a0 (separate memory)<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>Key Points:<\/p>\n<ul>\n<li>The inner function <em>remembers<\/em> count from the outer function\u2019s scope.<\/li>\n<li>Even though counter() finished, count still lives because the closure keeps it alive.<\/li>\n<\/ul>\n<h2><strong>65.What are Generics in Go?<\/strong><\/h2>\n<ul>\n<li><strong>Generics<\/strong> allow you to write <strong>functions and data structures<\/strong> that work with different <strong>types<\/strong> while keeping <strong>type safety<\/strong>.<\/li>\n<li>Before Go 1.18, developers used interface{} (or any) to achieve type flexibility, but it lost compile-time type safety.<\/li>\n<li>With generics, you can <strong>parameterize types<\/strong> using <strong>type parameters<\/strong> (like templates in C++ or generics in Java).<\/li>\n<\/ul>\n<p><strong>\ud83d\udd39<\/strong><strong> Which version introduced Generics?<\/strong><\/p>\n<p>\ud83d\udc49 <strong>Generics were introduced in Go 1.18 (released in March 2022).<\/strong><\/p>\n<p><strong>Key Points<\/strong><\/p>\n<ul>\n<li>Introduced in <strong>Go 1.18<\/strong>.<\/li>\n<li>Uses <strong>type parameters in square brackets [T any]<\/strong>.<\/li>\n<li>Supports <strong>constraints<\/strong> (int | float64 or custom interfaces).<\/li>\n<li>Increases <strong>code reusability<\/strong> with <strong>type safety<\/strong>.<\/li>\n<\/ul>\n<h2>66.Can we pass parameters to main() in Go?<\/h2>\n<ul>\n<li>In <strong>Go<\/strong>, the main() function <strong>cannot accept parameters<\/strong><\/li>\n<\/ul>\n<p>func main() {<\/p>\n<p>\/\/ os.Args holds all command-line arguments<\/p>\n<p>fmt.Println(&#8220;All args:&#8221;, os.Args)<\/p>\n<p>if len(os.Args) &gt; 1 {<\/p>\n<p>fmt.Println(&#8220;First arg:&#8221;, os.Args[1])<\/p>\n<p>}<\/p>\n<p>}<br \/>\ngo run main.go hello world<\/p>\n<p>O\/p<\/p>\n<p>All args: [main hello world]<\/p>\n<p>First arg: hello<\/p>\n<p>os.Args[0] \u2192 program name (e.g., main).<\/p>\n<p>os.Args[1:] \u2192 actual command-line parameters.<\/p>\n<p>You can pass any number of parameters.<\/p>\n<h2>67. How to Avoid Memory Leaks in Go<\/h2>\n<ul>\n<li>pprof<\/li>\n<li>Monitor goroutines<\/li>\n<li>Check unclosed channels<\/li>\n<\/ul>\n<p>Close Goroutines Properly<\/p>\n<ul>\n<li>If a goroutine is waiting forever (e.g., on a channel that never sends), it leaks.<br \/>\n\u2705 Always provide a way to stop goroutines using context cancellation or done channels.<\/li>\n<\/ul>\n<p>Close Channels<\/p>\n<ul>\n<li>Unclosed channels may cause blocked senders\/receivers \u2192 memory leak.<br \/>\n\u2705 Close channels when no longer needed.<\/li>\n<\/ul>\n<p>Avoid Holding References Unnecessarily<\/p>\n<ul>\n<li>If a large object is stored in a global variable or slice but no longer needed, it prevents GC.<br \/>\n\u2705 Set it to nil when done.<\/li>\n<\/ul>\n<p>Use defer to Release Resources<\/p>\n<ul>\n<li>Files, DB connections, network sockets \u2192 always close them.<\/li>\n<\/ul>\n<p>Question: What is Composition in Go?<\/p>\n<ul>\n<li>Composition is a way to combine behaviors by embedding one struct inside another.<\/li>\n<li>Instead of inheritance (\u201cis-a\u201d relationship), Go uses composition (\u201chas-a\u201d relationship).<\/li>\n<li>It allows code reuse without a rigid class hierarchy.<\/li>\n<\/ul>\n<p>Key Points for Interview<\/p>\n<ol>\n<li>Go does not support inheritance \u2192 uses composition.<\/li>\n<li>Composition is done via struct embedding.<\/li>\n<li>Promotes \u201chas-a\u201d relationship instead of \u201cis-a\u201d.<\/li>\n<li>Helps in code reuse and flexible design.<\/li>\n<\/ol>\n<h2>68. What is composition in Go?<\/h2>\n<p><em>Composition in Go is achieved by embedding structs or interfaces within other structs, enabling code reuse and polymorphic behavior without inheritance.<\/em><\/p>\n<ul>\n<li>Question: Why Use Empty Struct in Go?<br \/>\nEmpty struct (struct{}) takes 0 bytes of memory.<\/li>\n<li>Uses:\n<ol>\n<li>As a map value for sets.<\/li>\n<li>As a signal in channels.<\/li>\n<li>As a placeholder type.<\/li>\n<li>In concurrency utilities for efficiency.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>\ud83d\udc49 So if asked: <em>\u201cWhy use empty struct in Go?\u201d<\/em><br \/>\n\u2705 Answer: <em>Because it has zero size, making it memory-efficient for cases where only presence or signaling matters, not the actual data.<\/em><\/p>\n<p>Example:<\/p>\n<p>Strings = [&#8220;sumita&#8221;, &#8220;amit&#8221;, &#8220;mahesh&#8221;]<\/p>\n<p>Output = [&#8220;m&#8221;, &#8220;a&#8221;]<\/p>\n<p>Here\u2019s a working Go program:<\/p>\n<p>package main<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;strings&#8221;<\/p>\n<p>)<\/p>\n<p>func commonChars(words []string) []string {<\/p>\n<p>if len(words) == 0 {<\/p>\n<p>return []string{}<\/p>\n<p>}<\/p>\n<p>\/\/ Take first word as base<\/p>\n<p>common := make(map[rune]int)<\/p>\n<p>for _, ch := range words[0] {<\/p>\n<p>common[ch]++<\/p>\n<p>}<\/p>\n<p>\/\/ Compare with each next word<\/p>\n<p>for _, word := range words[1:] {<\/p>\n<p>temp := make(map[rune]int)<\/p>\n<p>for _, ch := range word {<\/p>\n<p>if common[ch] &gt; 0 {<\/p>\n<p>temp[ch]++<\/p>\n<p>if temp[ch] &gt; common[ch] {<\/p>\n<p>temp[ch] = common[ch]<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>common = temp<\/p>\n<p>}<\/p>\n<p>\/\/ Collect results<\/p>\n<p>var result []string<\/p>\n<p>for ch, count := range common {<\/p>\n<p>for i := 0; i &lt; count; i++ {<\/p>\n<p>result = append(result, string(ch))<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>return result<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>words := []string{&#8220;sumita&#8221;, &#8220;amit&#8221;, &#8220;mahesh&#8221;}<\/p>\n<p>result := commonChars(words)<\/p>\n<p>fmt.Println(result) \/\/ [&#8220;m&#8221;, &#8220;a&#8221;]<\/p>\n<p>}<\/p>\n<h2><strong>69.What is reflection in Golang<\/strong><\/h2>\n<p><strong>Go provides reflection through the reflect package.<\/strong><\/p>\n<p>. Why Reflection?<\/p>\n<p>Normally in Go, types are known at compile time. But sometimes you want to:<\/p>\n<ul>\n<li>Inspect an unknown type at runtime (e.g., in a JSON library, ORM like GORM, or serialization).<\/li>\n<li>Dynamically access struct fields, methods, or values.<\/li>\n<li>Write generic utilities (like logging, validation, data mapping).<\/li>\n<\/ul>\n<p>\ud83d\udd39 Key Concepts in reflect<\/p>\n<ol>\n<li>reflect.TypeOf(i) \u2192 returns the type of a variable.<\/li>\n<li>reflect.ValueOf(i) \u2192 returns the value of a variable.<\/li>\n<\/ol>\n<ol start=\"3\">\n<li><strong>Kind<\/strong> \u2192 the underlying category (int, string, slice, struct, etc.).<\/li>\n<li><strong>Set<\/strong> \u2192 you can change values if the reflect.Value is addressable and exported.<\/li>\n<\/ol>\n<h2>70. How to set limit for goroutine in project<\/h2>\n<p>In Go, goroutines are lightweight, but unlimited spawning can overload memory\/CPU.\u00a0\u00a0\u00a0 So, we often set a limit (worker pool pattern) to control concurrency.<\/p>\n<p>Here are common ways to do it:<\/p>\n<p>\ud83d\udd39 1. Use a Buffered Channel as a Semaphore<\/p>\n<ol start=\"2\">\n<li>Worker Pool Pattern<br \/>\n\uf0b7 Small projects \/ simple APIs \u2192 Use semaphore (channel).<\/li>\n<\/ol>\n<p>\uf0b7\u00a0 Batch processing \/ pipelines \u2192 Use worker pool.<\/p>\n<p>\uf0b7\u00a0 Structured concurrency \u2192 Use errgroup with SetLimit().<\/p>\n<h2>71. What is pointer<\/h2>\n<p>In Go, a pointer is a variable that stores the memory address of another variable.<br \/>\nInstead of holding the actual value, it points to where that value is stored in memory.<\/p>\n<p>\ud83d\udd39 Basics<\/p>\n<ul>\n<li>A pointer type is written with *T, where T is the type it points to.<\/li>\n<li>The address-of operator (&amp;) is used to get the memory address of a variable.<\/li>\n<li>The dereference operator (*) is used to access the value stored at that address.<\/li>\n<\/ul>\n<p><strong>Example<\/strong><\/p>\n<p>package main<\/p>\n<p>import &#8220;fmt&#8221;<\/p>\n<p>func main() {<\/p>\n<p>x := 10\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ normal integer variable<\/p>\n<p>p := &amp;x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ p is a pointer to x (type *int)<\/p>\n<p>fmt.Println(&#8220;x =&#8221;, x)\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 10<\/p>\n<p>fmt.Println(&#8220;p =&#8221;, p)\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ memory address of x<\/p>\n<p>fmt.Println(&#8220;*p =&#8221;, *p)\u00a0\u00a0\u00a0 \/\/ value at that memory address (10)<\/p>\n<p>\/\/ change value using pointer<\/p>\n<p>*p = 20<\/p>\n<p>fmt.Println(&#8220;x after change =&#8221;, x) \/\/ 20<\/p>\n<p>}<\/p>\n<h2><span style=\"font-size: 12.0pt; line-height: 115%;\">72.What is GOROOT in Go?<\/span><\/h2>\n<ul>\n<li>GOROOT is an environment variable in Go that tells your system where the Go SDK (compiler, tools, standard library) is installed.<\/li>\n<\/ul>\n<p>In simple words: it\u2019s the root directory of the Go installation.<\/p>\n<h2>73.what is wrap &amp; unwrap error<\/h2>\n<p>&nbsp;<\/p>\n<p>In Go 1.13+, the standard library introduced error wrapping and unwrapping so you can attach extra context to errors while still keeping track of the original cause.<br \/>\nWrapping Errors<\/p>\n<p>When you return an error, you can wrap it with more context using fmt.Errorf and the special %w verb.<\/p>\n<p>package main<\/p>\n<p><strong>\u00a0<\/strong>import (<\/p>\n<p>&#8220;errors&#8221;<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>)<\/p>\n<p>var ErrNotFound = errors.New(&#8220;record not found&#8221;)<\/p>\n<p>func findUser(id int) error {<\/p>\n<p>if id == 0 {<\/p>\n<p>return ErrNotFound<\/p>\n<p>}<\/p>\n<p>return nil<\/p>\n<p>}<\/p>\n<p>func getUser(id int) error {<\/p>\n<p>if err := findUser(id); err != nil {<\/p>\n<p>\/\/ wrap the original error with more context<\/p>\n<p>return fmt.Errorf(&#8220;getUser failed: %w&#8221;, err)<\/p>\n<p>}<\/p>\n<p>return nil<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>err := getUser(0)<\/p>\n<p>fmt.Println(&#8220;Error:&#8221;, err) \/\/ Error: getUser failed: record not found<\/p>\n<p>}<\/p>\n<p>Here:<\/p>\n<ul>\n<li>%w wraps the original error inside the new one.<\/li>\n<li>The caller sees the context + the original root error.<\/li>\n<\/ul>\n<p><strong>\ud83d\udd39 <\/strong>Unwrapping Errors<\/p>\n<p>You can unwrap errors using the errors package:<\/p>\n<ol>\n<li>errors.Is(err, target)<br \/>\nChecks if the error or any wrapped error is target.<\/li>\n<\/ol>\n<p>if errors.Is(err, ErrNotFound) {<\/p>\n<p>fmt.Println(&#8220;User not found!&#8221;)<\/p>\n<p>}<\/p>\n<ol>\n<li>errors.As(err, &amp;targetType)<br \/>\nExtracts the original error into a variable if it matches a type.<\/li>\n<\/ol>\n<p>var pathErr *os.PathError<\/p>\n<p>if errors.As(err, &amp;pathErr) {<\/p>\n<p>fmt.Println(&#8220;Path error on file:&#8221;, pathErr.Path)<\/p>\n<p>}<\/p>\n<ol>\n<li>errors.Unwrap(err)<br \/>\nReturns the next error in the chain (unwraps one layer).<\/li>\n<\/ol>\n<p>cause := errors.Unwrap(err)<\/p>\n<p>fmt.Println(&#8220;Root cause:&#8221;, cause) \/\/ Root cause: record not found<\/p>\n<p><strong>\ud83d\udd39 Why use Wrap &amp; Unwrap?<\/strong><\/p>\n<ul>\n<li>Add context without losing the original error.<\/li>\n<li>Helps in debugging (you know <em>where<\/em> the error happened and <em>why<\/em>).<\/li>\n<li>Makes error handling more powerful with errors.Is \/ errors.As.<\/li>\n<\/ul>\n<p>\u2705 In short:<\/p>\n<ul>\n<li>Wrap = Add context to an error (fmt.Errorf(&#8220;&#8230; %w&#8221;, err)).<\/li>\n<li>Unwrap = Retrieve or check the original error (errors.Unwrap, errors.Is, errors.As).<\/li>\n<\/ul>\n<h2><strong>74. Using Oauth third party Api access only some users how can we done in Golang<\/strong><\/h2>\n<ol>\n<li>Users log in with a third-party provider (Google, GitHub, etc.) via OAuth2.<\/li>\n<li>Your Go backend receives the access token (or ID token) from the provider.<\/li>\n<li>You verify the token and extract user info (like email, user ID).<\/li>\n<li>You check the user against your own allowlist (DB, config, or role table).<\/li>\n<li>If allowed \u2192 issue your own JWT\/session \u2192 grant access.<\/li>\n<li>If not allowed \u2192 deny access.<\/li>\n<\/ol>\n<p>\ud83d\udd39 2. Go Libraries to Use<\/p>\n<ul>\n<li>golang.org\/x\/oauth2 \u2192 standard OAuth2 client.<\/li>\n<li>github.com\/coreos\/go-oidc \u2192 if you need to verify OpenID Connect (ID tokens, e.g., Google).<\/li>\n<\/ul>\n<ol>\n<li>What client ID \/ client secret mean<\/li>\n<\/ol>\n<ul>\n<li>When you register your app with a third-party provider (Google, GitHub, Zoho, etc.), they give you:\n<ul>\n<li>Client ID \u2192 identifies your app.<\/li>\n<li>Client Secret \u2192 used to prove your app\u2019s authenticity.<\/li>\n<\/ul>\n<\/li>\n<li>Your Go service uses these to request tokens on behalf of a user.<\/li>\n<\/ul>\n<p>\ud83d\udd39 2. OAuth Flow (with client ID &amp; secret)<\/p>\n<ol>\n<li>User Login Redirect\n<ul>\n<li>Your app redirects user to the third-party OAuth authorization URL (with client ID).<\/li>\n<\/ul>\n<\/li>\n<li>User Authorizes\n<ul>\n<li>User logs in and grants permission.<\/li>\n<\/ul>\n<\/li>\n<li>Token Exchange\n<ul>\n<li>The provider redirects back with a code.<\/li>\n<li>Your backend exchanges that code for an access token using client ID + client secret.<\/li>\n<\/ul>\n<\/li>\n<li>User Info Fetch\n<ul>\n<li>You call the provider\u2019s \/userinfo (or similar) API with the access token to get user details (email, id, etc.).<\/li>\n<\/ul>\n<\/li>\n<li>Restrict Access\n<ul>\n<li>You check if the user is allowed (via DB\/allowlist). If yes \u2192 issue your own token\/session.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h1><span style=\"font-size: 14.0pt; line-height: 115%;\">75.Call multiple goroutines directly in the main() function \u2014 each one doing a separate task concurrently.<\/span><\/h1>\n<p>Here\u2019s a clean and production-style example showing how to call multiple goroutines safely and properly from main() using sync.WaitGroup.<\/p>\n<p>\u2705 Example \u2014 Multiple Goroutines in main()<\/p>\n<p>package main<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;sync&#8221;<\/p>\n<p>&#8220;time&#8221;<\/p>\n<p>)<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ simulateTask simulates a time-consuming task<\/p>\n<p>func simulateTask(name string, delay time.Duration, wg *sync.WaitGroup) {<\/p>\n<p>defer wg.Done() \/\/ mark this goroutine as done when finished<\/p>\n<p>for i := 1; i &lt;= 3; i++ {<\/p>\n<p>fmt.Printf(&#8220;%s working&#8230; step %d\\n&#8221;, name, i)<\/p>\n<p>time.Sleep(delay)<\/p>\n<p>}<\/p>\n<p>fmt.Printf(&#8220;%s completed \u2705\\n&#8221;, name)<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>var wg sync.WaitGroup<\/p>\n<p>\/\/ Launch multiple goroutines<\/p>\n<p>wg.Add(1)<\/p>\n<p>go simulateTask(&#8220;Task 1&#8221;, 500*time.Millisecond, &amp;wg)<\/p>\n<p>wg.Add(1)<\/p>\n<p>go simulateTask(&#8220;Task 2&#8221;, 300*time.Millisecond, &amp;wg)<\/p>\n<p>wg.Add(1)<\/p>\n<p>go simulateTask(&#8220;Task 3&#8221;, 700*time.Millisecond, &amp;wg)<\/p>\n<p>wg.Add(1)<\/p>\n<p>go simulateTask(&#8220;Task 4&#8221;, 400*time.Millisecond, &amp;wg)<\/p>\n<p>\/\/ Wait for all goroutines to finish<\/p>\n<p>wg.Wait()<\/p>\n<p>fmt.Println(&#8220;\ud83c\udf89 All tasks finished. Exiting main.&#8221;)<\/p>\n<p>}<\/p>\n<h2><strong>76.How to achive parallelism in golang using Parallel Computation<\/strong><\/h2>\n<p>package main<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;runtime&#8221;<\/p>\n<p>&#8220;sync&#8221;<\/p>\n<p>&#8220;time&#8221;<\/p>\n<p>)<\/p>\n<p>func compute(id int, wg *sync.WaitGroup) {<\/p>\n<p>defer wg.Done()<\/p>\n<p>fmt.Printf(&#8220;Goroutine %d started on CPU %d\\n&#8221;, id, runtime.NumCPU())<\/p>\n<p>sum := 0<\/p>\n<p>for i := 0; i &lt; 100000000; i++ {<\/p>\n<p>sum += i<\/p>\n<p>}<\/p>\n<p>fmt.Printf(&#8220;Goroutine %d done: Sum=%d\\n&#8221;, id, sum)<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>\/\/ Use all CPU cores available<\/p>\n<p>numCPU := runtime.NumCPU()<\/p>\n<p>runtime.GOMAXPROCS(numCPU)<\/p>\n<p>fmt.Printf(&#8220;Running with %d CPUs\\n&#8221;, numCPU)<\/p>\n<p>&nbsp;<\/p>\n<p>var wg sync.WaitGroup<\/p>\n<p>start := time.Now()<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Launch multiple goroutines in parallel<\/p>\n<p>for i := 1; i &lt;= numCPU; i++ {<\/p>\n<p>wg.Add(1)<\/p>\n<p>go compute(i, &amp;wg)<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>wg.Wait()<\/p>\n<p>fmt.Printf(&#8220;\u2705 All done in %v\\n&#8221;, time.Since(start))<\/p>\n<p>}<\/p>\n<p>Parallelism with Worker Pool Example<\/p>\n<p>package main<\/p>\n<p>&nbsp;<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;runtime&#8221;<\/p>\n<p>&#8220;sync&#8221;<\/p>\n<p>)<\/p>\n<p>func worker(id int, jobs &lt;-chan int, wg *sync.WaitGroup) {<\/p>\n<p>defer wg.Done()<\/p>\n<p>for j := range jobs {<\/p>\n<p>fmt.Printf(&#8220;Worker %d processing job %d\\n&#8221;, id, j)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>func main() {<\/p>\n<p>runtime.GOMAXPROCS(runtime.NumCPU())<\/p>\n<p>jobs := make(chan int, 10)<\/p>\n<p>var wg sync.WaitGroup<\/p>\n<p>&nbsp;<\/p>\n<p>for w := 1; w &lt;= 4; w++ {<\/p>\n<p>wg.Add(1)<\/p>\n<p>go worker(w, jobs, &amp;wg)<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>for j := 1; j &lt;= 8; j++ {<\/p>\n<p>jobs &lt;- j<\/p>\n<p>}<\/p>\n<p>close(jobs)<\/p>\n<p>wg.Wait()<\/p>\n<p>}<\/p>\n<p><strong>Here, 4 workers handle 8 jobs in parallel across CPU cores.<\/strong><\/p>\n<p><strong>\u2705 Summary<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Feature<\/strong><\/td>\n<td><strong>Use<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>go func()<\/td>\n<td>Runs goroutine concurrently<\/td>\n<\/tr>\n<tr>\n<td>runtime.NumCPU()<\/td>\n<td>Finds number of CPU cores<\/td>\n<\/tr>\n<tr>\n<td>runtime.GOMAXPROCS(n)<\/td>\n<td>Enables parallel execution on n cores<\/td>\n<\/tr>\n<tr>\n<td>sync.WaitGroup<\/td>\n<td>Waits for goroutines to complete<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<td>Parallelism achieved when multiple goroutines actually run on multiple CPU cores<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>77. Authentication comes first or Authorization<\/strong><\/h2>\n<table>\n<tbody>\n<tr>\n<td>Authentication comes first, then Authorization.<\/p>\n<p>\ud83d\udd11 Why?<\/p>\n<table>\n<thead>\n<tr>\n<td>Step<\/td>\n<td>Meaning<\/td>\n<td>Question it answers<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Authentication<\/td>\n<td>Verifies who the user is<\/td>\n<td><em>&#8220;Are you really this user?&#8221;<\/em><\/td>\n<\/tr>\n<tr>\n<td>Authorization<\/td>\n<td>Verifies what the user is allowed to do<\/td>\n<td><em>&#8220;What permissions do you have?&#8221;<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\ud83d\udd04 Order in real flow<\/p>\n<ol>\n<li>User logs in with username\/password (Authentication)<\/li>\n<li>System confirms identity \u2192 generates token\/session<\/li>\n<li>System checks user roles\/permissions (Authorization)<\/li>\n<li>User is allowed or denied access to the requested feature<\/li>\n<\/ol>\n<p>\ud83d\udccc Real-world analogy<\/p>\n<table>\n<thead>\n<tr>\n<td>Daily Life Example<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>At office gate: Security checks ID \u2192 Authentication<\/td>\n<\/tr>\n<tr>\n<td>In the office building: Access card decides which floors\/rooms you can enter \u2192 Authorization<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\ud83d\udd25 One-line answer<\/p>\n<p>Authentication always comes before Authorization.<\/p>\n<p>Because system cannot decide what a user can access until it knows who the user is.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>78. What is 401 api response<\/strong><\/h2>\n<p>Unauthorized \u2013 the request has not been applied because it lacks valid authentication credentials.<\/p>\n<p>You will usually receive HTTP 401 when:<\/p>\n<ul>\n<li>No token is sent<\/li>\n<li>Token is expired<\/li>\n<li>Token is invalid or malformed<\/li>\n<li>Wrong username\/password<\/li>\n<li>Authorization header missing<\/li>\n<\/ul>\n<p><strong>\ud83d\udca1 <\/strong>Example of 401 response<\/p>\n<p>HTTP\/1.1 401 Unauthorized<\/p>\n<p>{<\/p>\n<p>&#8220;error&#8221;: &#8220;Unauthorized&#8221;,<\/p>\n<p>&#8220;message&#8221;: &#8220;Invalid or missing token&#8221;<\/p>\n<p>}<\/p>\n<p>\u26a0 401 vs 403 (important difference)<\/p>\n<table>\n<thead>\n<tr>\n<td>Status<\/td>\n<td>Meaning<\/td>\n<td>Cause<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>401 Unauthorized<\/td>\n<td>Authentication failed<\/td>\n<td>User is not logged in or token is invalid<\/td>\n<\/tr>\n<tr>\n<td>403 Forbidden<\/td>\n<td>Authorization failed<\/td>\n<td>User is logged in but doesn&#8217;t have permission<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u2714 How to fix 401<\/p>\n<table>\n<thead>\n<tr>\n<td>Problem<\/td>\n<td>Solution<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Token not sent<\/td>\n<td>Add Authorization: Bearer &lt;token&gt; in header<\/td>\n<\/tr>\n<tr>\n<td>Token expired<\/td>\n<td>Refresh token \/ login again<\/td>\n<\/tr>\n<tr>\n<td>Invalid token<\/td>\n<td>Re-generate token<\/td>\n<\/tr>\n<tr>\n<td>Wrong credentials<\/td>\n<td>Correct username\/password<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>79.What code practice need to follow in Golang<\/h2>\n<p>Follow Go Naming Conventions<\/p>\n<table>\n<thead>\n<tr>\n<td>Type<\/td>\n<td>Convention<\/td>\n<td>Example<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Package<\/td>\n<td>lower case, no underscore<\/td>\n<td>package userauth<\/td>\n<\/tr>\n<tr>\n<td>Variable \/ Function<\/td>\n<td>camelCase<\/td>\n<td>totalCount, getUserData()<\/td>\n<\/tr>\n<tr>\n<td>Exported Name<\/td>\n<td>PascalCase<\/td>\n<td>GetUser()<\/td>\n<\/tr>\n<tr>\n<td>Constants<\/td>\n<td>UPPER_SNAKE_CASE (or PascalCase)<\/td>\n<td>MAX_RETRY, DefaultLimit<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u2705 2. <\/strong>Use gofmt Formatting<\/p>\n<p>Always format your code:<\/p>\n<p>go fmt .\/&#8230;<\/p>\n<p>Go prefers tabs over spaces, automatic alignment, and clean formatting.<\/p>\n<p>\u2705 3. Keep Functions Small<\/p>\n<p>A function should do one task only.<br \/>\nBad:<\/p>\n<p>func ProcessOrder() { \/* 100s of lines *\/ }<\/p>\n<p>Good:<\/p>\n<p>func ValidateOrder() {}<\/p>\n<p>func SaveOrder() {}<\/p>\n<p>func SendOrderEmail() {}<\/p>\n<p>\u2705 4. Error Handling Practices<\/p>\n<ul>\n<li>Don\u2019t ignore errors<\/li>\n<li>Return early instead of nesting deeply<\/li>\n<\/ul>\n<p>Bad:<\/p>\n<p>if err != nil {<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>} else {<\/p>\n<p>\/\/ nested large logic<\/p>\n<p>}<\/p>\n<p>Good:<\/p>\n<p>if err != nil {<\/p>\n<p>return err<\/p>\n<p>}<\/p>\n<p>\/\/ continue<\/p>\n<p>Use meaningful error messages:<\/p>\n<p>return fmt.Errorf(&#8220;failed to connect DB: %w&#8221;, err)<\/p>\n<p>\u2705 5. Avoid Global Variables<\/p>\n<p>Prefer dependency injection:<\/p>\n<p>type Service struct {<\/p>\n<p>db *sql.DB<\/p>\n<p>}<\/p>\n<p>func NewService(db *sql.DB) *Service { return &amp;Service{db: db} }<\/p>\n<p>\u2705 6. Use Context for Request-Scoped Data<\/p>\n<p>Especially for APIs \/ DB operations:<\/p>\n<p>func GetUser(ctx context.Context, id int) {}<\/p>\n<p>\u2705 7. Keep Packages Small and Purposeful<\/p>\n<p>Bad:<\/p>\n<p>package utils\u00a0 \/\/ used for everything<\/p>\n<p>Good:<\/p>\n<p>package logger<\/p>\n<p>package validation<\/p>\n<p>package repository<\/p>\n<p>\u2705 8. Write Unit Tests<\/p>\n<p>Use testing package:<\/p>\n<p>go test .\/&#8230;<\/p>\n<p>Test exported and critical functions.<\/p>\n<p>\u2705 9. Use Interfaces Only When Needed<\/p>\n<p>Don\u2019t create interfaces just for abstraction.<br \/>\nUse interfaces only when multiple implementations are expected.<\/p>\n<p>\u2705 10. Concurrency Safety<\/p>\n<ul>\n<li>Use channels or mutex for shared memory<\/li>\n<li>Avoid race conditions<\/li>\n<li>Run tests with race detector:<\/li>\n<\/ul>\n<p>go test -race .\/&#8230;<\/p>\n<p>Performance Practices<\/p>\n<table>\n<thead>\n<tr>\n<td>Do<\/td>\n<td>Avoid<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Use slices over arrays<\/td>\n<td>Using large arrays<\/td>\n<\/tr>\n<tr>\n<td>Use sync.Pool for reusable objects<\/td>\n<td>Allocating unnecessary memory<\/td>\n<\/tr>\n<tr>\n<td>Use jsoniter for heavy JSON workloads<\/td>\n<td>Default JSON when performance critical<\/td>\n<\/tr>\n<tr>\n<td>Use Goroutines for I\/O-bound tasks<\/td>\n<td>Overspawning goroutines<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>80.What is use of Sonarqube<\/h2>\n<p>What SonarQube Does<\/p>\n<p>SonarQube scans your source code and detects:<\/p>\n<table>\n<thead>\n<tr>\n<td>Category<\/td>\n<td>Examples<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Bugs<\/td>\n<td>Null pointer issues, resource leaks<\/td>\n<\/tr>\n<tr>\n<td>Vulnerabilities<\/td>\n<td>SQL Injection, XSS, hardcoded credentials<\/td>\n<\/tr>\n<tr>\n<td>Code Smells<\/td>\n<td>Bad design, duplicated code, long functions<\/td>\n<\/tr>\n<tr>\n<td>Test Coverage<\/td>\n<td>How much code is covered by unit tests<\/td>\n<\/tr>\n<tr>\n<td>Maintainability Issues<\/td>\n<td>High complexity, unused variables, dead code<\/td>\n<\/tr>\n<tr>\n<td>Security Hotspots<\/td>\n<td>Code needing review for security risk<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Why SonarQube Is Used<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td>Purpose<\/td>\n<td>Benefit<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Improve code quality<\/td>\n<td>Cleaner &amp; maintainable code<\/td>\n<\/tr>\n<tr>\n<td>Detect vulnerabilities early<\/td>\n<td>Reduce security risk<\/td>\n<\/tr>\n<tr>\n<td>Enforce coding standards<\/td>\n<td>Consistency across teams<\/td>\n<\/tr>\n<tr>\n<td>Track technical debt<\/td>\n<td>Visibility of improvement areas<\/td>\n<\/tr>\n<tr>\n<td>Continuous integration<\/td>\n<td>Automates scanning in pipelines (CI\/CD)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\ud83d\udee0 Where SonarQube Is Used<\/p>\n<ul>\n<li>Software development teams<\/li>\n<li>DevOps CI\/CD pipelines<\/li>\n<li>Code review stages (PR scanning)<\/li>\n<li>Agile projects to maintain long-term quality<\/li>\n<\/ul>\n<p>\ud83d\udca1 CI\/CD Usage Example<\/p>\n<p>SonarQube is integrated into:<\/p>\n<ul>\n<li>Jenkins<\/li>\n<li>GitHub Actions<\/li>\n<li>GitLab CI\/CD<\/li>\n<li>Azure DevOps<\/li>\n<li>Bitbucket Pipeline<\/li>\n<\/ul>\n<p>It scans code after every commit \/ pull request and generates a quality report.<\/p>\n<h2>81.What is authorization<\/h2>\n<p>Authorization is the process of determining what an authenticated user is allowed to access or do.<\/p>\n<p>\ud83d\udd11 Simple Definition<\/p>\n<p>Authorization decides whether a user has permission to perform an action or access a resource.<\/p>\n<p>It happens after authentication.<\/p>\n<p>\ud83d\udccc Example<\/p>\n<ol>\n<li>Authentication \u2192 User logs in with username + password<\/li>\n<li>Authorization \u2192 System checks:\n<ul>\n<li>Can this user access the dashboard?<\/li>\n<li>Can this user delete a record?<\/li>\n<li>Can this user upload a file?<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>82. What is used of gorila mux lib in gin how the routing done<\/h2>\n<table>\n<thead>\n<tr>\n<td>Framework<\/td>\n<td>Package<\/td>\n<td>Usage<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Gorilla Mux<\/td>\n<td>github.com\/gorilla\/mux<\/td>\n<td>Router for net\/http<\/td>\n<\/tr>\n<tr>\n<td>Gin<\/td>\n<td>github.com\/gin-gonic\/gin<\/td>\n<td>Full web framework &amp; router<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\ud83d\uded1 Gorilla Mux is NOT used inside Gin.<br \/>\nThey are separate libraries. You use either Gorilla Mux or Gin, not both together.<\/p>\n<p><strong>\ud83d\udd39 What is Gorilla Mux used for?<\/strong><\/p>\n<p>Gorilla Mux is a powerful HTTP router for Go (used with net\/http).<br \/>\nIt provides:<\/p>\n<ul>\n<li>URL path parameters (\/users\/{id})<\/li>\n<li>Query matching<\/li>\n<li>Method matching (GET\/POST\/PUT)<\/li>\n<li>Middleware support<\/li>\n<\/ul>\n<p>\u2714 Example routing using Gorilla Mux<\/p>\n<p>package main<\/p>\n<p>&nbsp;<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;net\/http&#8221;<\/p>\n<p>&#8220;github.com\/gorilla\/mux&#8221;<\/p>\n<p>)<\/p>\n<p>&nbsp;<\/p>\n<p>func main() {<\/p>\n<p>r := mux.NewRouter()<\/p>\n<p>&nbsp;<\/p>\n<p>r.HandleFunc(&#8220;\/users\/{id}&#8221;, func(w http.ResponseWriter, r *http.Request) {<\/p>\n<p>params := mux.Vars(r)<\/p>\n<p>fmt.Fprintf(w, &#8220;User ID: %s&#8221;, params[&#8220;id&#8221;])<\/p>\n<p>}).Methods(&#8220;GET&#8221;)<\/p>\n<p>&nbsp;<\/p>\n<p>http.ListenAndServe(&#8220;:8080&#8221;, r)<\/p>\n<p>}<\/p>\n<h2>83. What is Gin used for?<\/h2>\n<p>Gin is a fast web framework with router + middleware + JSON handling.<\/p>\n<p>\u2714 Example routing using Gin<\/p>\n<p>package main<\/p>\n<p>import &#8220;github.com\/gin-gonic\/gin&#8221;<\/p>\n<p>func main() {<\/p>\n<p>r := gin.Default()<\/p>\n<p>r.GET(&#8220;\/users\/:id&#8221;, func(c *gin.Context) {<\/p>\n<p>id := c.Param(&#8220;id&#8221;)<\/p>\n<p>c.JSON(200, gin.H{&#8220;user_id&#8221;: id})<\/p>\n<p>})<\/p>\n<p>r.Run(&#8220;:8080&#8221;)<\/p>\n<p>}<\/p>\n<p>\ud83d\udd25 Key Difference in Routing Style<\/p>\n<table>\n<thead>\n<tr>\n<td>Feature<\/td>\n<td>Gorilla Mux<\/td>\n<td>Gin<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Path parameter<\/td>\n<td>{id}<\/td>\n<td>:id<\/td>\n<\/tr>\n<tr>\n<td>Response<\/td>\n<td>Manual fmt.Fprintf \/ json.NewEncoder<\/td>\n<td>c.JSON, c.String, etc.<\/td>\n<\/tr>\n<tr>\n<td>Performance<\/td>\n<td>Slower<\/td>\n<td>Very fast<\/td>\n<\/tr>\n<tr>\n<td>Framework<\/td>\n<td>Only router<\/td>\n<td>Full framework (router + middleware + binding)<\/td>\n<\/tr>\n<tr>\n<td>Learning curve<\/td>\n<td>Simple<\/td>\n<td>Moderate<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\ud83d\udcdd When to use which<\/p>\n<table>\n<thead>\n<tr>\n<td>Use Gorilla Mux when<\/td>\n<td>Use Gin when<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>You want simple http routers<\/td>\n<td>You want a full web backend<\/td>\n<\/tr>\n<tr>\n<td>You prefer net\/http<\/td>\n<td>You need fast JSON APIs<\/td>\n<\/tr>\n<tr>\n<td>Small\/lightweight project<\/td>\n<td>REST API projects<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>84. What is diff between query and params<\/h2>\n<p><strong>: <\/strong>Query parameters are sent after ? symbol in the URL and used for filtering, searching, sorting, pagination, etc.<\/p>\n<p>URL example:<\/p>\n<p>\/users?role=admin&amp;page=2<\/p>\n<p>Key points<\/p>\n<table>\n<thead>\n<tr>\n<td>Feature<\/td>\n<td>Value<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Part of URL<\/td>\n<td>After ?<\/td>\n<\/tr>\n<tr>\n<td>Optional<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>Used for<\/td>\n<td>Filters \/ search \/ sorting \/ pagination<\/td>\n<\/tr>\n<tr>\n<td>Format<\/td>\n<td>key=value&amp;key2=value2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Difference Summary<\/p>\n<table>\n<thead>\n<tr>\n<td>Feature<\/td>\n<td>Query Parameters<\/td>\n<td>Path Parameters<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Location in URL<\/td>\n<td>After ?<\/td>\n<td>Inside route<\/td>\n<\/tr>\n<tr>\n<td>Mandatory<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>Purpose<\/td>\n<td>Filters \/ search \/ optional inputs<\/td>\n<td>Identify a specific resource<\/td>\n<\/tr>\n<tr>\n<td>Example<\/td>\n<td>\/users?role=admin<\/td>\n<td>\/users\/10<\/td>\n<\/tr>\n<tr>\n<td>Extract in Gin<\/td>\n<td>c.Query(&#8220;key&#8221;)<\/td>\n<td>c.Param(&#8220;key&#8221;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>85. How you have management cache in Golang<\/h2>\n<p><strong>Use case<\/strong><\/p>\n<ul>\n<li>Single service instance<\/li>\n<li>Fast reads<\/li>\n<li>Data can be lost on restart<\/li>\n<\/ul>\n<ul>\n<li>Never cache auth-sensitive data blindly<\/li>\n<li>Cache <strong>read-heavy<\/strong>, not write-heavy data<\/li>\n<li>Measure hit ratio<\/li>\n<\/ul>\n<h2>86. where you used mutex in Golang<\/h2>\n<p>mutex is used to protect shared data when multiple goroutines access it at the same time.<br \/>\nWithout a mutex \u2192 race condition \u274c<br \/>\nWith a mutex \u2192 safe, predictable behavior \u2705<br \/>\nWhy mutex is needed (problem)<\/p>\n<p>var count int<\/p>\n<p>&nbsp;<\/p>\n<p>func increment() {<\/p>\n<p>count++<\/p>\n<p>}<\/p>\n<p>If 100 goroutines call increment():<\/p>\n<ul>\n<li>count++ is not atomic<\/li>\n<li>Final value will be wrong<\/li>\n<\/ul>\n<p>2\ufe0f.Basic mutex usage<\/p>\n<p>var (<\/p>\n<p>count int<\/p>\n<p>mu\u00a0\u00a0\u00a0 sync.Mutex<\/p>\n<p>)<\/p>\n<p>func increment() {<\/p>\n<p>mu.Lock()<\/p>\n<p>count++<\/p>\n<p>mu.Unlock()<\/p>\n<p>}<\/p>\n<p>\u2714 Only one goroutine modifies count at a time<\/p>\n<p>3\ufe0f. Where mutex is ACTUALLY used in real projects<\/p>\n<p>\u2705 1. Protect shared in-memory cache<\/p>\n<p>type Cache struct {<\/p>\n<p>data map[string]string<\/p>\n<p>mu\u00a0\u00a0 sync.RWMutex<\/p>\n<p>}<\/p>\n<p>func (c *Cache) Get(key string) (string, bool) {<\/p>\n<p>c.mu.RLock()<\/p>\n<p>defer c.mu.RUnlock()<\/p>\n<p>&nbsp;<\/p>\n<p>val, ok := c.data[key]<\/p>\n<p>return val, ok<\/p>\n<p>}<\/p>\n<p>func (c *Cache) Set(key, value string) {<\/p>\n<p>c.mu.Lock()<\/p>\n<p>defer c.mu.Unlock()<\/p>\n<p>&nbsp;<\/p>\n<p>c.data[key] = value<\/p>\n<p>}<\/p>\n<p>\ud83d\udccc Most common use-case<\/p>\n<p>\u2705 2. API request counters \/ metrics<\/p>\n<p>type Metrics struct {<\/p>\n<p>TotalRequests int<\/p>\n<p>mu\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sync.Mutex<\/p>\n<p>}<\/p>\n<p>func (m *Metrics) Inc() {<\/p>\n<p>m.mu.Lock()<\/p>\n<p>m.TotalRequests++<\/p>\n<p>m.mu.Unlock()<\/p>\n<p>}<\/p>\n<p>\u2705 3. Singleton initialization<\/p>\n<p>var (<\/p>\n<p>db\u00a0\u00a0 *sql.DB<\/p>\n<p>once sync.Once<\/p>\n<p>)<\/p>\n<p>func GetDB() *sql.DB {<\/p>\n<p>once.Do(func() {<\/p>\n<p>db = connectDB()<\/p>\n<p>})<\/p>\n<p>return db<\/p>\n<p>}<\/p>\n<p>\u2714 Prevents multiple initializations<\/p>\n<p>\u2705 4. Worker pool shared state<\/p>\n<p>type JobQueue struct {<\/p>\n<p>jobs []string<\/p>\n<p>mu\u00a0\u00a0 sync.Mutex<\/p>\n<p>}<\/p>\n<p>func (q *JobQueue) Add(job string) {<\/p>\n<p>q.mu.Lock()<\/p>\n<p>q.jobs = append(q.jobs, job)<\/p>\n<p>q.mu.Unlock()<\/p>\n<p>}<\/p>\n<p>\u2705 5. Session \/ token store<\/p>\n<p>type SessionStore struct {<\/p>\n<p>sessions map[string]string<\/p>\n<p>mu\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sync.Mutex<\/p>\n<p>}<\/p>\n<p>Used when:<\/p>\n<ul>\n<li>In-memory auth sessions<\/li>\n<li>OTP tracking<\/li>\n<li>Rate limiting<\/li>\n<\/ul>\n<p>4\ufe0f.Mutex vs RWMutex (important)<\/p>\n<table>\n<thead>\n<tr>\n<td>Mutex<\/td>\n<td>RWMutex<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>One reader\/writer<\/td>\n<td>Multiple readers<\/td>\n<\/tr>\n<tr>\n<td>Slower reads<\/td>\n<td>Faster reads<\/td>\n<\/tr>\n<tr>\n<td>Simple<\/td>\n<td>Best for read-heavy<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>var mu sync.RWMutex<\/p>\n<p>mu.RLock()\u00a0\u00a0 \/\/ read<\/p>\n<p>mu.RUnlock()<\/p>\n<p>mu.Lock()\u00a0\u00a0\u00a0 \/\/ write<\/p>\n<p>mu.Unlock()<\/p>\n<p>5\ufe0f.When NOT to use mutex<\/p>\n<p>\u274c If only one goroutine accesses data<br \/>\n\u274c If using channels already<br \/>\n\u274c For DB-level concurrency (DB handles it)<\/p>\n<p>6\ufe0f.Mutex vs Channel (confusion point)<\/p>\n<table>\n<thead>\n<tr>\n<td>Mutex<\/td>\n<td>Channel<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Protect shared memory<\/td>\n<td>Share memory<\/td>\n<\/tr>\n<tr>\n<td>State protection<\/td>\n<td>Message passing<\/td>\n<\/tr>\n<tr>\n<td>Low overhead<\/td>\n<td>Better for workflows<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Rule of thumb:<\/p>\n<p>Use mutex for data, channels for flow<\/p>\n<p>Summary<\/p>\n<p>You use mutex when:<\/p>\n<ul>\n<li>Multiple goroutines access shared data<\/li>\n<li>In-memory cache<\/li>\n<li>Counters \/ metrics<\/li>\n<li>Job queues<\/li>\n<li>Rate limiters<\/li>\n<\/ul>\n<p>Session storage<\/p>\n<h2>87. How to do authentication and authorization in Golang<\/h2>\n<p>Below is the <strong>standard, production-ready way to do Authentication &amp; Authorization in Go<\/strong>, exactly how it\u2019s done in <strong>real backend APIs (Gin\/Fiber)<\/strong>.<\/p>\n<p><strong>Authentication vs Authorization (first clear this)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Term<\/strong><\/td>\n<td><strong>Meaning<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Authentication<\/td>\n<td><strong>Who are you?<\/strong> (Login, token)<\/td>\n<\/tr>\n<tr>\n<td>Authorization<\/td>\n<td><strong>What are you allowed to do?<\/strong> (Roles, permissions)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\ud83d\udc49 <strong>Authentication always comes first<\/strong><\/p>\n<p><strong>Common Ways to Do Authentication in Go<\/strong><\/p>\n<ol>\n<li><strong>Basic Authentication (username + password in headers, rarely used in production)<\/strong><\/li>\n<li><strong>Session-Based Authentication (store session ID in cookies, typical for web apps)<\/strong><\/li>\n<li><strong>Token-Based Authentication (JWT, OAuth2, etc.) \u2192 widely used in APIs<\/strong><\/li>\n<li><strong>Third-party Authentication (Google, GitHub, etc. via OAuth2\/OpenID Connect)<\/strong><\/li>\n<\/ol>\n<ol start=\"5\">\n<li><strong>JWT Authentication (Most Common for APIs)<\/strong><\/li>\n<li>JWT (<strong>JSON Web Token<\/strong>) is a stateless token used for authenticating API requests.<\/li>\n<li>Create JWT Token<br \/>\nMiddleware to Validate Token<br \/>\n. Usage in Routes<\/li>\n<\/ol>\n<p><strong>Authentication in Go (JWT-based \u2013 most common)<\/strong><\/p>\n<p><strong>Flow<\/strong><\/p>\n<p>Login \u2192 Verify credentials \u2192 Generate JWT \u2192 Client sends JWT \u2192 Middleware validattes JWT<\/p>\n<p><strong>When to use what (important)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Scenario<\/strong><\/td>\n<td><strong>Solution<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Simple API<\/td>\n<td>JWT<\/td>\n<\/tr>\n<tr>\n<td>Microservices<\/td>\n<td>JWT + Redis<\/td>\n<\/tr>\n<tr>\n<td>Enterprise<\/td>\n<td>OAuth2 \/ Keycloak<\/td>\n<\/tr>\n<tr>\n<td>Mobile app<\/td>\n<td>JWT + Refresh token<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\uf0b7\u00a0 <strong>Authenticate<\/strong> \u2192 Login + JWT<\/p>\n<p>\uf0b7\u00a0 <strong>Authorize<\/strong> \u2192 Role\/Permission middleware<\/p>\n<p>\uf0b7\u00a0 <strong>Protect routes<\/strong> \u2192 Middleware<\/p>\n<p>\uf0b7\u00a0 <strong>Secure passwords<\/strong> \u2192 bcrypt<\/p>\n<p>\uf0b7\u00a0 <strong>Always use HTTPS<\/strong><\/p>\n<h2>88. What is 0 value in Golang<\/h2>\n<p>In <strong>Go (Golang)<\/strong>, the <strong>zero value<\/strong> is the <strong>default value automatically assigned to a variable<\/strong> when it is declared <strong>without initialization<\/strong>.<\/p>\n<p><strong>Go always initializes variables \u2014 there are no uninitialized variables.<\/strong><\/p>\n<h2>89. why use rebase in git<\/h2>\n<p>We use <strong>git rebase<\/strong> to <strong>clean up commit history<\/strong> and make it <strong>linear and easy to understand<\/strong>.<\/p>\n<p>In short:<\/p>\n<p><strong>Rebase reapplies your commits on top of another branch as if they were written later.<\/strong><\/p>\n<p><strong>\u201cPeek\u201d in Git (Informal Term <\/strong><strong>\u2705)<\/strong><\/p>\n<p><strong>Peek<\/strong> means:<\/p>\n<p><strong>Quickly look at something without changing anything<\/strong><\/p>\n<p>It is a <strong>concept<\/strong>, not a Git command.<\/p>\n<p><strong>Common \u201cpeek\u201d actions in Git:<\/strong><\/p>\n<p><strong>1\ufe0f<\/strong><strong>.Peek at commit history<\/strong><\/p>\n<p>git log &#8211;oneline<\/p>\n<p><strong>2\ufe0f<\/strong><strong>.Peek at changes (without committing)<\/strong><\/p>\n<p>git status<\/p>\n<p>git diff<\/p>\n<p><strong>3\ufe0f<\/strong><strong>.Peek at a specific commit<\/strong><\/p>\n<p>git show &lt;commit-hash&gt;<\/p>\n<p><strong>4\ufe0f<\/strong><strong>.Peek at a file from another branch<\/strong><\/p>\n<p>git show main:file.txt<\/p>\n<h2>90. Async function in go<\/h2>\n<pre>Ans: <strong>\u201cGolang doesn\u2019t have async\/await; instead it uses goroutines to run functions concurrently. When a function is started with the go keyword, it executes independently without blocking the caller. However, we must synchronize using WaitGroups, channels, or context to prevent premature exits and race conditions.\u201d\r\n<\/strong>\u201cConcurrency is not parallelism \u2014 Go enables concurrency, and parallelism happens when multiple CPU cores execute goroutines.\u201d\r\n<strong>Common Problems When Running Async Code:\r\nRace Conditions<\/strong><\/pre>\n<p><strong>Multiple goroutines accessing shared memory.<\/strong><\/p>\n<p><strong>\u2705 Fix \u2192 Mutex<\/strong><\/p>\n<ol start=\"2\">\n<li><strong> Goroutine Leaks<\/strong><\/li>\n<\/ol>\n<p>If goroutines never stop \u2192 memory usage increases.<\/p>\n<p>\u2705 Fix \u2192 Context \/ cancellation.<\/p>\n<ol start=\"3\">\n<li><strong> Deadlocks<\/strong><\/li>\n<\/ol>\n<p>When goroutines wait forever.<br \/>\n<strong>When Should You Use Async in Go?<\/strong><\/p>\n<p>Best for:<\/p>\n<p>\u2705 API calls<br \/>\n\u2705 File processing<br \/>\n\u2705 Background jobs<br \/>\n\u2705 Microservices<br \/>\n\u2705 Parallel computation<\/p>\n<h2>91.How to Reduce GC Overhead<\/h2>\n<p><strong>1. Reduce Allocations<\/strong><\/p>\n<p>Reuse objects instead of creating new ones.<\/p>\n<p>Using sync.Pool<br \/>\n<strong>2. Avoid Unnecessary Pointers<\/strong><\/p>\n<p>More pointers \u2192 more work for GC.<\/p>\n<h2>92.Does GC Prevent Memory Leaks<\/h2>\n<p><strong>No \u2014 not always.<\/strong><\/p>\n<p>If something is still referenced (like a stuck goroutine), GC cannot remove it.<\/p>\n<p>Go uses a concurrent tri-color mark-and-sweep garbage collector. It identifies reachable objects from root references, deletes unreachable ones, and runs mostly in the background to minimize pause times. While GC automates memory management, developers must still avoid holding unnecessary references to prevent memory leaks.\u201d<br \/>\n<strong>Memory leaks in Go usually happen due to stuck goroutines, unclosed resources, growing maps, or slice retention. Even with garbage collection, objects that remain referenced cannot be freed. We detect leaks using pprof and runtime metrics, and prevent them by closing resources, using context cancellation, and controlling goroutines.\u201d<br \/>\nBest Practices to Prevent Memory Leaks<\/strong><br \/>\n<strong>\u00a0Always close resources<\/strong><\/p>\n<p>Use defer<\/p>\n<p><strong>\u2714 Control goroutines<\/strong><\/p>\n<p>Use:<\/p>\n<ul>\n<li>Context cancellation<\/li>\n<li>Timeouts<\/li>\n<li>Worker pools<\/li>\n<\/ul>\n<ul>\n<li><strong>Avoid Global Variables<\/strong><\/li>\n<li>They stay for the app lifetime.<br \/>\n<strong>Limit Cache Size<\/strong><\/li>\n<li>Never allow unbounded growth.<\/li>\n<li><\/li>\n<li><strong>\u2714<\/strong><strong> Use Buffered Channels Carefully<\/strong><\/li>\n<li>Huge buffers = high memory usage.<\/li>\n<\/ul>\n<h2>93.What is Garbage Collection (GC) in Golang?<\/h2>\n<p>Garbage Collection is an <strong>automatic memory management process<\/strong> that frees memory occupied by objects <strong>no longer in use<\/strong>.<\/p>\n<p>\ud83d\udc49 You don\u2019t manually free memory like in C\/C++ \u2014 Go handles it for you.<\/p>\n<p><strong>Goal:<\/strong><br \/>\n\u2705 Prevent memory leaks<br \/>\n\u2705 Reduce manual memory handling<br \/>\n\u2705 Improve developer productivity<\/p>\n<h2>94.What is gateway<\/h2>\n<p><strong>\u201c<\/strong>An API Gateway is a centralized entry point that manages client requests and routes them to appropriate microservices. It handles cross-cutting concerns like authentication, rate limiting, caching, and load balancing, improving security and scalability.\u201d<br \/>\n<strong>With Gateway Benefits:<\/strong><\/p>\n<p>\u2705 Centralized security<br \/>\n\u2705 Easier monitoring<br \/>\n\u2705 Better scalability<br \/>\n\u2705 Cleaner architecture<\/p>\n<p><strong>Popular API Gateways (Interview Bonus)<\/strong><\/p>\n<p>\ud83d\udc49 Mentioning these impresses interviewers:<\/p>\n<ul>\n<li><strong>NGINX<\/strong><\/li>\n<li><strong>Kong<\/strong><\/li>\n<li><strong>AWS API Gateway<\/strong><\/li>\n<li><strong>Apigee<\/strong><\/li>\n<li><strong>Traefik<\/strong><\/li>\n<\/ul>\n<h2>95.Common Ways to Handle Errors<\/h2>\n<p>Return the Error<br \/>\n<strong>2<\/strong>. Wrap Errors (VERY IMPRESSIVE IN INTERVIEWS)<\/p>\n<p>Provides more context.<\/p>\n<p>if err != nil {<\/p>\n<p>return fmt.Errorf(&#8220;failed to open file: %w&#8221;, err)}<\/p>\n<ol start=\"3\">\n<li><strong> Custom Errors<\/strong><\/li>\n<\/ol>\n<h5>When you want business-level error messages.<br \/>\nvar ErrUserNotFound = errors.New(&#8220;user not found&#8221;)<\/h5>\n<p>77.When Does Panic Occur?<\/p>\n<p><strong>Runtime Errors (MOST COMMON)<\/strong><\/p>\n<p>Go automatically triggers panic in situations like:<\/p>\n<p>\u2705 Index out of range<br \/>\n\u2705 Nil pointer dereference<br \/>\n\u2705 Divide by zero<br \/>\n\u2705 Closing a nil file<br \/>\n\u2705 Writing to a closed channel<\/p>\n<p><strong>Panic vs Error (EXTREMELY IMPORTANT)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Panic<\/strong><\/td>\n<td><strong>Error<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Crashes program<\/td>\n<td>Returned and handled<\/td>\n<\/tr>\n<tr>\n<td>For unexpected situations<\/td>\n<td>For expected failures<\/td>\n<\/tr>\n<tr>\n<td>Rare usage<\/td>\n<td>Recommended approach<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><strong>How to Stop a Panic? <\/strong>\r\n<strong>\u2192 recover()<\/strong>You can catch panic using recover inside a deferred function.<\/pre>\n<h2>96.How do you stop one microservice from calling another repeatedly?<\/h2>\n<p>\u2705 Rate limiting<br \/>\n\u2705 Circuit breaker<br \/>\n\u2705 Retry with backoff<br \/>\n\u2705 Caching<\/p>\n<p>\u2b50 Mention tools like:<\/p>\n<ul>\n<li>Redis<\/li>\n<li>API Gateway<\/li>\n<\/ul>\n<h4>WHERE vs HAVING<\/h4>\n<p>\ud83d\udc49 <strong>WHERE:<\/strong> filters before grouping<br \/>\n\ud83d\udc49 <strong>HAVING:<\/strong> filters after GROUP BY<\/p>\n<h5>1. Primary Key vs Unique Key<\/h5>\n<table>\n<thead>\n<tr>\n<td><strong>Primary Key<\/strong><\/td>\n<td><strong>Unique Key<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Only one per table<\/td>\n<td>Multiple allowed<\/td>\n<\/tr>\n<tr>\n<td>Cannot be NULL<\/td>\n<td>Can be NULL<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-size: 12.0pt; line-height: 115%;\">97. Why is Golang fast?<\/span><\/h2>\n<p>Reasons:<\/p>\n<ul>\n<li>Compiled language<\/li>\n<li>Goroutines (lightweight threads)<\/li>\n<li>Efficient garbage collector<\/li>\n<li>Minimal runtime<\/li>\n<\/ul>\n<h2>98. Docker commands<\/h2>\n<p>1.docker \u2013version<\/p>\n<p>2.docker pull nginx<\/p>\n<p>3.docker images<br \/>\n4. docker run -d -p 8080:80 nginx<\/p>\n<p>5.docker ps- <strong>list of running containers<\/strong><\/p>\n<p>6.docker stats- <strong>Check Resource Usage<\/strong><\/p>\n<p>7. docker-compose up -d<\/p>\n<h2>99. Fiber vs Gin<\/h2>\n<p><strong>Fiber<\/strong><\/p>\n<ul>\n<li>Built on fasthttp (high-performance HTTP engine).<\/li>\n<li>Does not use Go\u2019s net\/http.<\/li>\n<li>Optimized for speed and low latency.<\/li>\n<\/ul>\n<p>\ud83d\udd25 Result \u2192 Better raw performance.<br \/>\n\uf0b7\u00a0 Fiber can handle more requests per second.<\/p>\n<p>\uf0b7\u00a0 Lower memory footprint.<\/p>\n<p>\uf0b7\u00a0 Great for high-throughput microservices.<\/p>\n<p>In real production, database queries and architecture matter more than framework speed.<br \/>\nChoose Fiber if:<\/p>\n<p>\u2705 You need ultra-high performance<br \/>\n\u2705 Building lightweight microservices<br \/>\n\u2705 Want Express.js-like syntax<br \/>\n\u2705 Expect massive concurrent traffic<\/p>\n<p><strong>\u00a0<\/strong><strong>Gin<\/strong><\/p>\n<ul>\n<li>Built directly on <strong>net\/http<\/strong>.<\/li>\n<li>More aligned with standard Go practices.<\/li>\n<li>Highly stable for production systems.<\/li>\n<\/ul>\n<p>\ud83d\udd25 Result \u2192 Better reliability + compatibility.<\/p>\n<p><strong>When to Choose Gin<\/strong><\/p>\n<p>Choose Gin if:<\/p>\n<p>\u2705 You want <strong>production-proven stability<\/strong><br \/>\n\u2705 Prefer standard Go behavior<br \/>\n\u2705 Need strong community support<br \/>\n\u2705 Building enterprise APIs<\/p>\n<h2>100. which command used to install fiber in Golang<\/h2>\n<p>go get github.com\/gofiber\/fiber\/v2<\/p>\n<p>&nbsp;<\/p>\n<h2>101. JWT Parameters<\/h2>\n<p>Header.Payload.Signature<br \/>\n<strong>1. Header<\/strong><\/p>\n<p>The <strong>header<\/strong> contains metadata about the token.<\/p>\n<p><strong>Common parameters:<\/strong><\/p>\n<ul>\n<li><strong>alg<\/strong> \u2192 Algorithm used for signing the token<br \/>\nExample: HS256, RS256<\/li>\n<li><strong>typ<\/strong> \u2192 Token type (always JWT)<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong> Payload (Claims)<\/strong><\/li>\n<\/ol>\n<p>The payload holds the <strong>actual data<\/strong> you want to transmit.<\/p>\n<p>Claims are of <strong>three types:<\/strong><\/p>\n<p><strong>\u2714 Registered Claims (Standard)<\/strong><\/p>\n<p>These are predefined and recommended:<\/p>\n<ul>\n<li><strong>iss (Issuer)<\/strong> \u2192 Who created the token<\/li>\n<li><strong>sub (Subject)<\/strong> \u2192 User identifier<\/li>\n<li><strong>aud (Audience)<\/strong> \u2192 Who the token is intended for<\/li>\n<li><strong>exp (Expiration Time)<\/strong> \u2192 When token expires<\/li>\n<li><strong>nbf (Not Before)<\/strong> \u2192 Token valid after this time<\/li>\n<li><strong>iat (Issued At)<\/strong> \u2192 When token was generated<\/li>\n<li><strong>jti (JWT ID)<\/strong> \u2192 Unique token ID<\/li>\n<\/ul>\n<ul>\n<li><strong> Signature<\/strong><\/li>\n<li>Used to <strong>verify the token is not tampered with<\/strong>.<\/li>\n<\/ul>\n<h2>102. What is GraphQl<\/h2>\n<p>GraphQL exposes a single endpoint, and each field in the schema is backed by a resolver that can independently call a different API. Each resolver independently calls a different backend service, and the GraphQL execution engine orchestrates, parallelizes, and merges the results into one response.<\/p>\n<h2><span style=\"font-size: 12.0pt; line-height: 115%;\">103.How Goroutines Work Internally (G-M-P Model)<\/span><\/h2>\n<p>Go uses the GMP scheduler:<\/p>\n<ul>\n<li>G (Goroutine) \u2192 your function<\/li>\n<li>M (Machine) \u2192 OS thread<\/li>\n<li>P (Processor) \u2192 scheduler context<\/li>\n<\/ul>\n<p>\ud83d\udc49 Many goroutines run on few OS threads<br \/>\n<strong>G \u2013 Goroutine<\/strong><\/p>\n<ul>\n<li>Lightweight execution unit<\/li>\n<li>Starts with ~2KB stack<\/li>\n<li>Created using go func()<\/li>\n<li>Contains:\n<ul>\n<li>stack<\/li>\n<li>instruction pointer<\/li>\n<li>state (runnable, running, waiting)<br \/>\n<strong>M \u2013 Machine (OS Thread)<\/strong><\/li>\n<li>An actual <strong>OS thread<\/strong><\/li>\n<li>Executes Go code<\/li>\n<li>Can run <strong>only one goroutine at a time<\/strong><\/li>\n<li>If an M blocks (e.g., syscall), Go creates or reuses another M<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>P \u2013 Processor (Scheduler Context)<\/strong><\/p>\n<ul>\n<li>The <strong>most important part<\/strong><\/li>\n<li>Holds:\n<ul>\n<li>run queue of goroutines<\/li>\n<li>scheduler state<\/li>\n<\/ul>\n<\/li>\n<li>Controls <strong>how many goroutines can run in parallel<\/strong><\/li>\n<\/ul>\n<p>\ud83d\udd11 Number of <strong>P<\/strong> = GOMAXPROCS<\/p>\n<p><strong>How GMP Works (Step-by-Step)<\/strong><\/p>\n<p>1\ufe0f\u20e3 Goroutine (<strong>G<\/strong>) is created<br \/>\n2\ufe0f\u20e3 It is placed in a <strong>P\u2019s local run queue<\/strong><br \/>\n3\ufe0f\u20e3 <strong>M<\/strong> picks up a <strong>P<\/strong><br \/>\n4\ufe0f\u20e3 <strong>M executes G<\/strong> using P<br \/>\n5\ufe0f\u20e3 When G blocks \u2192 M parks it \u2192 scheduler picks next G<\/p>\n<h2>104. Why use slice instead of array?<\/h2>\n<ol>\n<li><strong>Flexibility<\/strong> \u2192 size can change dynamically.<\/li>\n<li><strong>Efficiency<\/strong> \u2192 slices avoid copying data unnecessarily (share underlying array).<\/li>\n<li><strong>Simplicity<\/strong> \u2192 slices are easier to work with (append, copy, slicing).<\/li>\n<li><strong>Idiomatic Go<\/strong> \u2192 slices are the standard way to handle collections in Go.<\/li>\n<\/ol>\n<ol start=\"5\">\n<li><strong>Call by value<\/strong> \u2192 function gets a copy (safe, but original unchanged).<\/li>\n<li><strong>Call by reference<\/strong> \u2192 function gets address (efficient, can modify original).<\/li>\n<li><strong>Slices &gt; Arrays<\/strong> \u2192 because slices are dynamic, flexible, and Go\u2019s idiomatic way.<\/li>\n<\/ol>\n<h2>105. The difference between == and === operators is mainly in type coercion.<\/h2>\n<p>: <strong>== (Loose Equality)<\/strong><\/p>\n<ul>\n<li>Compares two values <strong>after converting<\/strong> them to a common type (type coercion).<\/li>\n<li>Returns true if the values are <strong>equal after type conversion<\/strong>.<\/li>\n<\/ul>\n<p><strong>\u2705 === (Strict Equality)<\/strong><\/p>\n<ul>\n<li>Compares both value and <strong>type<\/strong>.<\/li>\n<li>Returns true only if <strong>both the value and type are exactly the same<\/strong>.<\/li>\n<\/ul>\n<p>console.log(5 == &#8220;5&#8221;);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ true (string &#8220;5&#8221; is converted to number 5)<\/p>\n<p>console.log(true == 1);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ true (true is converted to 1)<\/p>\n<p>console.log(null == undefined); \/\/ true (special case)<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Using ===<\/p>\n<p>console.log(5 === &#8220;5&#8221;);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false (number !== string)<\/p>\n<p>console.log(true === 1);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ false (boolean !== number)<\/p>\n<p>console.log(null === undefined); \/\/ false (different types)<\/p>\n<h2>106. If one microservice calling many times to another microservice what should we use to stop that in Golang.<\/h2>\n<p><strong>: <\/strong>Circuit Breaker (MOST IMPORTANT)<\/p>\n<p>Stops calls automatically when the downstream service is failing or slow.<\/p>\n<p>Why?<\/p>\n<ul>\n<li>Prevents cascading failures<\/li>\n<li>Gives the downstream service time to recover<\/li>\n<\/ul>\n<p>Popular Go libraries<\/p>\n<ul>\n<li>sony\/gobreaker<\/li>\n<li>resilience4go<br \/>\n<strong>Rate Limiting<\/strong><\/li>\n<li>Limits <strong>how many requests<\/strong> a service can send or receive.<\/li>\n<li><strong>Use when<\/strong><\/li>\n<li>One service is flooding another<\/li>\n<li>Prevents overload<\/li>\n<\/ul>\n<p>Timeout + Context (MANDATORY)<br \/>\n<strong>Caching (Reduce repeated calls)<\/strong><\/p>\n<p>If data doesn\u2019t change frequently:<\/p>\n<ul>\n<li>Redis<\/li>\n<li>In-memory cache<\/li>\n<\/ul>\n<h2>107. When CPU spike<\/h2>\n<p>CPU spikes occur when workload suddenly increases due to high traffic, inefficient code, excessive concurrency, garbage collection, or heavy computations. They are diagnosed using system metrics and profiling tools and mitigated through optimization, rate limiting, and autoscaling.<\/p>\n<h2><span style=\"font-size: 12.0pt; line-height: 115%;\">108.What is sharding?<\/span><\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"73\" data-end=\"271\"><strong data-start=\"73\" data-end=\"85\">Sharding<\/strong> is a technique used to split data into smaller, independent parts called <strong data-start=\"159\" data-end=\"169\">shards<\/strong>. Each shard stores only a portion of the total data, allowing your application to scale horizontally.<\/p>\n<p data-start=\"273\" data-end=\"348\">For example, instead of storing all 1 million users in one map or database:<\/p>\n<ul data-start=\"350\" data-end=\"505\">\n<li data-section-id=\"vbo6eh\" data-start=\"350\" data-end=\"383\"><strong data-start=\"352\" data-end=\"364\">Shard 1:<\/strong> User IDs 1\u2013250,000<\/li>\n<li data-section-id=\"45hfub\" data-start=\"384\" data-end=\"423\"><strong data-start=\"386\" data-end=\"398\">Shard 2:<\/strong> User IDs 250,001\u2013500,000<\/li>\n<li data-section-id=\"hjlrzr\" data-start=\"424\" data-end=\"463\"><strong data-start=\"426\" data-end=\"438\">Shard 3:<\/strong> User IDs 500,001\u2013750,000<\/li>\n<li data-section-id=\"1jcbvl4\" data-start=\"464\" data-end=\"505\"><strong data-start=\"466\" data-end=\"478\">Shard 4:<\/strong> User IDs 750,001\u20131,000,000<\/li>\n<\/ul>\n<p data-start=\"507\" data-end=\"576\">When a request comes in, you determine which shard contains the data.<\/p>\n<hr data-start=\"578\" data-end=\"581\" \/>\n<h4 data-section-id=\"10m80ik\" data-start=\"583\" data-end=\"609\">Why use sharding in Go?<\/h4>\n<p data-start=\"611\" data-end=\"646\">Sharding is commonly used in Go to:<\/p>\n<ul data-start=\"648\" data-end=\"800\">\n<li data-section-id=\"pvhnwi\" data-start=\"648\" data-end=\"672\">Reduce lock contention<\/li>\n<li data-section-id=\"1q3i5tt\" data-start=\"673\" data-end=\"705\">Improve concurrent performance<\/li>\n<li data-section-id=\"6wjmvk\" data-start=\"706\" data-end=\"745\">Scale large in-memory data structures<\/li>\n<li data-section-id=\"2t0trx\" data-start=\"746\" data-end=\"800\">Distribute data across multiple databases or servers<\/li>\n<\/ul>\n<hr data-start=\"802\" data-end=\"805\" \/>\n<h4 data-section-id=\"l45hh3\" data-start=\"807\" data-end=\"835\">Example 1: Sharding a map<\/h4>\n<p data-start=\"837\" data-end=\"894\">Suppose many goroutines are reading and writing to a map.<\/p>\n<h3 data-section-id=\"1eduy5i\" data-start=\"896\" data-end=\"916\">Without sharding<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">Cache<\/span> <span class=\"\u037cg\">struct<\/span> {<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>    mu <span class=\"\u037cm\">sync<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">RWMutex<\/span>\r\n    data <span class=\"\u037cg\">map<\/span>[<span class=\"\u037cm\">string<\/span>]<span class=\"\u037cm\">string<\/span>\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Every operation must acquire the same mutex.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>100 goroutines<\/code><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>      \u2193\r\n Single RWMutex\r\n      \u2193\r\n     Map<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">This becomes a bottleneck.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"1137\" data-end=\"1140\" \/>\n<h3 data-section-id=\"83srag\" data-start=\"1142\" data-end=\"1159\">With sharding<\/h3>\n<p data-start=\"1161\" data-end=\"1215\">Create multiple smaller maps, each with its own mutex.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">Shard<\/span> <span class=\"\u037cg\">struct<\/span> {<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>    mu   <span class=\"\u037cm\">sync<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">RWMutex<\/span>\r\n    data <span class=\"\u037cg\">map<\/span>[<span class=\"\u037cm\">string<\/span>]<span class=\"\u037cm\">string<\/span>\r\n}\r\n\r\n<span class=\"\u037cg\">const<\/span> <span class=\"\u037cm\">NumShards<\/span> <span class=\"\u037cg\">=<\/span> <span class=\"\u037cj\">16<\/span>\r\n\r\n<span class=\"\u037cg\">type<\/span> <span class=\"\u037cm\">ShardedCache<\/span> <span class=\"\u037cg\">struct<\/span> {\r\n    shards [<span class=\"\u037cm\">NumShards<\/span>]<span class=\"\u037cm\">Shard<\/span>\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Each key belongs to one shard.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0 PDq2pG_selectionAnchorContainer\"><code><span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">getShard<\/span>(<span class=\"\u037cm\">key<\/span> <span class=\"\u037cm\">string<\/span>) <span class=\"\u037cm\">int<\/span> {\r\n    <span class=\"\u037cm\">h<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">fnv<\/span><span class=\"\u037cg\">.<\/span>New32a()\r\n    <span class=\"\u037cm\">h<\/span><span class=\"\u037cg\">.<\/span>Write([]<span class=\"\u037cm\">byte<\/span>(<span class=\"\u037cm\">key<\/span>))\r\n    <span class=\"\u037cg\">return<\/span> <span class=\"\u037cm\">int<\/span>(<span class=\"\u037cm\">h<\/span><span class=\"\u037cg\">.<\/span>Sum32()) <span class=\"\u037cg\">%<\/span> <span class=\"\u037cm\">NumShards<\/span>\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Usage:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">func<\/span> (<span class=\"\u037cm\">c<\/span> <span class=\"\u037cg\">*<\/span><span class=\"\u037cm\">ShardedCache<\/span>) Set(<span class=\"\u037cm\">key<\/span>, <span class=\"\u037cm\">value<\/span> <span class=\"\u037cm\">string<\/span>) {\r\n    <span class=\"\u037cm\">shard<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cg\">&amp;<\/span><span class=\"\u037cm\">c<\/span><span class=\"\u037cg\">.<\/span>shards[<span class=\"\u037cm\">getShard<\/span>(<span class=\"\u037cm\">key<\/span>)]\r\n\r\n    <span class=\"\u037cm\">shard<\/span><span class=\"\u037cg\">.<\/span>mu<span class=\"\u037cg\">.<\/span>Lock()\r\n    <span class=\"\u037cm\">shard<\/span><span class=\"\u037cg\">.<\/span>data[<span class=\"\u037cm\">key<\/span>] <span class=\"\u037cg\">=<\/span> <span class=\"\u037cm\">value<\/span>\r\n    <span class=\"\u037cm\">shard<\/span><span class=\"\u037cg\">.<\/span>mu<span class=\"\u037cg\">.<\/span>Unlock()\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Now:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse\/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>100 goroutines<\/code><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n \u2193     \u2193     \u2193     \u2193\r\n\r\nShard0 Shard1 Shard2 ... Shard15\r\n Lock    Lock    Lock       Lock\r\n Map     Map     Map        Map<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ol start=\"3\" data-start=\"2537\" data-end=\"2562\">\n<li data-section-id=\"1wv9eh6\" data-start=\"2537\" data-end=\"2562\"><strong data-start=\"2540\" data-end=\"2562\">Consistent hashing<\/strong><\/li>\n<\/ol>\n<p data-start=\"2564\" data-end=\"2658\">Used in distributed systems because it minimizes data movement when adding or removing shards.<\/p>\n<hr data-start=\"2660\" data-end=\"2663\" \/>\n<h4 data-section-id=\"1344z7h\" data-start=\"2665\" data-end=\"2678\">Advantages<\/h4>\n<ul data-start=\"2680\" data-end=\"2819\">\n<li data-section-id=\"1fso1hw\" data-start=\"2680\" data-end=\"2700\">Higher concurrency<\/li>\n<li data-section-id=\"1dojirr\" data-start=\"2701\" data-end=\"2721\">Better scalability<\/li>\n<li data-section-id=\"2h4zja\" data-start=\"2722\" data-end=\"2747\">Reduced lock contention<\/li>\n<li data-section-id=\"15p6790\" data-start=\"2748\" data-end=\"2772\">Better CPU utilization<\/li>\n<li data-section-id=\"1bwxq24\" data-start=\"2773\" data-end=\"2819\">Can distribute load across multiple machines<\/li>\n<\/ul>\n<hr data-start=\"2821\" data-end=\"2824\" \/>\n<h4 data-section-id=\"1ukenwj\" data-start=\"2826\" data-end=\"2842\">Disadvantages<\/h4>\n<ul data-start=\"2844\" data-end=\"3078\">\n<li data-section-id=\"3j5fhr\" data-start=\"2844\" data-end=\"2873\">More complex implementation<\/li>\n<li data-section-id=\"1fwrow1\" data-start=\"2874\" data-end=\"2914\">Data lookup requires shard calculation<\/li>\n<li data-section-id=\"1vflh9t\" data-start=\"2915\" data-end=\"3019\">Rebalancing data when changing the number of shards can be difficult (unless using consistent hashing)<\/li>\n<li data-section-id=\"u0xioh\" data-start=\"3020\" data-end=\"3078\">Cross-shard queries or transactions are more complicated<\/li>\n<\/ul>\n<hr data-start=\"3080\" data-end=\"3083\" \/>\n<h4 data-section-id=\"motujo\" data-start=\"3085\" data-end=\"3123\">When should you use sharding in Go?<\/h4>\n<p data-start=\"3125\" data-end=\"3143\">Use sharding when:<\/p>\n<ul data-start=\"3144\" data-end=\"3400\">\n<li data-section-id=\"1d5v7t7\" data-start=\"3144\" data-end=\"3196\">You have a shared map accessed by many goroutines.<\/li>\n<li data-section-id=\"1azxf42\" data-start=\"3197\" data-end=\"3247\">A single mutex becomes a performance bottleneck.<\/li>\n<li data-section-id=\"1be7e6\" data-start=\"3248\" data-end=\"3310\">You need to scale data across multiple servers or databases.<\/li>\n<li data-section-id=\"1rt41ty\" data-start=\"3311\" data-end=\"3400\">You&#8217;re building high-performance systems like caches, rate limiters, or session stores.<\/li>\n<\/ul>\n<p data-start=\"3402\" data-end=\"3414\">Avoid it if:<\/p>\n<ul data-start=\"3415\" data-end=\"3538\">\n<li data-section-id=\"16w5kdi\" data-start=\"3415\" data-end=\"3454\">Your application has low concurrency.<\/li>\n<li data-section-id=\"dh12co\" data-start=\"3455\" data-end=\"3479\">The data set is small.<\/li>\n<li data-section-id=\"nguk47\" data-start=\"3480\" data-end=\"3538\">The added complexity outweighs the performance benefits.<\/li>\n<\/ul>\n<h2>109.Class in Golang?<\/h2>\n<p>Golang does <strong>not<\/strong> have classes.<\/p>\n<p>Instead, Go uses:<\/p>\n<ul>\n<li><strong>structs<\/strong> \u2192 data<\/li>\n<li><strong>methods on structs<\/strong> \u2192 behavior<\/li>\n<li><strong>interfaces<\/strong> \u2192 abstraction<\/li>\n<li><strong>composition<\/strong> \u2192 reuse<\/li>\n<\/ul>\n<h2>110. What is Cherry-Pick in Git?<\/h2>\n<p><strong>git cherry-pick<\/strong> is a Git command used to <strong>apply a specific commit from one branch into another branch<\/strong> <strong>without merging the entire branch<\/strong>.<\/p>\n<h2>111.Categories of Data Types in Golang.<\/h2>\n<p>Golang data types are mainly divided into <strong>four categories<\/strong>:<\/p>\n<p>1\ufe0f.Basic data types<br \/>\n2\ufe0f.Composite data types<br \/>\n3\ufe0f.Reference data types<br \/>\n4\ufe0f.nterface data type<\/p>\n<p>\u201cGolang data types include basic types like int, float, string, and bool; composite types like array, slice, struct, and map; reference types like pointer and channel; and interface types for abstraction.\u201d<\/p>\n<h2>112.Can class call within class in Golang?<\/h2>\n<p><strong>Why \u201cclass within class\u201d doesn\u2019t exist in Go<\/strong><\/p>\n<ul>\n<li>Go has <strong>no class keyword<\/strong><\/li>\n<li>No inheritance like Java<\/li>\n<li>Go follows <strong>composition over inheritance<\/strong><\/li>\n<\/ul>\n<p>1.One struct using another struct (composition)<\/p>\n<p><strong>2.Struct embedding (most Go-idiomatic way)<\/strong><\/p>\n<p>Equivalent to <em>inheritance-like behavior<\/em><\/p>\n<p>3.Using interfaces (recommended for large projects)<\/p>\n<h2>113. What is anonymous struct in Golang.<\/h2>\n<p>An anonymous struct is a struct declared without a type name and typically used for temporary or inline data structures.<br \/>\nperson := struct {<\/p>\n<p>Name string<\/p>\n<p>Age\u00a0 int<\/p>\n<p>}{Name: &#8220;Smita&#8221;,<\/p>\n<p>Age:\u00a0 25,<\/p>\n<p>}<\/p>\n<ul>\n<li>fmt.Println(person.Name)<br \/>\nNo type Person struct<\/li>\n<li>Struct is declared and initialized directly<\/li>\n<li>It exists only in that scope<\/li>\n<\/ul>\n<p><strong>\ud83d\udd39 Why Use Anonymous Struct?<\/strong><\/p>\n<p>\u2714 Temporary data<br \/>\n\u2714 JSON responses<br \/>\n\u2714 Testing<br \/>\n\u2714 One-time use structures<br \/>\n\u2714 Avoid creating unnecessary types.<\/p>\n<h2>114.What is diff between new and init keyword in Golang.<\/h2>\n<p>new is a built-in function used to allocate memory<strong>.<br \/>\n<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \uf0b7\u00a0 Allocates memory for a type<\/p>\n<p>\uf0b7\u00a0 Returns a pointer to zero value of that type<\/p>\n<p><strong>init() is a special function that runs automatically:<\/strong><\/p>\n<ul>\n<li>Before main()<\/li>\n<li>When a package is initialized<br \/>\n\uf0b7\u00a0 Cannot be called manually<\/li>\n<li>\uf0b7\u00a0 Cannot take arguments<\/li>\n<li>\uf0b7\u00a0 Cannot return values<\/li>\n<li>\uf0b7\u00a0 Can have multiple init() functions<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<td><strong>new<\/strong><\/td>\n<td><strong>make<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Allocates memory<\/td>\n<td>Initializes slice, map, channel<\/td>\n<\/tr>\n<tr>\n<td>Returns pointer<\/td>\n<td>Returns actual type<\/td>\n<\/tr>\n<tr>\n<td>Works for any type<\/td>\n<td>Only slice, map, channel<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>115.What is difference between package and module.<\/h2>\n<p>A package is a collection of Go source files in the same directory that are compiled together.<br \/>\nA package is a folder containing Go files that share the same package name.<br \/>\n<strong>A module is a collection of related Go packages.<\/strong><\/p>\n<p><strong>It is defined by a go.mod file.<\/strong><\/p>\n<p>A module is a versioned collection of Go packages.<\/p>\n<p>This defines:<\/p>\n<ul>\n<li>Module name, Go version, Dependencies<\/li>\n<\/ul>\n<h2>116. How to detect race condition happened in code<\/h2>\n<p><strong>Use Go Race Detector<br \/>\n<\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 go run -race main.go<br \/>\n<strong>Common Signs of Race Condition<\/strong><\/p>\n<p><strong>Even without race detector:<\/strong><\/p>\n<ul>\n<li><strong>Inconsistent output<\/strong><\/li>\n<li><strong>Unexpected values<\/strong><\/li>\n<li><strong>Program behaves differently each run<\/strong><\/li>\n<li><strong>Random crashes<\/strong><\/li>\n<\/ul>\n<h2>117.<strong>How to compile go code in windows and linux<\/strong><\/h2>\n<p><strong>go build main.go<\/strong><\/p>\n<p><strong>Compile Windows binary from Linux<\/strong><\/p>\n<p>GOOS=windows GOARCH=amd64 go build main.go<\/p>\n<p><strong>Compile Linux binary from Windows (PowerShell)<\/strong><\/p>\n<p>$env:GOOS=&#8221;linux&#8221;<\/p>\n<p>$env:GOARCH=&#8221;amd64&#8243;<\/p>\n<p>go build main.go<\/p>\n<h2>118.How do you handle API failure?<\/h2>\n<ul>\n<li>Retry mechanism<\/li>\n<li>Proper error messages<\/li>\n<li>Logging<\/li>\n<\/ul>\n<h2>119.what is retry mechanism.<\/h2>\n<p>A <strong>retry mechanism<\/strong> is a technique where a system <strong>automatically tries an operation again<\/strong> if it fails.<br \/>\n\uf0b7\u00a0 Network issues<\/p>\n<p>\uf0b7\u00a0 Timeout errors<\/p>\n<p>\uf0b7\u00a0 Server overload <strong>What is a Load Balancer in Golang?<\/strong><\/p>\n<p>\ud83d\udc49 A <strong>load balancer in Golang<\/strong> is a program\/service written in Go that <strong>distributes incoming requests across multiple backend servers<\/strong> to improve performance, scalability, and availability.<\/p>\n<h2>120. select in Golang.<\/h2>\n<p>select is used to handle multiple channel operations concurrently. It executes the case that is ready first.<\/p>\n<p><strong>Advantages:<\/strong><\/p>\n<ul>\n<li>Non-blocking concurrency<\/li>\n<li>Timeout handling<\/li>\n<li>Efficient goroutine coordination<\/li>\n<\/ul>\n<h2><span style=\"font-size: 12.0pt; line-height: 115%;\">121.How do you handle high traffic?<\/span><\/h2>\n<ul>\n<li>Load balancing<\/li>\n<li>Caching<\/li>\n<li>DB indexing<\/li>\n<li>Horizontal scaling<\/li>\n<\/ul>\n<h2><span style=\"font-size: 12.0pt; line-height: 115%;\">122.How do you debug production issues?<\/span><\/h2>\n<ul>\n<li>Logs<\/li>\n<li>Monitoring (CloudWatch)<\/li>\n<li>Metrics<\/li>\n<li>Tracing<\/li>\n<\/ul>\n<h2>123.How do you ensure code quality?<\/h2>\n<ul>\n<li>Unit testing<\/li>\n<li>Code reviews<\/li>\n<li>Linting<\/li>\n<li>CI pipeline<\/li>\n<\/ul>\n<h2>124.Why Load Balancing is Needed.<\/h2>\n<ul>\n<li>Prevent server overload<\/li>\n<li>Improve response time<\/li>\n<li>Ensure high availability<\/li>\n<li>Handle high traffic<\/li>\n<\/ul>\n<p><strong>\ud83d\udd04 How It Works<\/strong><\/p>\n<ol>\n<li>Client sends request<\/li>\n<li>Load balancer receives it<\/li>\n<li>Chooses a server<\/li>\n<li>Forwards request<\/li>\n<li>Sends response back<\/li>\n<\/ol>\n<p>Types of Load Balancing<br \/>\n<strong>1. Round Robin<\/strong><\/p>\n<ul>\n<li>Requests distributed one by one<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong> Least Connections<\/strong><\/li>\n<\/ol>\n<ul>\n<li>Sends request to server with least active connections<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong> IP Hash<\/strong><\/li>\n<\/ol>\n<ul>\n<li>Same user \u2192 same server<\/li>\n<\/ul>\n<ol start=\"4\">\n<li><strong> Weighted Load Balancing<\/strong><\/li>\n<\/ol>\n<ul>\n<li>More powerful server gets more traffic<\/li>\n<\/ul>\n<h2>125.What is stateless Api.<\/h2>\n<p>A <strong>stateless API<\/strong> is an API where <strong>each request from the client contains all the information needed to process it<\/strong>, and the server <strong>does not store any client session data between requests.<\/strong><\/p>\n<p>Why Stateless APIs are Important<\/p>\n<ul>\n<li>Horizontal scaling (add more servers easily)<\/li>\n<li>Fault tolerance<\/li>\n<li>Better performance<\/li>\n<li>Works well with microservices<\/li>\n<\/ul>\n<h2>126.What is routing in Golang.<\/h2>\n<p><strong>Routing in Golang<\/strong> means <strong>mapping an incoming HTTP request (URL + method) to a specific handler function<\/strong> that processes it and returns a response.<\/p>\n<p><strong>How Routing Works<\/strong><\/p>\n<ol>\n<li>Client sends request (URL + method)<\/li>\n<li>Router matches path<\/li>\n<li>Calls corresponding handler<\/li>\n<li>Sends response<\/li>\n<\/ol>\n<p>Types of Routing<br \/>\nStatic Routing<br \/>\nDynamic Routing<br \/>\nQuery Parameters<\/p>\n<h2>127. what is diff between httphandlefunc and httpmethod<\/h2>\n<p>http.HandleFunc is used to register a route in Go\u2019s net\/http package, mapping a URL to a handler function. HTTP methods like GET and POST define the type of operation on that route. With HandleFunc, we manually check the method using r.Method, whereas frameworks like Gin or Fiber provide built-in method-based routing.<br \/>\nAns:-<\/p>\n<table>\n<thead>\n<tr>\n<td><strong>Concept<\/strong><\/td>\n<td colspan=\"2\"><strong>Meaning<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>http.HandleFunc<\/td>\n<td colspan=\"2\">Registers a route (URL \u2192 handler)- net\/http package<\/td>\n<\/tr>\n<tr>\n<td>HTTP Method<\/td>\n<td colspan=\"2\">Defines the type of request (GET, POST, PUT, DELETE)<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">&nbsp;<\/p>\n<p><strong>Problem with http.HandleFunc<\/strong><\/p>\n<ul>\n<li>No built-in method separation<\/li>\n<li>You must manually write conditions<\/li>\n<li>Not scalable for large APIs<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>128. What is slide window in DS<\/h2>\n<p><strong>Sliding Window<\/strong> is a technique used in Data Structures and Algorithms to <strong>process a subset (window) of elements in an array or string efficiently<\/strong> by moving the window step-by-step instead of recalculating everything.<\/p>\n<h2>129.Why Use Sliding Window?<\/h2>\n<table>\n<tbody>\n<tr>\n<td>\n<ul>\n<li>Reduces time complexity<\/li>\n<li>Avoids nested loops<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Converts <strong>O(n\u00b2) \u2192 O(n)<\/strong><\/p>\n<p><strong>Types of Sliding Window<\/strong><\/p>\n<ol>\n<li><strong> Fixed Size Window<\/strong><\/li>\n<\/ol>\n<p>Window size remains constant<\/p>\n<p>\ud83d\udc49 Example: max sum of subarray of size k<\/p>\n<ol start=\"2\">\n<li><strong> Variable Size Window<\/strong><\/li>\n<\/ol>\n<p>Window expands\/shrinks dynamically<\/p>\n<p>\ud83d\udc49 Example: longest substring without repeating characters<\/p>\n<h2><strong>130.What is a goroutine leak?<\/strong><\/h2>\n<p>When goroutines keep running but are no longer needed \u2192 memory leak.<\/p>\n<p>\ud83d\udc49 Fix:<\/p>\n<ul>\n<li>Use context.WithCancel<\/li>\n<li>Proper channel closing<\/li>\n<li>Avoid infinite loops<\/li>\n<\/ul>\n<h1><span style=\"font-size: 14.0pt; line-height: 115%;\">131.How to design high-scale low-latency system?<\/span><\/h1>\n<ul>\n<li>Caching (Redis)<\/li>\n<li>Load balancing<\/li>\n<li>Horizontal scaling<\/li>\n<li>Async processing (Kafka)<\/li>\n<li>DB indexing<\/li>\n<li>CDN<\/li>\n<\/ul>\n<h2><span style=\"font-size: 14.0pt; line-height: 115%;\">132.How to reduce latency?<\/span><\/h2>\n<ul>\n<li>Use cache<\/li>\n<li>Reduce DB calls<\/li>\n<li>Use connection pooling<\/li>\n<li>Optimize queries<\/li>\n<\/ul>\n<h2>133.What is context switching?<\/h2>\n<p>Switching CPU from one process\/thread to another..<\/p>\n<h2>134. What is Variable Scope?<\/h2>\n<p>Variable scope is the region of a program where a variable is <strong>declared and can be used<\/strong>.<\/p>\n<h2><strong>135. what is connection pool in Golang<\/strong><\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"219\">A <strong data-start=\"2\" data-end=\"21\">connection pool<\/strong> in Golang is a collection of reusable connections (typically database or network connections) that are kept open and shared across multiple requests instead of creating a new connection every time.<\/p>\n<p data-start=\"221\" data-end=\"424\">Creating a new database connection is expensive because it involves network communication, authentication, and resource allocation. A connection pool improves performance by reusing existing connections.<\/p>\n<h3 data-section-id=\"1w16shl\" data-start=\"426\" data-end=\"442\">How it works<\/h3>\n<p data-start=\"444\" data-end=\"493\">Suppose your application receives 1,000 requests:<\/p>\n<ul data-start=\"495\" data-end=\"967\">\n<li data-section-id=\"isdnbj\" data-start=\"495\" data-end=\"718\"><strong data-start=\"497\" data-end=\"526\">Without a connection pool<\/strong>\n<ul data-start=\"529\" data-end=\"718\">\n<li data-section-id=\"8n3f3x\" data-start=\"529\" data-end=\"595\">Request 1 \u2192 Create connection \u2192 Execute query \u2192 Close connection<\/li>\n<li data-section-id=\"1817bdq\" data-start=\"598\" data-end=\"664\">Request 2 \u2192 Create connection \u2192 Execute query \u2192 Close connection<\/li>\n<li data-section-id=\"1o23ba\" data-start=\"667\" data-end=\"672\">&#8230;<\/li>\n<li data-section-id=\"12wb6yt\" data-start=\"675\" data-end=\"718\">This is slow and consumes more resources.<\/li>\n<\/ul>\n<\/li>\n<li data-section-id=\"1ackqzf\" data-start=\"720\" data-end=\"967\"><strong data-start=\"722\" data-end=\"748\">With a connection pool<\/strong>\n<ul data-start=\"751\" data-end=\"967\">\n<li data-section-id=\"xxjtne\" data-start=\"751\" data-end=\"815\">At startup, Go creates (or lazily opens) a set of connections.<\/li>\n<li data-section-id=\"lfee8\" data-start=\"818\" data-end=\"875\">Request 1 uses Connection 1 and returns it to the pool.<\/li>\n<li data-section-id=\"kw0me6\" data-start=\"878\" data-end=\"923\">Request 2 uses Connection 2 and returns it.<\/li>\n<li data-section-id=\"kcjb1a\" data-start=\"926\" data-end=\"967\">Other requests reuse these connections.<br \/>\n<h2 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"s4ua74\" data-start=\"1334\" data-end=\"1358\">Connection Pool in Go<\/h2>\n<p data-start=\"1360\" data-end=\"1427\">The <code data-start=\"1364\" data-end=\"1378\">database\/sql<\/code> package automatically manages a connection pool.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">db<\/span>, <span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">sql<\/span><span class=\"\u037cg\">.<\/span>Open(<span class=\"\u037ck\">\"postgres\"<\/span>, <span class=\"\u037cm\">connStr<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">if<\/span> <span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">!=<\/span> <span class=\"\u037cj\">nil<\/span> {\r\n    <span class=\"\u037cm\">log<\/span><span class=\"\u037cg\">.<\/span>Fatal(<span class=\"\u037cm\">err<\/span>)\r\n}\r\n<span class=\"\u037cg\">defer<\/span> <span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>Close()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">You don&#8217;t need to manually create or manage individual database connections.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 data-section-id=\"5lfamy\" data-start=\"1613\" data-end=\"1639\">Important Pool Settings<\/h2>\n<h3 data-section-id=\"i0u5kw\" data-start=\"1641\" data-end=\"1672\">1. Maximum Open Connections<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetMaxOpenConns(<span class=\"\u037cj\">20<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"\">At most <strong data-start=\"1716\" data-end=\"1722\">20<\/strong> database connections can be open simultaneously.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 data-section-id=\"52g33n\" data-start=\"1773\" data-end=\"1804\">2. Maximum Idle Connections<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetMaxIdleConns(<span class=\"\u037cj\">5<\/span>)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"1839\" data-end=\"1884\">Keeps <strong data-start=\"1845\" data-end=\"1867\">5 idle connections<\/strong> ready for reuse.<\/p>\n<h3 data-section-id=\"1bi475i\" data-start=\"1886\" data-end=\"1920\">3. Maximum Connection Lifetime<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetConnMaxLifetime(<span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Hour)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"\">Recreates a connection after it has been open for 1 hour.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 data-section-id=\"8iq39h\" data-start=\"2025\" data-end=\"2049\">4. Maximum Idle Time<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetConnMaxIdleTime(<span class=\"\u037cj\">10<\/span> <span class=\"\u037cg\">*<\/span> <span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Minute)<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"\">Closes idle connections after 10 minutes.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 data-section-id=\"1wifc04\" data-start=\"2145\" data-end=\"2164\">\u00a0Example<\/h2>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">package<\/span> <span class=\"\u037cm\">main<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>\r\n<span class=\"\u037cg\">import<\/span> (\r\n\t<span class=\"\u037ck\">\"database\/sql\"<\/span>\r\n\t<span class=\"\u037ck\">\"log\"<\/span>\r\n\t<span class=\"\u037ck\">\"time\"<\/span>\r\n\r\n\t<span class=\"\u037cm\">_<\/span> <span class=\"\u037ck\">\"github.com\/lib\/pq\"<\/span>\r\n)\r\n\r\n<span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">main<\/span>() {\r\n\t<span class=\"\u037cm\">db<\/span>, <span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">sql<\/span><span class=\"\u037cg\">.<\/span>Open(<span class=\"\u037ck\">\"postgres\"<\/span>, <span class=\"\u037ck\">\"host=localhost user=postgres password=123 dbname=test sslmode=disable\"<\/span>)\r\n\t<span class=\"\u037cg\">if<\/span> <span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">!=<\/span> <span class=\"\u037cj\">nil<\/span> {\r\n\t\t<span class=\"\u037cm\">log<\/span><span class=\"\u037cg\">.<\/span>Fatal(<span class=\"\u037cm\">err<\/span>)\r\n\t}\r\n\t<span class=\"\u037cg\">defer<\/span> <span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>Close()\r\n\r\n\t<span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetMaxOpenConns(<span class=\"\u037cj\">20<\/span>)\r\n\t<span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetMaxIdleConns(<span class=\"\u037cj\">5<\/span>)\r\n\t<span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetConnMaxLifetime(<span class=\"\u037cj\">1<\/span> <span class=\"\u037cg\">*<\/span> <span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Hour)\r\n\t<span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetConnMaxIdleTime(<span class=\"\u037cj\">10<\/span> <span class=\"\u037cg\">*<\/span> <span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Minute)\r\n\r\n\t<span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">=<\/span> <span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>Ping()\r\n\t<span class=\"\u037cg\">if<\/span> <span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">!=<\/span> <span class=\"\u037cj\">nil<\/span> {\r\n\t\t<span class=\"\u037cm\">log<\/span><span class=\"\u037cg\">.<\/span>Fatal(<span class=\"\u037cm\">err<\/span>)\r\n\t}\r\n\r\n\t<span class=\"\u037cm\">log<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037ck\">\"Database connected successfully\"<\/span>)\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">Benefits<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<ul data-start=\"2679\" data-end=\"2950\">\n<li data-section-id=\"uvjagc\" data-start=\"2679\" data-end=\"2731\">Faster performance because connections are reused.<\/li>\n<li data-section-id=\"166rvj\" data-start=\"2732\" data-end=\"2791\">Reduces the overhead of creating and closing connections.<\/li>\n<li data-section-id=\"17n705m\" data-start=\"2792\" data-end=\"2849\">Limits the number of simultaneous database connections.<\/li>\n<li data-section-id=\"2r2wz0\" data-start=\"2850\" data-end=\"2903\">Improves scalability for high-traffic applications.<\/li>\n<li data-section-id=\"1qxcwa8\" data-start=\"2904\" data-end=\"2950\">Helps prevent exhausting database resources.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<h2>136.Can multiple goroutines acquire the same mutex?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"2634\" data-end=\"2726\">No. Only one goroutine can hold a <code data-start=\"2668\" data-end=\"2680\">sync.Mutex<\/code> at a time. Others block until it is unlocked.<\/p>\n<\/li>\n<\/ul>\n<h2>\u00a0 \u00a0 137. What happens if you forget to unlock a mutex?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"2787\" data-end=\"2880\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Other goroutines waiting on that mutex will block indefinitely, which can lead to a deadlock.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><span style=\"font-family: Consolas, Monaco, monospace;\">\u00a0 \u00a0\u00a0 \u00a0 <\/span><code><span class=\"\u037cm\">mu<\/span><span class=\"\u037cg\">.<\/span>Lock()<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<h5 class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">\u00a0 \u00a0 \u00a0 \u00a0 defer<\/span> <span class=\"\u037cm\">mu<\/span><span class=\"\u037cg\">.<\/span>Unlock()<\/code><\/h5>\n<h2 class=\"cm-content q9tKkq_readonly m-0\"><code>138.Mutex vs RWMutex<\/code><\/h2>\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"3053\" data-end=\"3321\">\n<thead data-start=\"3053\" data-end=\"3072\">\n<tr data-start=\"3053\" data-end=\"3072\">\n<th class=\"last:pe-10\" data-start=\"3053\" data-end=\"3061\" data-col-size=\"sm\">Mutex<\/th>\n<th class=\"last:pe-10\" data-start=\"3061\" data-end=\"3072\" data-col-size=\"md\">RWMutex<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"3094\" data-end=\"3321\">\n<tr data-start=\"3094\" data-end=\"3179\">\n<td data-start=\"3094\" data-end=\"3136\" data-col-size=\"sm\">One goroutine at a time (read or write)<\/td>\n<td data-start=\"3136\" data-end=\"3179\" data-col-size=\"md\">Multiple readers allowed simultaneously<\/td>\n<\/tr>\n<tr data-start=\"3180\" data-end=\"3225\">\n<td data-start=\"3180\" data-end=\"3190\" data-col-size=\"sm\">Simpler<\/td>\n<td data-start=\"3190\" data-end=\"3225\" data-col-size=\"md\">Better for read-heavy workloads<\/td>\n<\/tr>\n<tr data-start=\"3226\" data-end=\"3321\">\n<td data-start=\"3226\" data-end=\"3250\" data-col-size=\"sm\"><code data-start=\"3228\" data-end=\"3236\">Lock()<\/code> \/ <code data-start=\"3239\" data-end=\"3249\">Unlock()<\/code><\/td>\n<td data-start=\"3250\" data-end=\"3321\" data-col-size=\"md\"><code data-start=\"3252\" data-end=\"3261\">RLock()<\/code> \/ <code data-start=\"3264\" data-end=\"3275\">RUnlock()<\/code> for reads, <code data-start=\"3287\" data-end=\"3295\">Lock()<\/code> \/ <code data-start=\"3298\" data-end=\"3308\">Unlock()<\/code> for writes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>139.what is deadlock<\/h2>\n<p>A <strong data-start=\"2\" data-end=\"14\">deadlock<\/strong> is a situation where <strong data-start=\"36\" data-end=\"86\">two or more goroutines are permanently blocked<\/strong>, waiting for each other (or waiting for an event that will never happen). As a result, the program cannot make further progress.<\/p>\n<p>ex. sending to a channel with no receiver<\/p>\n<p><span class=\"\u037cg\">package<\/span> <span class=\"\u037cm\">main<\/span><\/p>\n<p><span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">main<\/span>() {<\/p>\n<p><span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">chan<\/span> <span class=\"\u037cm\">int<\/span>) <span class=\"\u037cm\">ch<\/span> <span class=\"\u037cg\">&lt;-<\/span> <span class=\"\u037cj\">10<\/span><\/p>\n<p>}<\/p>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"860\" data-end=\"868\"><strong data-start=\"860\" data-end=\"868\">Why?<\/strong><\/p>\n<ul data-start=\"870\" data-end=\"980\">\n<li data-section-id=\"1uz6y7q\" data-start=\"870\" data-end=\"914\">An unbuffered channel requires a receiver.<\/li>\n<li data-section-id=\"seuqzp\" data-start=\"915\" data-end=\"943\">No goroutine is receiving.<\/li>\n<li data-section-id=\"1eov5ir\" data-start=\"944\" data-end=\"980\">The send operation blocks forever.<\/li>\n<\/ul>\n<h2>140. what is escape analysis in Golang<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"137\"><strong data-start=\"0\" data-end=\"19\">Escape analysis<\/strong> is a compiler optimization in Go that determines <strong data-start=\"69\" data-end=\"136\">whether a variable should be allocated on the stack or the heap<\/strong>.<\/p>\n<ul data-start=\"139\" data-end=\"290\">\n<li data-section-id=\"jszys2\" data-start=\"139\" data-end=\"218\"><strong data-start=\"141\" data-end=\"161\">Stack allocation<\/strong> \u2192 Faster, automatically freed when the function returns.<\/li>\n<li data-section-id=\"1pzwyhn\" data-start=\"219\" data-end=\"290\"><strong data-start=\"221\" data-end=\"240\">Heap allocation<\/strong> \u2192 Slower, managed later by the garbage collector.<\/li>\n<\/ul>\n<p data-start=\"292\" data-end=\"349\">The Go compiler performs escape analysis at compile time.<\/p>\n<hr data-start=\"351\" data-end=\"354\" \/>\n<h3 data-section-id=\"qb96lt\" data-start=\"356\" data-end=\"371\">Stack vs Heap<\/h3>\n<h4 data-section-id=\"77ckx4\" data-start=\"373\" data-end=\"382\">Stack<\/h4>\n<ul>\n<li data-section-id=\"77ckx4\" data-start=\"373\" data-end=\"382\">\u00a0 Fast allocation and deallocation.<\/li>\n<li data-section-id=\"77ckx4\" data-start=\"373\" data-end=\"382\">\u00a0Memory is automatically released when the function returns.<\/li>\n<li data-section-id=\"1utr21q\" data-start=\"482\" data-end=\"541\">Used for local variables that don&#8217;t outlive the function.<\/li>\n<li data-section-id=\"1utr21q\" data-start=\"482\" data-end=\"541\"><strong>Heap<\/strong><\/li>\n<li data-section-id=\"10z87gm\" data-start=\"553\" data-end=\"573\">Slower allocation.<\/li>\n<li data-section-id=\"13vdfgp\" data-start=\"574\" data-end=\"624\">Memory is managed by the garbage collector (GC).<\/li>\n<li data-section-id=\"c1eu7n\" data-start=\"625\" data-end=\"681\">Used when data needs to live beyond the function call.<\/li>\n<\/ul>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1vck9tc\" data-start=\"1885\" data-end=\"1920\">Why is Escape Analysis Important?<\/h3>\n<ul data-start=\"1922\" data-end=\"2111\">\n<li data-section-id=\"18k82y2\" data-start=\"1922\" data-end=\"1949\">Reduces heap allocations.<\/li>\n<li data-section-id=\"wq3yr7\" data-start=\"1950\" data-end=\"1987\">Lowers garbage collection overhead.<\/li>\n<li data-section-id=\"amb13h\" data-start=\"1988\" data-end=\"2023\">Improves application performance.<\/li>\n<li data-section-id=\"1pl3uvf\" data-start=\"2024\" data-end=\"2111\">Helps Go automatically manage memory efficiently without requiring manual allocation.<\/li>\n<\/ul>\n<h2>141.what is leak memeory?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"226\">A <strong data-start=\"2\" data-end=\"17\">memory leak<\/strong> occurs when a program <strong data-start=\"40\" data-end=\"92\">continues to hold memory that it no longer needs<\/strong>, so that memory cannot be reclaimed. Over time, memory usage grows unnecessarily, which can lead to poor performance or even crashes.<\/p>\n<p data-start=\"228\" data-end=\"331\">In Go, memory leaks are different from languages like C\/C++ because Go has <strong data-start=\"303\" data-end=\"330\">garbage collection (GC)<\/strong>.<\/p>\n<ul data-start=\"333\" data-end=\"551\">\n<li data-section-id=\"mja87p\" data-start=\"333\" data-end=\"405\"><strong data-start=\"335\" data-end=\"345\">C\/C++:<\/strong> A memory leak often means allocated memory was never freed.<\/li>\n<li data-section-id=\"1mtlwn6\" data-start=\"406\" data-end=\"551\"><strong data-start=\"408\" data-end=\"415\">Go:<\/strong> A memory leak usually means your program still has references to objects it no longer needs, so the garbage collector cannot free them.<\/li>\n<li data-section-id=\"1mtlwn6\" data-start=\"406\" data-end=\"551\">\n<h4 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1pxu7y0\" data-start=\"558\" data-end=\"598\">Example<\/h4>\n<\/li>\n<\/ul>\n<h5 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1pxu7y0\" data-start=\"558\" data-end=\"598\">\u00a0 \u00a0 \u00a0 \u00a0Global Slice Keeps Growing<\/h5>\n<pre class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1pxu7y0\" data-start=\"558\" data-end=\"598\"><span style=\"font-size: 16px;\">   Holding References<\/span><\/pre>\n<h2 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"yeeui9\" data-start=\"1722\" data-end=\"1752\">How to Prevent Memory Leaks<\/h2>\n<ul data-start=\"1754\" data-end=\"2081\">\n<li data-section-id=\"13g8ylp\" data-start=\"1754\" data-end=\"1811\">Remove references to objects that are no longer needed.<\/li>\n<li data-section-id=\"1j2d5u1\" data-start=\"1812\" data-end=\"1866\">Close files, database rows, and network connections.<\/li>\n<li data-section-id=\"1sr6dlw\" data-start=\"1867\" data-end=\"1931\">Cancel goroutines using <code data-start=\"1893\" data-end=\"1910\">context.Context<\/code> or proper signaling.<\/li>\n<li data-section-id=\"1fapgdp\" data-start=\"1932\" data-end=\"1984\">Avoid unbounded growth of slices, maps, or caches.<\/li>\n<li data-section-id=\"1vy0sfn\" data-start=\"1985\" data-end=\"2019\">Close channels when appropriate.<\/li>\n<li data-section-id=\"9zrca1\" data-start=\"2020\" data-end=\"2081\">Monitor memory usage using profiling tools such as <code data-start=\"2073\" data-end=\"2080\">pprof<\/code>.<\/li>\n<\/ul>\n<h2 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"19n7nn7\" data-start=\"2088\" data-end=\"2113\">Detecting Memory Leaks<\/h2>\n<p data-start=\"2115\" data-end=\"2149\">Use Go&#8217;s built-in profiling tools:<\/p>\n<h3 data-section-id=\"1ak95e4\" data-start=\"2151\" data-end=\"2169\">Memory Profile<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code>go tool pprof http:\/\/localhost:6060\/debug\/pprof\/heap<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 data-section-id=\"1a1vyij\" data-start=\"2237\" data-end=\"2258\">Goroutine Profile<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code>go tool pprof http:\/\/localhost:6060\/debug\/pprof\/goroutine<\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"\">\n<div><\/div>\n<div class=\"\">You can also monitor memory with:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><code><span class=\"\u037cg\">import<\/span> <span class=\"\u037ck\">\"runtime\"<\/span><\/code><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">var<\/span> <span class=\"\u037cm\">m<\/span> <span class=\"\u037cm\">runtime<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">MemStats<\/span>\r\n<span class=\"\u037cm\">runtime<\/span><span class=\"\u037cg\">.<\/span>ReadMemStats(<span class=\"\u037cg\">&amp;<\/span><span class=\"\u037cm\">m<\/span>)\r\n<span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037cm\">m<\/span><span class=\"\u037cg\">.<\/span>Alloc)\r\n<\/code><\/pre>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"x92yfh\" data-start=\"2713\" data-end=\"2786\">142.What is the difference between a memory leak and a goroutine leak?<\/h3>\n<div class=\"TyagGW_tableContainer\">\n<div class=\"group TyagGW_tableWrapper flex flex-col-reverse w-fit\" tabindex=\"-1\">\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"2788\" data-end=\"3107\">\n<thead data-start=\"2788\" data-end=\"2820\">\n<tr data-start=\"2788\" data-end=\"2820\">\n<th class=\"last:pe-10\" data-start=\"2788\" data-end=\"2802\" data-col-size=\"md\">Memory Leak<\/th>\n<th class=\"last:pe-10\" data-start=\"2802\" data-end=\"2820\" data-col-size=\"md\">Goroutine Leak<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"2854\" data-end=\"3107\">\n<tr data-start=\"2854\" data-end=\"2961\">\n<td data-start=\"2854\" data-end=\"2913\" data-col-size=\"md\">Unused memory remains referenced and cannot be collected<\/td>\n<td data-start=\"2913\" data-end=\"2961\" data-col-size=\"md\">Goroutines remain blocked or running forever<\/td>\n<\/tr>\n<tr data-start=\"2962\" data-end=\"3033\">\n<td data-start=\"2962\" data-end=\"2985\" data-col-size=\"md\">Increases heap usage<\/td>\n<td data-start=\"2985\" data-end=\"3033\" data-col-size=\"md\">Increases goroutine count and resource usage<\/td>\n<\/tr>\n<tr data-start=\"3034\" data-end=\"3107\">\n<td data-start=\"3034\" data-end=\"3058\" data-col-size=\"md\">Affects GC and memory<\/td>\n<td data-start=\"3058\" data-end=\"3107\" data-col-size=\"md\">Affects memory, scheduling, and CPU resources<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>143.I have 1000 request what services used in Golang or Aws how can I handle?<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">To handle 1,000 concurrent requests, I would deploy multiple instances of my Go application behind an AWS Application Load Balancer. Since Go handles each HTTP request in its own goroutine, it can efficiently process many concurrent requests. I would configure a database connection pool to avoid opening a new connection per request and use Redis to cache frequently accessed data, reducing database load. For CPU-intensive or long-running tasks, I&#8217;d use a worker pool in Go or offload the work to Amazon SQS with background workers. On AWS, I&#8217;d enable Auto Scaling so additional instances are created automatically when traffic increases. Files would be stored in Amazon S3, and the database would be hosted on Amazon RDS or Aurora with read replicas if needed. This architecture is scalable, resilient, and suitable for production workloads.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1kkopnn\" data-start=\"691\" data-end=\"705\">Goroutines<\/h3>\n<p data-start=\"707\" data-end=\"757\">Each HTTP request is handled in its own goroutine.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">handler<\/span>(<span class=\"\u037cm\">w<\/span> <span class=\"\u037cm\">http<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">ResponseWriter<\/span>, <span class=\"\u037cm\">r<\/span> <span class=\"\u037cg\">*<\/span><span class=\"\u037cm\">http<\/span><span class=\"\u037cg\">.<\/span><span class=\"\u037cm\">Request<\/span>) {\r\n    <span class=\"\u037ce\">\/\/ Each request runs concurrently<\/span>\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"865\" data-end=\"955\">Go can efficiently handle thousands of concurrent goroutines because they are lightweight.<\/p>\n<hr data-start=\"957\" data-end=\"960\" \/>\n<h3 data-section-id=\"atw9xo\" data-start=\"962\" data-end=\"977\">Worker Pool<\/h3>\n<p data-start=\"979\" data-end=\"1078\">If processing is CPU-intensive or involves background work, use a worker pool to limit concurrency.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">jobs<\/span> <span class=\"\u037cg\">:=<\/span> make(<span class=\"\u037cg\">chan<\/span> <span class=\"\u037cm\">Job<\/span>, <span class=\"\u037cj\">100<\/span>)\r\n\r\n<span class=\"\u037cg\">for<\/span> <span class=\"\u037cm\">i<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cj\">0<\/span>; <span class=\"\u037cm\">i<\/span> <span class=\"\u037cg\">&lt;<\/span> <span class=\"\u037cj\">10<\/span>; <span class=\"\u037cm\">i<\/span><span class=\"\u037cg\">++<\/span> {\r\n    <span class=\"\u037cg\">go<\/span> <span class=\"\u037cm\">worker<\/span>(<span class=\"\u037cm\">jobs<\/span>)\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"1168\" data-end=\"1225\">This prevents creating an unbounded number of goroutines.<\/p>\n<hr data-start=\"1227\" data-end=\"1230\" \/>\n<h3 data-section-id=\"1sdbyq1\" data-start=\"1232\" data-end=\"1260\">Database Connection Pool<\/h3>\n<p data-start=\"1262\" data-end=\"1324\">Reuse database connections instead of opening one per request.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetMaxOpenConns(<span class=\"\u037cj\">100<\/span>)\r\n<span class=\"\u037cm\">db<\/span><span class=\"\u037cg\">.<\/span>SetMaxIdleConns(<span class=\"\u037cj\">20<\/span>)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"1384\" data-end=\"1387\" \/>\n<h3 data-section-id=\"hybg8j\" data-start=\"1389\" data-end=\"1414\">Context with Timeouts<\/h3>\n<p data-start=\"1416\" data-end=\"1447\">Avoid requests hanging forever.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute inset-x-4 top-12 bottom-4\">\n<div class=\"pointer-events-none sticky z-40 shrink-0 z-1!\">\n<div class=\"sticky bg-token-border-light\"><\/div>\n<\/div>\n<\/div>\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"\">\n<div class=\"relative\">\n<div class=\"\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><span class=\"\u037cm\">ctx<\/span>, <span class=\"\u037cm\">cancel<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">context<\/span><span class=\"\u037cg\">.<\/span>WithTimeout(<span class=\"\u037cm\">context<\/span><span class=\"\u037cg\">.<\/span>Background(), <span class=\"\u037cj\">5<\/span><span class=\"\u037cg\">*<\/span><span class=\"\u037cm\">time<\/span><span class=\"\u037cg\">.<\/span>Second)\r\n<span class=\"\u037cg\">defer<\/span> <span class=\"\u037cm\">cancel<\/span>()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"1547\" data-end=\"1550\" \/>\n<h3 data-section-id=\"1jsy6wz\" data-start=\"1552\" data-end=\"1567\">Redis Cache<\/h3>\n<p data-start=\"1569\" data-end=\"1624\">Cache frequently accessed data to reduce database load.<\/p>\n<p data-start=\"1626\" data-end=\"1634\">Example:<\/p>\n<ul data-start=\"1636\" data-end=\"1682\">\n<li data-section-id=\"1stejdt\" data-start=\"1636\" data-end=\"1650\">Product list<\/li>\n<li data-section-id=\"os3uz6\" data-start=\"1651\" data-end=\"1665\">User profile<\/li>\n<li data-section-id=\"1xurdss\" data-start=\"1666\" data-end=\"1682\">Dashboard data<\/li>\n<\/ul>\n<hr data-start=\"1684\" data-end=\"1687\" \/>\n<h2 data-section-id=\"1m313bm\" data-start=\"1689\" data-end=\"1707\">2. AWS Services<\/h2>\n<h3 data-section-id=\"eavz7k\" data-start=\"1709\" data-end=\"1744\">Application Load Balancer (ALB)<\/h3>\n<p data-start=\"1746\" data-end=\"1817\">Distributes incoming requests across multiple Go application instances.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>1000 Requests\r\n      |\r\n      v\r\n     ALB\r\n   \/  |   \\\r\nApp1 App2 App3<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"1894\" data-end=\"1897\" \/>\n<h3 data-section-id=\"114ikgb\" data-start=\"1899\" data-end=\"1921\">Auto Scaling Group<\/h3>\n<p data-start=\"1923\" data-end=\"2025\">Automatically adds or removes EC2 instances based on metrics such as CPU utilization or request count.<\/p>\n<p data-start=\"2027\" data-end=\"2039\">For example:<\/p>\n<ul data-start=\"2041\" data-end=\"2107\">\n<li data-section-id=\"1wapgiu\" data-start=\"2041\" data-end=\"2063\">2 instances normally<\/li>\n<li data-section-id=\"7jdats\" data-start=\"2064\" data-end=\"2107\">Scale to 10 instances during high traffic<\/li>\n<\/ul>\n<hr data-start=\"2109\" data-end=\"2112\" \/>\n<h3 data-section-id=\"16ahie\" data-start=\"2114\" data-end=\"2148\">Amazon ECS or Kubernetes (EKS)<\/h3>\n<p data-start=\"2150\" data-end=\"2227\">Instead of running directly on EC2, deploy your Go application as containers.<\/p>\n<p data-start=\"2229\" data-end=\"2238\">Benefits:<\/p>\n<ul data-start=\"2240\" data-end=\"2314\">\n<li data-section-id=\"fistqj\" data-start=\"2240\" data-end=\"2260\">Easier deployments<\/li>\n<li data-section-id=\"e17b8h\" data-start=\"2261\" data-end=\"2276\">Health checks<\/li>\n<li data-section-id=\"u2bdxk\" data-start=\"2277\" data-end=\"2296\">Automatic scaling<\/li>\n<li data-section-id=\"1ulfx5j\" data-start=\"2297\" data-end=\"2314\">Rolling updates<\/li>\n<\/ul>\n<hr data-start=\"2316\" data-end=\"2319\" \/>\n<h3 data-section-id=\"1c5wxds\" data-start=\"2321\" data-end=\"2344\">Amazon RDS \/ Aurora<\/h3>\n<p data-start=\"2346\" data-end=\"2380\">Use a managed relational database.<\/p>\n<p data-start=\"2382\" data-end=\"2392\">Configure:<\/p>\n<ul data-start=\"2394\" data-end=\"2487\">\n<li data-section-id=\"8g7oey\" data-start=\"2394\" data-end=\"2434\">Read replicas for read-heavy workloads<\/li>\n<li data-section-id=\"rrmsoy\" data-start=\"2435\" data-end=\"2455\">Connection pooling<\/li>\n<li data-section-id=\"q5m22p\" data-start=\"2456\" data-end=\"2487\">Backups and high availability<\/li>\n<\/ul>\n<hr data-start=\"2489\" data-end=\"2492\" \/>\n<h3 data-section-id=\"gds7xu\" data-start=\"2494\" data-end=\"2524\">Amazon ElastiCache (Redis)<\/h3>\n<p data-start=\"2526\" data-end=\"2558\">Cache frequently requested data.<\/p>\n<p data-start=\"2560\" data-end=\"2569\">Benefits:<\/p>\n<ul data-start=\"2571\" data-end=\"2618\">\n<li data-section-id=\"1mwpmt6\" data-start=\"2571\" data-end=\"2594\">Faster response times<\/li>\n<li data-section-id=\"1jk4yb3\" data-start=\"2595\" data-end=\"2618\">Reduced database load<\/li>\n<\/ul>\n<hr data-start=\"2620\" data-end=\"2623\" \/>\n<h3 data-section-id=\"gay41c\" data-start=\"2625\" data-end=\"2638\">Amazon S3<\/h3>\n<p data-start=\"2640\" data-end=\"2730\">Store uploaded files, images, PDFs, logs, etc., rather than keeping them on EC2 instances.<\/p>\n<hr data-start=\"2732\" data-end=\"2735\" \/>\n<h3 data-section-id=\"14th8xt\" data-start=\"2737\" data-end=\"2751\">Amazon SQS<\/h3>\n<p data-start=\"2753\" data-end=\"2894\">If a request triggers long-running work (email sending, report generation, image processing), enqueue it instead of processing synchronously.<\/p>\n<\/div>\n<h2 data-start=\"2753\" data-end=\"2894\">144. what is ECS<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"242\"><strong data-start=\"0\" data-end=\"42\">Amazon ECS (Elastic Container Service)<\/strong> is a fully managed container orchestration service provided by AWS. It allows you to <strong data-start=\"128\" data-end=\"180\">deploy, run, scale, and manage Docker containers<\/strong> without having to manage the orchestration software yourself.<\/p>\n<p data-start=\"244\" data-end=\"335\">Think of ECS as a service that runs your containerized applications reliably in production.<\/p>\n<hr data-start=\"337\" data-end=\"340\" \/>\n<h4 data-section-id=\"n3y5cj\" data-start=\"342\" data-end=\"363\">Why do we use ECS?<\/h4>\n<p data-start=\"365\" data-end=\"399\">Suppose you have a Go application.<\/p>\n<p data-start=\"401\" data-end=\"413\">Without ECS:<\/p>\n<ul data-start=\"415\" data-end=\"536\">\n<li data-section-id=\"2spp01\" data-start=\"415\" data-end=\"436\">Create EC2 instance<\/li>\n<li data-section-id=\"d5jdzx\" data-start=\"437\" data-end=\"453\">Install Docker<\/li>\n<li data-section-id=\"9ryyl\" data-start=\"454\" data-end=\"485\">Run Docker container manually<\/li>\n<li data-section-id=\"1lcza53\" data-start=\"486\" data-end=\"519\">Restart container if it crashes<\/li>\n<li data-section-id=\"19dvvpb\" data-start=\"520\" data-end=\"536\">Scale manually<\/li>\n<\/ul>\n<p data-start=\"538\" data-end=\"547\">With ECS:<\/p>\n<ul data-start=\"549\" data-end=\"694\">\n<li data-section-id=\"a1pd2f\" data-start=\"549\" data-end=\"575\">Deploy the Docker image.<\/li>\n<li data-section-id=\"1jvm10e\" data-start=\"576\" data-end=\"603\">ECS starts the container.<\/li>\n<li data-section-id=\"16hx500\" data-start=\"604\" data-end=\"632\">Restarts it if it crashes.<\/li>\n<li data-section-id=\"1gpetbp\" data-start=\"633\" data-end=\"659\">Scales it automatically.<\/li>\n<li data-section-id=\"1hqw4t3\" data-start=\"660\" data-end=\"694\">Integrates with a Load Balancer.<\/li>\n<\/ul>\n<h2>145.what is GMP?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"126\"><strong data-start=\"0\" data-end=\"7\">GMP<\/strong> is the scheduling model used by the Go runtime to efficiently execute <strong data-start=\"78\" data-end=\"92\">goroutines<\/strong> on operating system (OS) threads.<\/p>\n<p data-start=\"128\" data-end=\"147\"><strong data-start=\"128\" data-end=\"147\">GMP stands for:<\/strong><\/p>\n<ul data-start=\"149\" data-end=\"238\">\n<li data-section-id=\"yd5ykk\" data-start=\"149\" data-end=\"168\"><strong data-start=\"151\" data-end=\"156\">G<\/strong> = Goroutine<\/li>\n<li data-section-id=\"4outji\" data-start=\"169\" data-end=\"198\"><strong data-start=\"171\" data-end=\"176\">M<\/strong> = Machine (OS Thread)<\/li>\n<li data-section-id=\"p0rk07\" data-start=\"199\" data-end=\"238\"><strong data-start=\"201\" data-end=\"206\">P<\/strong> = Processor (Logical Processor)<\/li>\n<\/ul>\n<p data-start=\"240\" data-end=\"345\">The Go scheduler uses these three components to run thousands or even millions of goroutines efficiently.<\/p>\n<hr data-start=\"347\" data-end=\"350\" \/>\n<h4 data-section-id=\"1xrazmc\" data-start=\"352\" data-end=\"372\">Why is GMP needed?<\/h4>\n<p data-start=\"374\" data-end=\"433\">The operating system schedules <strong data-start=\"405\" data-end=\"416\">threads<\/strong>, not goroutines.<\/p>\n<p data-start=\"435\" data-end=\"526\">Go introduces its own scheduler to map many goroutines onto a smaller number of OS threads.<\/p>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"15ijzhp\" data-start=\"3147\" data-end=\"3166\">Advantages of GMP<\/h3>\n<ul data-start=\"3168\" data-end=\"3417\">\n<li data-section-id=\"xeqbph\" data-start=\"3168\" data-end=\"3217\">Efficient scheduling of millions of goroutines.<\/li>\n<li data-section-id=\"s9m3ik\" data-start=\"3218\" data-end=\"3263\">Low memory overhead compared to OS threads.<\/li>\n<li data-section-id=\"1oeoj0c\" data-start=\"3264\" data-end=\"3309\">Good CPU utilization through work stealing.<\/li>\n<li data-section-id=\"18iwu5a\" data-start=\"3310\" data-end=\"3372\">Handles blocking operations without stalling all goroutines.<\/li>\n<li data-section-id=\"1744xkf\" data-start=\"3373\" data-end=\"3417\">Supports scalable concurrent applications.<\/li>\n<\/ul>\n<h2>146.GMP Is increase thread or decrease ?<\/h2>\n<p><strong data-start=\"0\" data-end=\"12\">Neither.<\/strong> GMP itself does <strong data-start=\"29\" data-end=\"72\">not simply increase or decrease threads<\/strong>. Instead, it <strong data-start=\"86\" data-end=\"182\">efficiently manages a small number of OS threads (M) to run a large number of goroutines (G)<\/strong>.<\/p>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1rqlfrb\" data-start=\"814\" data-end=\"844\">Does GMP increase threads?<\/h3>\n<p data-start=\"846\" data-end=\"879\"><strong data-start=\"846\" data-end=\"879\">Yes, but only when necessary.<\/strong><\/p>\n<p data-start=\"881\" data-end=\"893\">For example:<\/p>\n<ul data-start=\"895\" data-end=\"1142\">\n<li data-section-id=\"17n80et\" data-start=\"895\" data-end=\"1065\">If an OS thread blocks on a system call (e.g., file I\/O or a network operation), the Go runtime may create another thread so that other goroutines can continue running.<\/li>\n<li data-section-id=\"1lh43go\" data-start=\"1066\" data-end=\"1142\">When the blocked thread becomes available again, the runtime can reuse it.<\/li>\n<\/ul>\n<h3 data-section-id=\"xu08o1\" data-start=\"1144\" data-end=\"1174\">Does GMP decrease threads?<\/h3>\n<p data-start=\"1176\" data-end=\"1303\"><strong data-start=\"1176\" data-end=\"1184\">Yes.<\/strong> The runtime can reuse existing threads and avoid creating unnecessary ones. Idle threads may eventually be cleaned up.<\/p>\n<h3 data-section-id=\"7pftqr\" data-start=\"1305\" data-end=\"1325\">Interview Answer<\/h3>\n<blockquote data-start=\"1327\" data-end=\"1725\">\n<p data-start=\"1329\" data-end=\"1725\">&#8220;GMP doesn&#8217;t simply increase or decrease threads. Its purpose is to efficiently schedule many goroutines onto a smaller number of OS threads. Go creates additional OS threads only when needed, such as when a thread blocks on a system call, and it reuses threads whenever possible. This allows Go applications to handle thousands of concurrent goroutines without creating thousands of OS threads.&#8221;<\/p>\n<\/blockquote>\n<p data-start=\"1727\" data-end=\"1753\"><strong data-start=\"1727\" data-end=\"1753\">Key point to remember:<\/strong><\/p>\n<ul data-start=\"1754\" data-end=\"1958\" data-is-last-node=\"\" data-is-only-node=\"\">\n<li data-section-id=\"1rmjeh8\" data-start=\"1754\" data-end=\"1796\">\u2705 <strong data-start=\"1758\" data-end=\"1796\">Goroutines can be in the millions.<\/strong><\/li>\n<li data-section-id=\"3pd37p\" data-start=\"1797\" data-end=\"1874\">\u2705 <strong data-start=\"1801\" data-end=\"1874\">OS threads are kept relatively few and are managed by the Go runtime.<\/strong><\/li>\n<li data-section-id=\"234q12\" data-start=\"1875\" data-end=\"1958\" data-is-last-node=\"\">\u2705 <strong data-start=\"1879\" data-end=\"1958\" data-is-last-node=\"\">GMP&#8217;s goal is to minimize thread creation while maximizing CPU utilization.<\/strong><\/li>\n<\/ul>\n<h2>147.what is dependency injection in Golang?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"141\"><strong data-start=\"0\" data-end=\"29\">Dependency Injection (DI)<\/strong> is a design pattern where an object <strong data-start=\"66\" data-end=\"140\">receives its dependencies from outside instead of creating them itself<\/strong>.<\/p>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"141\"><strong data-start=\"163\" data-end=\"247\">Don&#8217;t create dependencies inside a struct or function; inject them from outside.<\/strong><\/p>\n<p data-start=\"249\" data-end=\"330\">This makes your code <strong data-start=\"270\" data-end=\"289\">loosely coupled<\/strong>, easier to test, and easier to maintain.<\/p>\n<h2 data-start=\"249\" data-end=\"330\">148.what is url shortner ?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"83\">A <strong data-start=\"2\" data-end=\"19\">URL Shortener<\/strong> is a service that converts a long URL into a short, unique URL.<\/p>\n<h3 data-section-id=\"16zgw4u\" data-start=\"85\" data-end=\"96\">Example<\/h3>\n<p data-start=\"98\" data-end=\"111\">Original URL:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>https:\/\/www.example.com\/products\/electronics\/mobile\/apple\/iphone-16-pro-max?color=black&amp;storage=256GB<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div><\/div>\n<div class=\"\">Short URL:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>https:\/\/short.ly\/aB12Cd<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">When a user visits the short URL, they are <strong data-start=\"320\" data-end=\"334\">redirected<\/strong> to the original long URL.<\/div>\n<div>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"1xjlsu1\" data-start=\"367\" data-end=\"381\">How it Works<\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>User<\/code><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>  |\r\n  | 1. Submit Long URL\r\n  v\r\n+----------------+\r\n| URL Shortener  |\r\n+----------------+\r\n        |\r\n        | Generate Unique ID\r\n        v\r\n+----------------+\r\n| Database       |\r\n| aB12Cd -&gt; Long URL |\r\n+----------------+\r\n        |\r\n        | Return Short URL\r\n        v\r\nhttps:\/\/short.ly\/aB12Cd<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">When someone opens the short URL:<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>User<\/code><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>  |\r\n  | GET \/aB12Cd\r\n  v\r\nURL Shortener\r\n  |\r\n  | Lookup in Database\r\n  v\r\nLong URL\r\n  |\r\n  | HTTP 301\/302 Redirect\r\n  v\r\nOriginal Website\r\n\r\n<\/code><\/pre>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"z4uups\" data-start=\"1429\" data-end=\"1461\">How to Generate the Short Code<\/h3>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"z4uups\" data-start=\"1429\" data-end=\"1461\">Common approaches:<\/h3>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"z4uups\" data-start=\"1429\" data-end=\"1461\">1. Base62 Encoding (Most Common)<\/h3>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"z4uups\" data-start=\"1429\" data-end=\"1461\">Characters:<\/h3>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"z4uups\" data-start=\"1429\" data-end=\"1461\"><code style=\"font-size: 16px;\">0-9<\/code><\/h3>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<h4 class=\"cm-content q9tKkq_readonly m-0\"><code>A-Z<br \/>\na-z<\/p>\n<p>2.Random String<br \/>\n3.<\/code>Hashing<br \/>\nUse algorithms like MD5 or SHA-256 and take a portion of the hash.<\/h4>\n<h2 class=\"cm-content q9tKkq_readonly m-0\">149.whay microservices used in Golang main purpose ?<\/h2>\n<p class=\"PDq2pG_selectionAnchorContainer\" data-start=\"0\" data-end=\"177\">The main purpose of <strong data-start=\"20\" data-end=\"37\">microservices<\/strong> is to <strong data-start=\"44\" data-end=\"108\">break a large application into smaller, independent services<\/strong>, where each service is responsible for a single business capability.<\/p>\n<p class=\"\" data-start=\"179\" data-end=\"304\">Golang is widely used for microservices because it is <strong data-start=\"233\" data-end=\"303\">fast, lightweight, supports concurrency well, and uses less memory<\/strong>.<\/p>\n<h3 class=\"PDq2pG_selectionAnchorContainer\" data-section-id=\"fcilqd\" data-start=\"1204\" data-end=\"1222\">2. Scalability<\/h3>\n<p data-start=\"1224\" data-end=\"1232\">Suppose:<\/p>\n<ul data-start=\"1234\" data-end=\"1383\">\n<li data-section-id=\"laczf2\" data-start=\"1234\" data-end=\"1281\">User Service receives <strong data-start=\"1258\" data-end=\"1281\">100 requests\/minute<\/strong><\/li>\n<li data-section-id=\"1nuxl00\" data-start=\"1282\" data-end=\"1332\">Product Service receives <strong data-start=\"1309\" data-end=\"1332\">500 requests\/minute<\/strong><\/li>\n<li data-section-id=\"szfuuh\" data-start=\"1333\" data-end=\"1383\">Order Service receives <strong data-start=\"1358\" data-end=\"1383\">5,000 requests\/minute<\/strong><\/li>\n<\/ul>\n<p data-start=\"1385\" data-end=\"1442\">With microservices, you can scale only the Order Service.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>User Service      x1\r\nProduct Service   x2\r\nOrder Service     x10<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"1521\" data-end=\"1553\">This saves infrastructure costs.<\/p>\n<hr data-start=\"1555\" data-end=\"1558\" \/>\n<h3 data-section-id=\"ghju39\" data-start=\"1560\" data-end=\"1582\">3. Fault Isolation<\/h3>\n<p data-start=\"1584\" data-end=\"1620\">If the Notification Service crashes:<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>Notification \u274c<\/code><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\">The User, Product, and Order services can continue working.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p data-start=\"1711\" data-end=\"1787\">In a monolithic application, one failure might affect the whole application.<\/p>\n<hr data-start=\"1789\" data-end=\"1792\" \/>\n<h3 data-section-id=\"kjc087\" data-start=\"1794\" data-end=\"1823\">4. Technology Flexibility<\/h3>\n<p data-start=\"1825\" data-end=\"1885\">Different services can use different technologies if needed.<\/p>\n<p data-start=\"1887\" data-end=\"1895\">Example:<\/p>\n<ul data-start=\"1897\" data-end=\"1973\">\n<li data-section-id=\"1hmml8a\" data-start=\"1897\" data-end=\"1920\">User Service \u2192 Golang<\/li>\n<li data-section-id=\"712q4a\" data-start=\"1921\" data-end=\"1954\">Recommendation Service \u2192 Python<\/li>\n<li data-section-id=\"krrvjk\" data-start=\"1955\" data-end=\"1973\">Analytics \u2192 Java<\/li>\n<\/ul>\n<hr data-start=\"1975\" data-end=\"1978\" \/>\n<h3 data-section-id=\"1md54s9\" data-start=\"1980\" data-end=\"2005\">5. Faster Development<\/h3>\n<p data-start=\"2007\" data-end=\"2069\">Different teams can work on different services simultaneously.<\/p>\n<div class=\"relative w-full mt-4 mb-1\">\n<div class=\"\">\n<div class=\"contents\">\n<div class=\"relative\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"h-full min-h-0 min-w-0\">\n<div class=\"border border-token-border-light border-radius-3xl corner-superellipse\/1.1 rounded-3xl\">\n<div class=\"h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse\/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback\">\n<div class=\"pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1\"><code>Team A \u2192 User Service<\/code><\/div>\n<div class=\"relative\">\n<div class=\"pe-11 pt-3\">\n<div class=\"relative z-0 flex max-w-full\">\n<div id=\"code-block-viewer\" class=\"q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch \u037cd \u037cr\" dir=\"ltr\">\n<div class=\"cm-scroller\">\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code>Team B \u2192 Order Service\r\nTeam C \u2192 Payment Service<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"\">\n<div class=\"\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"2154\" data-end=\"2157\" \/>\n<h2 data-section-id=\"13rnu7z\" data-start=\"2159\" data-end=\"2191\">Why Golang for Microservices?<\/h2>\n<p data-start=\"2193\" data-end=\"2232\">Golang is a good fit because it offers:<\/p>\n<ul data-start=\"2234\" data-end=\"2445\">\n<li data-section-id=\"190ltve\" data-start=\"2234\" data-end=\"2250\">Fast execution<\/li>\n<li data-section-id=\"mvpcad\" data-start=\"2251\" data-end=\"2313\">Lightweight goroutines for handling many concurrent requests<\/li>\n<li data-section-id=\"6qbge0\" data-start=\"2314\" data-end=\"2332\">Low memory usage<\/li>\n<li data-section-id=\"1m2xuyg\" data-start=\"2333\" data-end=\"2352\">Fast startup time<\/li>\n<li data-section-id=\"10udld7\" data-start=\"2353\" data-end=\"2388\">Simple deployment (single binary)<\/li>\n<li data-section-id=\"16p5gk1\" data-start=\"2389\" data-end=\"2445\">Strong standard library for HTTP, JSON, and networking<\/li>\n<\/ul>\n<hr data-start=\"2447\" data-end=\"2450\" \/>\n<h2 data-section-id=\"53wsor\" data-start=\"2452\" data-end=\"2490\">Communication Between Microservices<\/h2>\n<p data-start=\"2492\" data-end=\"2519\">Services communicate using:<\/p>\n<ul data-start=\"2521\" data-end=\"2586\">\n<li data-section-id=\"3mxeoj\" data-start=\"2521\" data-end=\"2532\">REST APIs<\/li>\n<li data-section-id=\"1j3icku\" data-start=\"2533\" data-end=\"2539\">gRPC<\/li>\n<li data-section-id=\"hzojir\" data-start=\"2540\" data-end=\"2586\">Message queues (Kafka, RabbitMQ, Amazon SQS)<\/li>\n<\/ul>\n<h2>150. If i stored password in properties file and i want to print that how i print that ?<\/h2>\n<p><span class=\"\u037cg\">func<\/span> <span class=\"\u037cm\">main<\/span>() {<\/p>\n<p><span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">godotenv<\/span><span class=\"\u037cg\">.<\/span>Load(<span class=\"\u037ck\">&#8220;.env&#8221;<\/span>)<\/p>\n<p><span class=\"\u037cg\">if<\/span> <span class=\"\u037cm\">err<\/span> <span class=\"\u037cg\">!=<\/span> <span class=\"\u037cj\">nil<\/span> {<\/p>\n<p><span class=\"\u037cm\">log<\/span><span class=\"\u037cg\">.<\/span>Fatal(<span class=\"\u037ck\">&#8220;Error loading .env file&#8221;<\/span>)<\/p>\n<p>}<\/p>\n<p><span class=\"\u037cm\">password<\/span> <span class=\"\u037cg\">:=<\/span> <span class=\"\u037cm\">os<\/span><span class=\"\u037cg\">.<\/span>Getenv(<span class=\"\u037ck\">&#8220;DB_PASSWORD&#8221;<\/span>) <span class=\"\u037ce\">\/\/ Only for debugging &#8211; not recommended in production<\/span><\/p>\n<p><span class=\"\u037cm\">fmt<\/span><span class=\"\u037cg\">.<\/span>Println(<span class=\"\u037ck\">&#8220;Password:&#8221;<\/span>, <span class=\"\u037cm\">password<\/span>)<\/p>\n<p>}<\/p>\n<p>Technically, I can read it from the configuration and print it. However, in production applications I avoid logging or printing passwords because they are sensitive credentials. Instead, I verify that the value is loaded by masking it or by checking its presence, and I prefer storing secrets in a secure secret manager or environment variables rather than directly in a properties file.<\/p>\n<h2>151.which channel is synchronous in Golang?<\/h2>\n<h2><strong style=\"font-size: 16px;\" data-start=\"1482\" data-end=\"1587\">Unbuffered channels are synchronous because the sender and receiver must rendezvous at the same time.<\/strong><span style=\"font-size: 16px;\"> A send operation blocks until another goroutine receives the value, and a receive blocks until another goroutine sends one.<\/span><\/h2>\n<h4>Follow-up: Which channel is asynchronous?<\/h4>\n<h4><strong>Buffered channels are asynchronous because they allow sends to proceed without an immediate receiver, as long as there is space available in the buffer. Once the buffer is full, send operations block until a receiver consumes a value.<\/strong><\/h4>\n<h2 class=\"cm-content q9tKkq_readonly m-0\"><code>152.what is synchronous?<\/code><\/h2>\n<p><strong data-start=\"0\" data-end=\"15\">Synchronous<\/strong> means <strong data-start=\"22\" data-end=\"97\">one operation waits for another operation to complete before continuing<\/strong>.<\/p>\n<p>Synchronous means one operation waits for another operation to complete before proceeding. In Go, an unbuffered channel is synchronous because the sender blocks until a receiver is ready, and the receiver blocks until a sender provides a value. This synchronization ensures both goroutines meet at the communication point.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"cm-content q9tKkq_readonly m-0\"><code><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. What is Go and why is it popular? Go is an open-source programming language developed by Google. It is designed for simplicity, concurrency, fast compilation, and scalability. Key advantages: Simple syntax Fast execution Built-in concurrency (goroutines) Garbage collection Strong standard library Great for microservices and cloud systems 2. What are goroutines? Goroutines are lightweight&#8230;<\/p>\n","protected":false},"author":1,"featured_media":27,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[11,12],"class_list":["post-13","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview","tag-go-lang","tag-interview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Lang Interview Questions and Answers 2026 - Smita Bharat Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Lang Interview Questions and Answers 2026 - Smita Bharat Blog\" \/>\n<meta property=\"og:description\" content=\"1. What is Go and why is it popular? Go is an open-source programming language developed by Google. It is designed for simplicity, concurrency, fast compilation, and scalability. Key advantages: Simple syntax Fast execution Built-in concurrency (goroutines) Garbage collection Strong standard library Great for microservices and cloud systems 2. What are goroutines? Goroutines are lightweight...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-17T11:43:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-14T17:28:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png\" \/>\n\t<meta property=\"og:image:width\" content=\"886\" \/>\n\t<meta property=\"og:image:height\" content=\"388\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"64 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/8d82457aaa3ced63e3f875cb5a31ce4c\"},\"headline\":\"Go Lang Interview Questions and Answers 2026\",\"datePublished\":\"2026-05-17T11:43:34+00:00\",\"dateModified\":\"2026-07-14T17:28:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/\"},\"wordCount\":14521,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Go-Logo_LightBlue.png\",\"keywords\":[\"Go Lang\",\"Interview\"],\"articleSection\":[\"Interview\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/\",\"name\":\"Go Lang Interview Questions and Answers 2026 - Smita Bharat Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Go-Logo_LightBlue.png\",\"datePublished\":\"2026-05-17T11:43:34+00:00\",\"dateModified\":\"2026-07-14T17:28:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/8d82457aaa3ced63e3f875cb5a31ce4c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Go-Logo_LightBlue.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/Go-Logo_LightBlue.png\",\"width\":886,\"height\":388},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-lang-interview-questions-and-answers-2026\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Lang Interview Questions and Answers 2026\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/8d82457aaa3ced63e3f875cb5a31ce4c\",\"name\":\"Smita\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c2cc9a654a17e3944ea74c7317b81fa65c28ba7d50a629a551b0c9b321308883?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c2cc9a654a17e3944ea74c7317b81fa65c28ba7d50a629a551b0c9b321308883?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c2cc9a654a17e3944ea74c7317b81fa65c28ba7d50a629a551b0c9b321308883?s=96&d=mm&r=g\",\"caption\":\"Smita\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"],\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/author\\\/smita_bharat_wp\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Lang Interview Questions and Answers 2026 - Smita Bharat Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/","og_locale":"en_US","og_type":"article","og_title":"Go Lang Interview Questions and Answers 2026 - Smita Bharat Blog","og_description":"1. What is Go and why is it popular? Go is an open-source programming language developed by Google. It is designed for simplicity, concurrency, fast compilation, and scalability. Key advantages: Simple syntax Fast execution Built-in concurrency (goroutines) Garbage collection Strong standard library Great for microservices and cloud systems 2. What are goroutines? Goroutines are lightweight...","og_url":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/","og_site_name":"Smita Bharat Blog","article_published_time":"2026-05-17T11:43:34+00:00","article_modified_time":"2026-07-14T17:28:47+00:00","og_image":[{"width":886,"height":388,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"64 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/8d82457aaa3ced63e3f875cb5a31ce4c"},"headline":"Go Lang Interview Questions and Answers 2026","datePublished":"2026-05-17T11:43:34+00:00","dateModified":"2026-07-14T17:28:47+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/"},"wordCount":14521,"commentCount":0,"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png","keywords":["Go Lang","Interview"],"articleSection":["Interview"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/","url":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/","name":"Go Lang Interview Questions and Answers 2026 - Smita Bharat Blog","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png","datePublished":"2026-05-17T11:43:34+00:00","dateModified":"2026-07-14T17:28:47+00:00","author":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/8d82457aaa3ced63e3f875cb5a31ce4c"},"breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png","width":886,"height":388},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers-2026\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Lang Interview Questions and Answers 2026"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/8d82457aaa3ced63e3f875cb5a31ce4c","name":"Smita","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c2cc9a654a17e3944ea74c7317b81fa65c28ba7d50a629a551b0c9b321308883?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c2cc9a654a17e3944ea74c7317b81fa65c28ba7d50a629a551b0c9b321308883?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c2cc9a654a17e3944ea74c7317b81fa65c28ba7d50a629a551b0c9b321308883?s=96&d=mm&r=g","caption":"Smita"},"sameAs":["https:\/\/smitabharat.com\/blog"],"url":"https:\/\/smitabharat.com\/blog\/author\/smita_bharat_wp\/"}]}},"jetpack_featured_media_url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/05\/Go-Logo_LightBlue.png","_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":16,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/61"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/27"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}