<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Blocking Queue]]></title><description><![CDATA[Blocking Queue]]></description><link>https://blog.blockingqueue.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 10:23:43 GMT</lastBuildDate><atom:link href="https://blog.blockingqueue.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Language Switcher Cheatsheet: Java, JavaScript, Python, Go]]></title><description><![CDATA[Introduction
I've been a backend developer since 2008, spending most of my career writing Java. Java is how I think - when I approach a problem, my brain defaults to classes, interfaces, and explicit types. Over the years, I've jumped between platfor...]]></description><link>https://blog.blockingqueue.com/language-switcher-cheatsheet-java-javascript-python-go</link><guid isPermaLink="true">https://blog.blockingqueue.com/language-switcher-cheatsheet-java-javascript-python-go</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Java]]></category><category><![CDATA[Python]]></category><category><![CDATA[Go]]></category><category><![CDATA[golang]]></category><category><![CDATA[backend]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Thu, 18 Dec 2025 09:38:38 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1766053545591/d4612c93-29ea-4177-bc61-e79fae378b0e.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>I've been a backend developer since 2008, spending most of my career writing Java. Java is how I think - when I approach a problem, my brain defaults to classes, interfaces, and explicit types. Over the years, I've jumped between platforms and languages - Python, JavaScript, and now Go - each time experiencing the disorienting feeling of context switching between different philosophies and paradigms.</p>
<p>After spending time in Python, I found myself loving some aspects (the simplicity, the readability) while hating others (the lack of compile-time safety, the "we're all consenting adults" approach that sometimes feels too permissive). JavaScript is elegant in its own way, but it often feels like a toy to me - too loose, too flexible, too willing to let you shoot yourself in the foot.</p>
<p>Now, as I'm learning Go, I'm surprised to find it feels <em>more verbose</em> than Java in some ways, despite Go's reputation for simplicity. Things that feel natural in Java require a different mental model in Go.</p>
<p><strong>Everything here is opinion-based.</strong> These are my observations from years of switching between languages, and your experience might differ. In the end, I strongly believe <strong>the best language is the one you know best</strong> - but understanding <em>why</em> languages differ helps you switch contexts faster.</p>
<p>This cheatsheet is my attempt to document the "why" behind the differences - not just <em>what</em> the syntax is, but <em>why</em> each language chose to do things differently. If you, like me, think in one language and need to work in another, this guide is for you.</p>
<hr />
<h2 id="heading-variables-amp-initialization">Variables &amp; Initialization</h2>
<h3 id="heading-how-to-declare-variables">How to declare variables</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Basic variable</strong></td><td><code>String name = "John";</code></td><td><code>let name = "John";</code></td><td><code>name = "John"</code></td><td><code>name := "John"</code></td></tr>
<tr>
<td><strong>With explicit type</strong></td><td><code>String name = "John";</code></td><td>N/A (no types)</td><td><code>name: str = "John"</code> (optional)</td><td><code>var name string = "John"</code></td></tr>
<tr>
<td><strong>Constant (can't change)</strong></td><td><code>final String name = "John";</code></td><td><code>const name = "John";</code></td><td><code>NAME = "John"</code> (by convention)</td><td><code>const name = "John"</code></td></tr>
<tr>
<td><strong>Global constant</strong></td><td><code>static final String NAME = "John";</code></td><td><code>const NAME = "John";</code></td><td><code>NAME = "John"</code></td><td><code>const NAME = "John"</code></td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different">Why they're different</h3>
<p><strong>Java - <code>final</code> vs constants</strong></p>
<ul>
<li><code>final</code> means you can only assign once</li>
<li>Inside a method: <code>final String name = "John";</code> - you can't reassign <code>name</code></li>
<li>For a true constant (like <code>MAX_SIZE</code>): use <code>static final</code> in a class</li>
<li>Java doesn't have a simple "const" keyword</li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Config</span> </span>{
    <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MAX_SIZE = <span class="hljs-number">100</span>;  <span class="hljs-comment">// Global constant</span>

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">example</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> count = <span class="hljs-number">5</span>;  <span class="hljs-comment">// Local constant</span>
        <span class="hljs-comment">// count = 10;  // Error! Can't reassign</span>
    }
}
</code></pre>
<p><strong>JavaScript - <code>const</code>, <code>let</code>, and <code>var</code></strong></p>
<ul>
<li><code>const</code> - can't reassign (use this by default)</li>
<li><code>let</code> - can reassign (use when you need to change the value)</li>
<li><code>var</code> - old way, don't use (has confusing scoping rules)</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> name = <span class="hljs-string">"John"</span>;  <span class="hljs-comment">// Can't change</span>
<span class="hljs-comment">// name = "Jane";  // Error!</span>

<span class="hljs-keyword">let</span> age = <span class="hljs-number">30</span>;  <span class="hljs-comment">// Can change</span>
age = <span class="hljs-number">31</span>;  <span class="hljs-comment">// OK</span>

<span class="hljs-keyword">const</span> person = { <span class="hljs-attr">name</span>: <span class="hljs-string">"John"</span> };
person.name = <span class="hljs-string">"Jane"</span>;  <span class="hljs-comment">// OK! const prevents reassigning 'person', not changing its contents</span>
</code></pre>
<p><strong>Python - No constants, just convention</strong></p>
<ul>
<li>Python has no way to prevent reassignment</li>
<li>Use UPPERCASE names to show "this shouldn't change"</li>
<li>Type hints are completely optional (just for documentation/tools)</li>
<li><strong>Python philosophy</strong>: "We're all consenting adults here"<ul>
<li>Python trusts you to follow conventions instead of enforcing them</li>
<li>Gives you freedom and responsibility</li>
<li>If you see <code>MAX_SIZE</code>, you know not to change it - no language enforcement needed</li>
</ul>
</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># Convention: UPPERCASE means constant</span>
MAX_SIZE = <span class="hljs-number">100</span>

<span class="hljs-comment"># Type hints (optional, not enforced)</span>
name: str = <span class="hljs-string">"John"</span>
age: int = <span class="hljs-number">30</span>

<span class="hljs-comment"># Python doesn't stop you from doing this:</span>
MAX_SIZE = <span class="hljs-number">200</span>  <span class="hljs-comment"># No error, but you shouldn't do this!</span>
</code></pre>
<p><strong>Go - <code>const</code> vs <code>:=</code></strong></p>
<ul>
<li><code>:=</code> is shorthand for declaring and initializing (only inside functions)</li>
<li><code>const</code> creates a true constant</li>
<li>Go is strict: you must use every variable you declare</li>
</ul>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">example</span><span class="hljs-params">()</span></span> {
    name := <span class="hljs-string">"John"</span>  <span class="hljs-comment">// Shorthand: declare + assign</span>
    <span class="hljs-keyword">var</span> age <span class="hljs-keyword">int</span> = <span class="hljs-number">30</span>  <span class="hljs-comment">// Long form</span>
    <span class="hljs-keyword">const</span> MAX_SIZE = <span class="hljs-number">100</span>  <span class="hljs-comment">// Constant</span>

    <span class="hljs-comment">// name := "Jane"  // Error! := only for new variables</span>
    name = <span class="hljs-string">"Jane"</span>  <span class="hljs-comment">// OK, reassigning existing variable</span>
}

<span class="hljs-keyword">const</span> PI = <span class="hljs-number">3.14159</span>  <span class="hljs-comment">// Package-level constant</span>
</code></pre>
<hr />
<h2 id="heading-strings">Strings</h2>
<h3 id="heading-how-strings-work">How strings work</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Create string</strong></td><td><code>String name = "Hello";</code></td><td><code>let name = "Hello";</code></td><td><code>name = "Hello"</code></td><td><code>name := "Hello"</code></td></tr>
<tr>
<td><strong>Concatenation</strong></td><td><code>"Hello" + " " + "World"</code></td><td><code>`Hello ${name}` </code> or <code>"Hello" + name</code></td><td><code>f"Hello {name}"</code> or <code>"Hello" + name</code></td><td><code>"Hello " + name</code></td></tr>
<tr>
<td><strong>Multiline</strong></td><td>Text blocks <code>"""..."""</code> (Java 15+)</td><td>Template literals <code>`...` </code></td><td>Triple quotes <code>"""..."""</code></td><td>Backticks <code>`...` </code></td></tr>
<tr>
<td><strong>Immutable?</strong></td><td>Yes</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
<tr>
<td><strong>Character at index</strong></td><td><code>s.charAt(0)</code></td><td><code>s[0]</code> or <code>s.charAt(0)</code></td><td><code>s[0]</code></td><td><code>s[0]</code> (returns byte!)</td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-1">Why they're different</h3>
<p><strong>All four languages have immutable strings</strong> - once created, you can't change them. Any "modification" creates a new string.</p>
<p><strong>Java - String vs StringBuilder</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// Strings are immutable</span>
String name = <span class="hljs-string">"Alice"</span>;
name = name + <span class="hljs-string">" Smith"</span>;  <span class="hljs-comment">// Creates a NEW string</span>

<span class="hljs-comment">// String concatenation in loops is slow (creates many objects)</span>
String result = <span class="hljs-string">""</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000</span>; i++) {
    result = result + i;  <span class="hljs-comment">// BAD - creates 1000 string objects!</span>
}

<span class="hljs-comment">// Use StringBuilder for performance</span>
StringBuilder sb = <span class="hljs-keyword">new</span> StringBuilder();
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000</span>; i++) {
    sb.append(i);
}
String result = sb.toString();

<span class="hljs-comment">// String formatting</span>
String message = String.format(<span class="hljs-string">"Hello %s, age %d"</span>, name, <span class="hljs-number">30</span>);

<span class="hljs-comment">// Multiline strings (Java 15+)</span>
String text = <span class="hljs-string">""</span><span class="hljs-string">"
    Line 1
    Line 2
    Line 3
    "</span><span class="hljs-string">""</span>;
</code></pre>
<p><strong>JavaScript - Template literals</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Strings are immutable</span>
<span class="hljs-keyword">let</span> name = <span class="hljs-string">"Alice"</span>;
name = name + <span class="hljs-string">" Smith"</span>;  <span class="hljs-comment">// Creates a new string</span>

<span class="hljs-comment">// Template literals (modern way)</span>
<span class="hljs-keyword">let</span> message = <span class="hljs-string">`Hello <span class="hljs-subst">${name}</span>, age <span class="hljs-subst">${<span class="hljs-number">30</span>}</span>`</span>;

<span class="hljs-comment">// Multiline with template literals</span>
<span class="hljs-keyword">let</span> text = <span class="hljs-string">`
    Line 1
    Line 2
    Line 3
`</span>;

<span class="hljs-comment">// Old-style concatenation</span>
<span class="hljs-keyword">let</span> message2 = <span class="hljs-string">"Hello "</span> + name + <span class="hljs-string">", age "</span> + <span class="hljs-number">30</span>;
</code></pre>
<p><strong>Python - f-strings</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># Strings are immutable</span>
name = <span class="hljs-string">"Alice"</span>
name = name + <span class="hljs-string">" Smith"</span>  <span class="hljs-comment"># Creates a new string</span>

<span class="hljs-comment"># f-strings (Python 3.6+, modern way)</span>
message = <span class="hljs-string">f"Hello <span class="hljs-subst">{name}</span>, age <span class="hljs-subst">{<span class="hljs-number">30</span>}</span>"</span>

<span class="hljs-comment"># Older formatting</span>
message = <span class="hljs-string">"Hello {}, age {}"</span>.format(name, <span class="hljs-number">30</span>)
message = <span class="hljs-string">"Hello %s, age %d"</span> % (name, <span class="hljs-number">30</span>)

<span class="hljs-comment"># Multiline strings</span>
text = <span class="hljs-string">"""
Line 1
Line 2
Line 3
"""</span>

<span class="hljs-comment"># String concatenation in loops is OK (Python optimizes this)</span>
result = <span class="hljs-string">""</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">1000</span>):
    result += str(i)  <span class="hljs-comment"># Python handles this efficiently</span>
</code></pre>
<p><strong>Go - Strings are byte slices (UTF-8)</strong></p>
<p>Go strings are different - they're <strong>immutable byte slices</strong> encoded in UTF-8:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Strings are immutable</span>
name := <span class="hljs-string">"Alice"</span>
name = name + <span class="hljs-string">" Smith"</span>  <span class="hljs-comment">// Creates a new string</span>

<span class="hljs-comment">// String concatenation</span>
message := <span class="hljs-string">"Hello "</span> + name

<span class="hljs-comment">// fmt.Sprintf for formatting</span>
message := fmt.Sprintf(<span class="hljs-string">"Hello %s, age %d"</span>, name, <span class="hljs-number">30</span>)

<span class="hljs-comment">// Multiline strings (raw string literals with backticks)</span>
text := <span class="hljs-string">`
Line 1
Line 2
Line 3
`</span>

<span class="hljs-comment">// IMPORTANT: Indexing gives you BYTES, not characters!</span>
s := <span class="hljs-string">"Hello"</span>
b := s[<span class="hljs-number">0</span>]  <span class="hljs-comment">// b is a byte (uint8), value 72 ('H')</span>

<span class="hljs-comment">// For Unicode characters, use runes</span>
s := <span class="hljs-string">"Hello 世界"</span>
runes := []<span class="hljs-keyword">rune</span>(s)  <span class="hljs-comment">// Convert to slice of runes (Unicode code points)</span>
fmt.Println(runes[<span class="hljs-number">6</span>])  <span class="hljs-comment">// 世</span>

<span class="hljs-comment">// Iterating over string with range gives you runes</span>
<span class="hljs-keyword">for</span> i, r := <span class="hljs-keyword">range</span> <span class="hljs-string">"Hello 世界"</span> {
    fmt.Printf(<span class="hljs-string">"%d: %c\n"</span>, i, r)  <span class="hljs-comment">// Prints Unicode characters correctly</span>
}

<span class="hljs-comment">// String concatenation in loops - use strings.Builder</span>
<span class="hljs-keyword">var</span> sb strings.Builder
<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000</span>; i++ {
    sb.WriteString(fmt.Sprintf(<span class="hljs-string">"%d"</span>, i))
}
result := sb.String()
</code></pre>
<h3 id="heading-the-go-difference-bytes-vs-runes">The Go difference: Bytes vs Runes</h3>
<p><strong>Go is explicit about Unicode:</strong></p>
<ul>
<li><strong>string</strong>: Immutable sequence of bytes (UTF-8 encoded)</li>
<li><strong>byte</strong> (<code>uint8</code>): Single byte</li>
<li><strong>rune</strong> (<code>int32</code>): Unicode code point (a character)</li>
</ul>
<pre><code class="lang-go">s := <span class="hljs-string">"Hello 世界"</span>

<span class="hljs-comment">// Length in bytes</span>
fmt.Println(<span class="hljs-built_in">len</span>(s))  <span class="hljs-comment">// 12 (not 8!) - "世界" is 6 bytes in UTF-8</span>

<span class="hljs-comment">// Indexing returns a byte</span>
fmt.Println(s[<span class="hljs-number">0</span>])  <span class="hljs-comment">// 72 (byte value of 'H')</span>

<span class="hljs-comment">// To work with characters, convert to []rune</span>
runes := []<span class="hljs-keyword">rune</span>(s)
fmt.Println(<span class="hljs-built_in">len</span>(runes))  <span class="hljs-comment">// 8 (correct character count)</span>
fmt.Println(runes[<span class="hljs-number">6</span>])    <span class="hljs-comment">// 19990 (Unicode code point for '世')</span>

<span class="hljs-comment">// Range over string automatically decodes UTF-8 to runes</span>
<span class="hljs-keyword">for</span> i, r := <span class="hljs-keyword">range</span> s {
    fmt.Printf(<span class="hljs-string">"Position %d: %c\n"</span>, i, r)
}
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Position 0: H</span>
<span class="hljs-comment">// Position 1: e</span>
<span class="hljs-comment">// Position 2: l</span>
<span class="hljs-comment">// Position 3: l</span>
<span class="hljs-comment">// Position 4: o</span>
<span class="hljs-comment">// Position 5:</span>
<span class="hljs-comment">// Position 6: 世</span>
<span class="hljs-comment">// Position 9: 界</span>
</code></pre>
<p><strong>Why Go does this:</strong></p>
<ul>
<li><strong>Explicit</strong>: You can see when you're working with bytes vs characters</li>
<li><strong>Performance</strong>: Strings are just byte slices internally (efficient)</li>
<li><strong>Correctness</strong>: Forces you to think about Unicode properly</li>
<li><strong>Other languages</strong>: Hide these details (convenient but can cause bugs with non-ASCII text)</li>
</ul>
<p><strong>Common Go gotcha:</strong></p>
<pre><code class="lang-go">s := <span class="hljs-string">"café"</span>
fmt.Println(<span class="hljs-built_in">len</span>(s))     <span class="hljs-comment">// 5, not 4! (é is 2 bytes in UTF-8)</span>
fmt.Println(s[<span class="hljs-number">3</span>])       <span class="hljs-comment">// 195 (first byte of é), not 'é'!</span>
fmt.Println([]<span class="hljs-keyword">rune</span>(s))  <span class="hljs-comment">// [99 97 102 233] - correct 4 characters</span>
</code></pre>
<p><strong>In Java/JavaScript/Python</strong>, strings handle Unicode automatically (characters are the abstraction). <strong>In Go</strong>, you must explicitly choose bytes or runes.</p>
<hr />
<h2 id="heading-control-flow-if-for-while">Control Flow: if, for, while</h2>
<h3 id="heading-if-statements">If statements</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Basic if</strong></td><td><code>if (age &gt; 18) { }</code></td><td><code>if (age &gt; 18) { }</code></td><td><code>if age &gt; 18:</code></td><td><code>if age &gt; 18 { }</code></td></tr>
<tr>
<td><strong>If-else</strong></td><td><code>if (x) { } else { }</code></td><td><code>if (x) { } else { }</code></td><td><code>if x:\n    ...\nelse:\n    ...</code></td><td><code>if x { } else { }</code></td></tr>
<tr>
<td><strong>Ternary</strong></td><td><code>x &gt; 0 ? "pos" : "neg"</code></td><td><code>x &gt; 0 ? "pos" : "neg"</code></td><td><code>"pos" if x &gt; 0 else "neg"</code></td><td>No ternary operator</td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-2">Why they're different</h3>
<p><strong>Java - Parentheses required</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (age &gt; <span class="hljs-number">18</span>) {
    System.out.println(<span class="hljs-string">"Adult"</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (age &gt; <span class="hljs-number">12</span>) {
    System.out.println(<span class="hljs-string">"Teen"</span>);
} <span class="hljs-keyword">else</span> {
    System.out.println(<span class="hljs-string">"Child"</span>);
}

<span class="hljs-comment">// Ternary operator</span>
String status = age &gt; <span class="hljs-number">18</span> ? <span class="hljs-string">"Adult"</span> : <span class="hljs-string">"Minor"</span>;
</code></pre>
<p><strong>JavaScript - Same syntax as Java, but with "truthy/falsy" values</strong></p>
<p>JavaScript's <code>if</code> statements look like Java syntactically, but there's a key difference: JavaScript doesn't require a boolean expression. Instead, it converts any value to <code>true</code> or <code>false</code> using the concept of "truthy" and "falsy" values. This means you can write <code>if (name)</code> instead of <code>if (name !== null &amp;&amp; name !== "")</code>, which is convenient but can lead to unexpected bugs when values like <code>0</code> or <code>""</code> are valid data.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span> (age &gt; <span class="hljs-number">18</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Adult"</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (age &gt; <span class="hljs-number">12</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Teen"</span>);
} <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Child"</span>);
}

<span class="hljs-comment">// Ternary operator</span>
<span class="hljs-keyword">const</span> status = age &gt; <span class="hljs-number">18</span> ? <span class="hljs-string">"Adult"</span> : <span class="hljs-string">"Minor"</span>;

<span class="hljs-comment">// JavaScript's "truthy" and "falsy" concept</span>
<span class="hljs-keyword">if</span> (name) {  <span class="hljs-comment">// Checks if name is truthy (not just === true)</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Name exists"</span>);
}

<span class="hljs-comment">// These are ALL falsy (evaluate to false in conditions):</span>
<span class="hljs-comment">// - false</span>
<span class="hljs-comment">// - 0</span>
<span class="hljs-comment">// - "" (empty string)</span>
<span class="hljs-comment">// - null</span>
<span class="hljs-comment">// - undefined</span>
<span class="hljs-comment">// - NaN</span>

<span class="hljs-comment">// Everything else is truthy, including:</span>
<span class="hljs-comment">// - "0" (string with zero)</span>
<span class="hljs-comment">// - "false" (string)</span>
<span class="hljs-comment">// - [] (empty array)</span>
<span class="hljs-comment">// - {} (empty object)</span>

<span class="hljs-comment">// This can be convenient but also dangerous:</span>
<span class="hljs-keyword">if</span> (count) {  <span class="hljs-comment">// Oops! Fails when count is 0</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Has items"</span>);
}

<span class="hljs-comment">// Better to be explicit:</span>
<span class="hljs-keyword">if</span> (count &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Has items"</span>);
}
</code></pre>
<p><strong>Python - No parentheses, indentation matters</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> age &gt; <span class="hljs-number">18</span>:
    print(<span class="hljs-string">"Adult"</span>)
<span class="hljs-keyword">elif</span> age &gt; <span class="hljs-number">12</span>:  <span class="hljs-comment"># "elif", not "else if"</span>
    print(<span class="hljs-string">"Teen"</span>)
<span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">"Child"</span>)

<span class="hljs-comment"># Ternary (different syntax)</span>
status = <span class="hljs-string">"Adult"</span> <span class="hljs-keyword">if</span> age &gt; <span class="hljs-number">18</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"Minor"</span>
</code></pre>
<p><strong>Go - No parentheses, but braces required</strong></p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> age &gt; <span class="hljs-number">18</span> {
    fmt.Println(<span class="hljs-string">"Adult"</span>)
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> age &gt; <span class="hljs-number">12</span> {
    fmt.Println(<span class="hljs-string">"Teen"</span>)
} <span class="hljs-keyword">else</span> {
    fmt.Println(<span class="hljs-string">"Child"</span>)
}

<span class="hljs-comment">// No ternary operator! Must use if-else</span>
<span class="hljs-keyword">var</span> status <span class="hljs-keyword">string</span>
<span class="hljs-keyword">if</span> age &gt; <span class="hljs-number">18</span> {
    status = <span class="hljs-string">"Adult"</span>
} <span class="hljs-keyword">else</span> {
    status = <span class="hljs-string">"Minor"</span>
}

<span class="hljs-comment">// Go special: if with initialization</span>
<span class="hljs-keyword">if</span> err := doSomething(); err != <span class="hljs-literal">nil</span> {
    <span class="hljs-comment">// err only exists in this block</span>
    fmt.Println(err)
}
</code></pre>
<h3 id="heading-for-loops">For loops</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Traditional for</strong></td><td><code>for (int i = 0; i &lt; 10; i++)</code></td><td><code>for (let i = 0; i &lt; 10; i++)</code></td><td><code>for i in range(10):</code></td><td><code>for i := 0; i &lt; 10; i++</code></td></tr>
<tr>
<td><strong>For-each</strong></td><td><code>for (String s : list)</code></td><td><code>for (const s of list)</code></td><td><code>for s in list:</code></td><td><code>for _, s := range list</code></td></tr>
<tr>
<td><strong>While-style</strong></td><td><code>while (condition)</code></td><td><code>while (condition)</code></td><td><code>while condition:</code></td><td><code>for condition { }</code></td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-3">Why they're different</h3>
<p><strong>Java - Traditional C-style</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// Traditional for</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    System.out.println(i);
}

<span class="hljs-comment">// For-each (enhanced for loop)</span>
List&lt;String&gt; names = List.of(<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>);
<span class="hljs-keyword">for</span> (String name : names) {
    System.out.println(name);
}

<span class="hljs-comment">// Stream forEach (Java 8+, functional style)</span>
names.forEach(name -&gt; System.out.println(name));

<span class="hljs-comment">// Or with method reference</span>
names.forEach(System.out::println);

<span class="hljs-comment">// While loop</span>
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">10</span>) {
    System.out.println(i);
    i++;
}
</code></pre>
<p><strong>JavaScript - Multiple ways</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Traditional for</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    <span class="hljs-built_in">console</span>.log(i);
}

<span class="hljs-comment">// for...of (modern, for values)</span>
<span class="hljs-keyword">const</span> names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">of</span> names) {
    <span class="hljs-built_in">console</span>.log(name);
}

<span class="hljs-comment">// for...in (for keys/indices - avoid for arrays)</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> index <span class="hljs-keyword">in</span> names) {
    <span class="hljs-built_in">console</span>.log(index);  <span class="hljs-comment">// Prints "0", "1", "2"</span>
}

<span class="hljs-comment">// forEach (functional style)</span>
names.forEach(<span class="hljs-function"><span class="hljs-params">name</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(name));

<span class="hljs-comment">// While loop</span>
<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">10</span>) {
    <span class="hljs-built_in">console</span>.log(i);
    i++;
}
</code></pre>
<p><strong>Python - Simple and consistent</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># Range-based for (most common)</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
    print(i)

<span class="hljs-comment"># For-each</span>
names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>]
<span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> names:
    print(name)

<span class="hljs-comment"># With index</span>
<span class="hljs-keyword">for</span> i, name <span class="hljs-keyword">in</span> enumerate(names):
    print(<span class="hljs-string">f"<span class="hljs-subst">{i}</span>: <span class="hljs-subst">{name}</span>"</span>)

<span class="hljs-comment"># While loop</span>
i = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> i &lt; <span class="hljs-number">10</span>:
    print(i)
    i += <span class="hljs-number">1</span>
</code></pre>
<p><strong>Go - Only "for", but flexible</strong></p>
<p>Go only has ONE loop keyword: <code>for</code>. It replaces for, while, and for-each:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Traditional for</span>
<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++ {
    fmt.Println(i)
}

<span class="hljs-comment">// While-style (no "while" keyword!)</span>
i := <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> i &lt; <span class="hljs-number">10</span> {
    fmt.Println(i)
    i++
}

<span class="hljs-comment">// Infinite loop</span>
<span class="hljs-keyword">for</span> {
    <span class="hljs-comment">// break to exit</span>
}

<span class="hljs-comment">// Range over slice</span>
names := []<span class="hljs-keyword">string</span>{<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>}
<span class="hljs-keyword">for</span> i, name := <span class="hljs-keyword">range</span> names {
    fmt.Printf(<span class="hljs-string">"%d: %s\n"</span>, i, name)
}

<span class="hljs-comment">// Just the value (ignore index with _)</span>
<span class="hljs-keyword">for</span> _, name := <span class="hljs-keyword">range</span> names {
    fmt.Println(name)
}

<span class="hljs-comment">// Just the index</span>
<span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> names {
    fmt.Println(i)
}

<span class="hljs-comment">// Range over map</span>
ages := <span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">int</span>{<span class="hljs-string">"Alice"</span>: <span class="hljs-number">30</span>, <span class="hljs-string">"Bob"</span>: <span class="hljs-number">25</span>}
<span class="hljs-keyword">for</span> name, age := <span class="hljs-keyword">range</span> ages {
    fmt.Printf(<span class="hljs-string">"%s: %d\n"</span>, name, age)
}
</code></pre>
<hr />
<h2 id="heading-lists-maps-and-collections">Lists, Maps, and Collections</h2>
<h3 id="heading-how-to-create-and-use-them">How to create and use them</h3>
<h4 id="heading-lists-ordered-collections">Lists (ordered collections)</h4>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Create empty list</strong></td><td><code>List&lt;String&gt; names = new ArrayList&lt;&gt;();</code></td><td><code>const names = [];</code></td><td><code>names = []</code></td><td><code>names := []string{}</code></td></tr>
<tr>
<td><strong>Create with values</strong></td><td><code>List&lt;String&gt; names = Arrays.asList("Alice", "Bob");</code></td><td><code>const names = ["Alice", "Bob"];</code></td><td><code>names = ["Alice", "Bob"]</code></td><td><code>names := []string{"Alice", "Bob"}</code></td></tr>
<tr>
<td><strong>Add item</strong></td><td><code>names.add("Charlie");</code></td><td><code>names.push("Charlie");</code></td><td><code>names.append("Charlie")</code></td><td><code>names = append(names, "Charlie")</code></td></tr>
<tr>
<td><strong>Get item</strong></td><td><code>names.get(0)</code></td><td><code>names[0]</code></td><td><code>names[0]</code></td><td><code>names[0]</code></td></tr>
<tr>
<td><strong>Length</strong></td><td><code>names.size()</code></td><td><code>names.length</code></td><td><code>len(names)</code></td><td><code>len(names)</code></td></tr>
</tbody>
</table>
</div><h4 id="heading-mapsdictionaries-key-value-pairs">Maps/Dictionaries (key-value pairs)</h4>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Create empty map</strong></td><td><code>Map&lt;String, Integer&gt; ages = new HashMap&lt;&gt;();</code></td><td><code>const ages = {};</code> or <code>new Map()</code></td><td><code>ages = {}</code></td><td><code>ages := make(map[string]int)</code></td></tr>
<tr>
<td><strong>Create with values</strong></td><td>See below</td><td><code>const ages = {Alice: 30, Bob: 25};</code></td><td><code>ages = {"Alice": 30, "Bob": 25}</code></td><td><code>ages := map[string]int{"Alice": 30, "Bob": 25}</code></td></tr>
<tr>
<td><strong>Add/update</strong></td><td><code>ages.put("Alice", 30);</code></td><td><code>ages.Alice = 30;</code> or <code>ages["Alice"] = 30;</code></td><td><code>ages["Alice"] = 30</code></td><td><code>ages["Alice"] = 30</code></td></tr>
<tr>
<td><strong>Get value</strong></td><td><code>ages.get("Alice")</code></td><td><code>ages.Alice</code> or <code>ages["Alice"]</code></td><td><code>ages["Alice"]</code></td><td><code>ages["Alice"]</code></td></tr>
<tr>
<td><strong>Check if key exists</strong></td><td><code>ages.containsKey("Alice")</code></td><td><code>"Alice" in ages</code> or <code>ages.has("Alice")</code></td><td><code>"Alice" in ages</code></td><td><code>_, exists := ages["Alice"]</code></td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-4">Why they're different</h3>
<p><strong>Java - Arrays vs Lists</strong></p>
<p>Java has two main ways to work with collections:</p>
<p><strong>Arrays (fixed size):</strong></p>
<pre><code class="lang-java">String[] names = <span class="hljs-keyword">new</span> String[<span class="hljs-number">3</span>];  <span class="hljs-comment">// Fixed size: 3</span>
names[<span class="hljs-number">0</span>] = <span class="hljs-string">"Alice"</span>;
names[<span class="hljs-number">1</span>] = <span class="hljs-string">"Bob"</span>;

<span class="hljs-comment">// Or initialize with values</span>
String[] names = {<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>};
</code></pre>
<p><strong>Lists (dynamic size, more flexible):</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// Modern way (Java 10+ with var)</span>
<span class="hljs-keyword">var</span> names = <span class="hljs-keyword">new</span> ArrayList&lt;String&gt;();
names.add(<span class="hljs-string">"Alice"</span>);
names.add(<span class="hljs-string">"Bob"</span>);

<span class="hljs-comment">// Or with initial values</span>
<span class="hljs-keyword">var</span> names = List.of(<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>);  <span class="hljs-comment">// Java 9+, immutable</span>
<span class="hljs-keyword">var</span> names = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;(List.of(<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>));  <span class="hljs-comment">// Mutable copy</span>

<span class="hljs-comment">// Pre-Java 10 (verbose)</span>
List&lt;String&gt; names = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();
</code></pre>
<p><strong>Maps in Java:</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// Modern way</span>
<span class="hljs-keyword">var</span> ages = <span class="hljs-keyword">new</span> HashMap&lt;String, Integer&gt;();
ages.put(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>);
ages.put(<span class="hljs-string">"Bob"</span>, <span class="hljs-number">25</span>);

<span class="hljs-comment">// With initial values (Java 9+)</span>
<span class="hljs-keyword">var</span> ages = Map.of(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-number">25</span>);  <span class="hljs-comment">// Immutable</span>

<span class="hljs-comment">// Pre-Java 10</span>
Map&lt;String, Integer&gt; ages = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
</code></pre>
<p><strong>JavaScript - Arrays and Objects (or Map)</strong></p>
<p>JavaScript uses arrays for lists and objects for key-value pairs:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Arrays (lists)</span>
<span class="hljs-keyword">const</span> names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>];
names.push(<span class="hljs-string">"Charlie"</span>);  <span class="hljs-comment">// Add to end</span>
names[<span class="hljs-number">0</span>];  <span class="hljs-comment">// "Alice"</span>

<span class="hljs-comment">// Objects as maps (most common)</span>
<span class="hljs-keyword">const</span> ages = {
    <span class="hljs-attr">Alice</span>: <span class="hljs-number">30</span>,
    <span class="hljs-attr">Bob</span>: <span class="hljs-number">25</span>
};
ages.Charlie = <span class="hljs-number">35</span>;  <span class="hljs-comment">// Add new key</span>

<span class="hljs-comment">// Real Map (less common, but better for non-string keys)</span>
<span class="hljs-keyword">const</span> ages = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>();
ages.set(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>);
ages.get(<span class="hljs-string">"Alice"</span>);  <span class="hljs-comment">// 30</span>
</code></pre>
<p><strong>Python - Lists and Dicts</strong></p>
<p>Python keeps it simple - lists and dictionaries:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Lists</span>
names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>]
names.append(<span class="hljs-string">"Charlie"</span>)
names[<span class="hljs-number">0</span>]  <span class="hljs-comment"># "Alice"</span>

<span class="hljs-comment"># Dictionaries (dicts)</span>
ages = {<span class="hljs-string">"Alice"</span>: <span class="hljs-number">30</span>, <span class="hljs-string">"Bob"</span>: <span class="hljs-number">25</span>}
ages[<span class="hljs-string">"Charlie"</span>] = <span class="hljs-number">35</span>  <span class="hljs-comment"># Add new key</span>
ages[<span class="hljs-string">"Alice"</span>]  <span class="hljs-comment"># 30</span>

<span class="hljs-comment"># Check if key exists</span>
<span class="hljs-keyword">if</span> <span class="hljs-string">"Alice"</span> <span class="hljs-keyword">in</span> ages:
    print(ages[<span class="hljs-string">"Alice"</span>])
</code></pre>
<p><strong>Go - Slices and Maps, with <code>make</code></strong></p>
<p>Go has <strong>slices</strong> (dynamic arrays) and <strong>maps</strong>:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Slices (like dynamic arrays)</span>
names := []<span class="hljs-keyword">string</span>{<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>}  <span class="hljs-comment">// Create with values</span>
names = <span class="hljs-built_in">append</span>(names, <span class="hljs-string">"Charlie"</span>)  <span class="hljs-comment">// Must reassign!</span>
names[<span class="hljs-number">0</span>]  <span class="hljs-comment">// "Alice"</span>

<span class="hljs-comment">// Or with make (preallocate capacity)</span>
names := <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">string</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>)  <span class="hljs-comment">// length 0, capacity 10</span>

<span class="hljs-comment">// Maps</span>
ages := <span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">int</span>{<span class="hljs-string">"Alice"</span>: <span class="hljs-number">30</span>, <span class="hljs-string">"Bob"</span>: <span class="hljs-number">25</span>}
ages[<span class="hljs-string">"Charlie"</span>] = <span class="hljs-number">35</span>

<span class="hljs-comment">// Or with make</span>
ages := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">int</span>)
ages[<span class="hljs-string">"Alice"</span>] = <span class="hljs-number">30</span>

<span class="hljs-comment">// Check if key exists</span>
age, exists := ages[<span class="hljs-string">"Alice"</span>]
<span class="hljs-keyword">if</span> exists {
    fmt.Println(age)
}
</code></pre>
<p><strong>What is <code>make()</code>?</strong></p>
<p><code>make()</code> is Go's built-in function for creating slices, maps, and channels. You need it when:</p>
<p><strong>For slices:</strong></p>
<ul>
<li>You want to preallocate capacity (performance optimization)</li>
<li><code>make([]string, length, capacity)</code><ul>
<li><code>length</code>: current size (how many elements exist now)</li>
<li><code>capacity</code>: space allocated (how much it can grow before reallocating)</li>
</ul>
</li>
</ul>
<pre><code class="lang-go"><span class="hljs-comment">// Empty slice with capacity 100 - efficient if you know you'll add ~100 items</span>
names := <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">string</span>, <span class="hljs-number">0</span>, <span class="hljs-number">100</span>)  <span class="hljs-comment">// length=0, capacity=100</span>

<span class="hljs-comment">// Slice with 5 elements initialized to zero values</span>
numbers := <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">int</span>, <span class="hljs-number">5</span>)  <span class="hljs-comment">// [0, 0, 0, 0, 0]</span>

<span class="hljs-comment">// Without make (also valid)</span>
names := []<span class="hljs-keyword">string</span>{}  <span class="hljs-comment">// Empty slice, capacity 0</span>
</code></pre>
<p><strong>For maps:</strong></p>
<ul>
<li>You must use <code>make()</code> to create an empty map</li>
<li>Maps can't be created with <code>{}</code> like slices</li>
</ul>
<pre><code class="lang-go">ages := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">int</span>)  <span class="hljs-comment">// Empty map, ready to use</span>

<span class="hljs-comment">// This would panic:</span>
<span class="hljs-comment">// var ages map[string]int  // nil map, can't add to it!</span>
<span class="hljs-comment">// ages["Alice"] = 30  // PANIC!</span>
</code></pre>
<p><strong>Why <code>make()</code> exists:</strong></p>
<ul>
<li>Slices and maps need memory allocation</li>
<li><code>make()</code> allocates and initializes the underlying data structure</li>
<li>It's explicit: you can see when you're allocating memory</li>
</ul>
<p><strong><code>make()</code> vs <code>new()</code> in Go:</strong></p>
<p>Go has two allocation functions that confuse beginners:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td><code>make()</code></td><td><code>new()</code></td></tr>
</thead>
<tbody>
<tr>
<td><strong>What it returns</strong></td><td>Initialized value</td><td>Pointer to zeroed memory</td></tr>
<tr>
<td><strong>Use for</strong></td><td>Slices, maps, channels only</td><td>Any type</td></tr>
<tr>
<td><strong>Returns ready to use?</strong></td><td>Yes</td><td>No (need to initialize)</td></tr>
<tr>
<td><strong>Common usage</strong></td><td>Very common</td><td>Rare (just use <code>&amp;T{}</code>)</td></tr>
</tbody>
</table>
</div><pre><code class="lang-go"><span class="hljs-comment">// make() - for slices, maps, channels - RETURNS READY-TO-USE VALUE</span>
myMap := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">int</span>)  <span class="hljs-comment">// Returns initialized map</span>
myMap[<span class="hljs-string">"key"</span>] = <span class="hljs-number">1</span>  <span class="hljs-comment">// Works immediately</span>

<span class="hljs-comment">// new() - for any type - RETURNS POINTER TO ZEROED MEMORY</span>
myMap := <span class="hljs-built_in">new</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">int</span>)  <span class="hljs-comment">// Returns *map[string]int (pointer to nil map)</span>
<span class="hljs-comment">// *myMap["key"] = 1  // PANIC! The map is nil, not initialized</span>

<span class="hljs-comment">// Better alternative to new() - just use &amp; with composite literal</span>
<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    Name <span class="hljs-keyword">string</span>
    Age  <span class="hljs-keyword">int</span>
}

<span class="hljs-comment">// Don't do this:</span>
p := <span class="hljs-built_in">new</span>(Person)  <span class="hljs-comment">// Returns *Person with zero values</span>
p.Name = <span class="hljs-string">"Alice"</span>

<span class="hljs-comment">// Do this instead:</span>
p := &amp;Person{Name: <span class="hljs-string">"Alice"</span>, Age: <span class="hljs-number">30</span>}  <span class="hljs-comment">// Clearer and more idiomatic</span>
</code></pre>
<p><strong>Summary:</strong></p>
<ul>
<li><strong><code>make()</code></strong>: Use for slices, maps, channels - returns ready-to-use value</li>
<li><strong><code>new()</code></strong>: Almost never use - just use <code>&amp;T{}</code> instead</li>
<li><strong>Java comparison</strong>: <code>make()</code> is like <code>new ArrayList&lt;&gt;()</code> - allocates AND initializes</li>
<li><strong><code>new()</code> is like Java's <code>new</code></strong> - but in Go you rarely need it</li>
</ul>
<h3 id="heading-what-are-slices">What are slices?</h3>
<p><strong>In Go</strong>, a slice is like a dynamic array:</p>
<ul>
<li>Can grow and shrink</li>
<li><code>append()</code> adds items (you must reassign: <code>names = append(names, "Charlie")</code>)</li>
<li>More flexible than fixed arrays</li>
</ul>
<p><strong>Why Go uses slices:</strong></p>
<ul>
<li><strong>Performance</strong>: A slice is just a pointer to an underlying array, a length, and a capacity</li>
<li><strong>Memory efficient</strong>: Multiple slices can share the same underlying array</li>
<li><strong>Explicit control</strong>: You can see when memory is being allocated (when you <code>append()</code> beyond capacity)</li>
<li><strong>Simplicity</strong>: One simple concept instead of ArrayList, LinkedList, Vector, etc.</li>
<li><strong>Go philosophy</strong>: "There should be one obvious way to do it" - slices are that way</li>
</ul>
<p>Example of why it's efficient:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Creating a slice from an array - no copying!</span>
arr := [<span class="hljs-number">5</span>]<span class="hljs-keyword">int</span>{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}
slice := arr[<span class="hljs-number">1</span>:<span class="hljs-number">4</span>]  <span class="hljs-comment">// [2, 3, 4] - just a view, not a copy</span>

<span class="hljs-comment">// Preallocating capacity avoids multiple allocations</span>
names := <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">string</span>, <span class="hljs-number">0</span>, <span class="hljs-number">100</span>)  <span class="hljs-comment">// Space for 100, but length 0</span>
<span class="hljs-comment">// Now you can append 100 items without reallocating</span>
</code></pre>
<p><strong>In Java</strong>, you don't use the term "slice", but:</p>
<ul>
<li>Use <code>ArrayList</code> for dynamic arrays</li>
<li>Use arrays <code>[]</code> for fixed-size</li>
<li><code>ArrayList</code> hides the complexity (automatic resizing), but you pay for it with less control</li>
</ul>
<p><strong>In JavaScript</strong>, arrays are already dynamic (like slices)</p>
<ul>
<li>JavaScript hides all the complexity</li>
<li>You don't think about capacity or reallocation</li>
</ul>
<p><strong>In Python</strong>, lists are already dynamic (like slices)</p>
<ul>
<li>Python hides all the complexity</li>
<li><strong>Python philosophy</strong>: "We're all consenting adults here" - Python trusts you to use features responsibly</li>
<li>Python gives you power without forcing you to manage low-level details</li>
</ul>
<h3 id="heading-iterating-over-collections">Iterating over collections</h3>
<p><strong>Java - Traditional and enhanced for loop</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">var</span> names = List.of(<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>);

<span class="hljs-comment">// Traditional for loop</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; names.size(); i++) {
    System.out.println(names.get(i));
}

<span class="hljs-comment">// Enhanced for loop (for-each)</span>
<span class="hljs-keyword">for</span> (String name : names) {
    System.out.println(name);
}

<span class="hljs-comment">// Modern way - streams (Java 8+)</span>
names.forEach(name -&gt; System.out.println(name));
</code></pre>
<p><strong>JavaScript - for, for...of, forEach</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>];

<span class="hljs-comment">// Traditional for loop</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; names.length; i++) {
    <span class="hljs-built_in">console</span>.log(names[i]);
}

<span class="hljs-comment">// for...of (modern, recommended)</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">of</span> names) {
    <span class="hljs-built_in">console</span>.log(name);
}

<span class="hljs-comment">// forEach method</span>
names.forEach(<span class="hljs-function"><span class="hljs-params">name</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(name));
</code></pre>
<p><strong>Python - for loop</strong></p>
<pre><code class="lang-python">names = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>]

<span class="hljs-comment"># Standard way</span>
<span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> names:
    print(name)

<span class="hljs-comment"># With index</span>
<span class="hljs-keyword">for</span> i, name <span class="hljs-keyword">in</span> enumerate(names):
    print(<span class="hljs-string">f"<span class="hljs-subst">{i}</span>: <span class="hljs-subst">{name}</span>"</span>)
</code></pre>
<p><strong>Go - range</strong></p>
<pre><code class="lang-go">names := []<span class="hljs-keyword">string</span>{<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>}

<span class="hljs-comment">// With index and value</span>
<span class="hljs-keyword">for</span> i, name := <span class="hljs-keyword">range</span> names {
    fmt.Printf(<span class="hljs-string">"%d: %s\n"</span>, i, name)
}

<span class="hljs-comment">// Just value (ignore index with _)</span>
<span class="hljs-keyword">for</span> _, name := <span class="hljs-keyword">range</span> names {
    fmt.Println(name)
}

<span class="hljs-comment">// Just index</span>
<span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> names {
    fmt.Println(i)
}
</code></pre>
<hr />
<h2 id="heading-data-structures-tuples-records-and-structs">Data Structures: Tuples, Records, and Structs</h2>
<h3 id="heading-quick-comparison">Quick comparison</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Simple data holder</strong></td><td>Record (Java 14+)</td><td>Object literal</td><td>Tuple or NamedTuple</td><td>Struct</td></tr>
<tr>
<td><strong>Syntax</strong></td><td><code>record Point(int x, int y)</code></td><td><code>{x: 3, y: 4}</code></td><td><code>(3, 4)</code> or <code>Point(3, 4)</code></td><td><code>type Point struct { X, Y int }</code></td></tr>
<tr>
<td><strong>Immutable?</strong></td><td>Yes (records are)</td><td>No (unless frozen)</td><td>Yes (tuples), No (dataclasses)</td><td>No (but can't reassign struct)</td></tr>
</tbody>
</table>
</div><h3 id="heading-why-they-exist">Why they exist</h3>
<p>All languages need a way to group related data without writing a full class. Each language has different solutions:</p>
<p><strong>Java - Records (Java 14+)</strong></p>
<p>Before records, you needed a lot of boilerplate:</p>
<pre><code class="lang-java"><span class="hljs-comment">// Old way - verbose</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Point</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> x;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> y;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Point</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x, <span class="hljs-keyword">int</span> y)</span> </span>{
        <span class="hljs-keyword">this</span>.x = x;
        <span class="hljs-keyword">this</span>.y = y;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">x</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> x; }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">y</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> y; }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">equals</span><span class="hljs-params">(Object o)</span> </span>{ <span class="hljs-comment">/* ... */</span> }
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">hashCode</span><span class="hljs-params">()</span> </span>{ <span class="hljs-comment">/* ... */</span> }
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>{ <span class="hljs-comment">/* ... */</span> }
}

<span class="hljs-comment">// New way - record (one line!)</span>
<span class="hljs-function">record <span class="hljs-title">Point</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x, <span class="hljs-keyword">int</span> y)</span> </span>{ }

<span class="hljs-comment">// Usage</span>
Point p = <span class="hljs-keyword">new</span> Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>);
System.out.println(p.x());  <span class="hljs-comment">// 3</span>
System.out.println(p);      <span class="hljs-comment">// Point[x=3, y=4] - toString() auto-generated</span>
</code></pre>
<p><strong>Records automatically give you:</strong></p>
<ul>
<li>Constructor</li>
<li>Getters (but named <code>x()</code>, not <code>getX()</code>)</li>
<li><code>equals()</code>, <code>hashCode()</code>, <code>toString()</code></li>
<li>Immutable fields (can't change after creation)</li>
</ul>
<p><strong>JavaScript - Object literals</strong></p>
<p>JavaScript is flexible - just use an object:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Simple object literal</span>
<span class="hljs-keyword">const</span> point = { <span class="hljs-attr">x</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">4</span> };
<span class="hljs-built_in">console</span>.log(point.x);  <span class="hljs-comment">// 3</span>

<span class="hljs-comment">// Can modify (unless you freeze it)</span>
point.x = <span class="hljs-number">10</span>;  <span class="hljs-comment">// OK</span>

<span class="hljs-comment">// Make it immutable</span>
<span class="hljs-keyword">const</span> frozenPoint = <span class="hljs-built_in">Object</span>.freeze({ <span class="hljs-attr">x</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">4</span> });
<span class="hljs-comment">// frozenPoint.x = 10;  // Error in strict mode</span>

<span class="hljs-comment">// "Factory" function for consistency</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createPoint</span>(<span class="hljs-params">x, y</span>) </span>{
    <span class="hljs-keyword">return</span> { x, y };  <span class="hljs-comment">// Shorthand property syntax</span>
}

<span class="hljs-keyword">const</span> p = createPoint(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>);
</code></pre>
<p><strong>JavaScript doesn't have a dedicated "record" type</strong> - objects are flexible enough for most use cases.</p>
<p><strong>Python - Tuples, NamedTuples, and Dataclasses</strong></p>
<p>Python has THREE options:</p>
<pre><code class="lang-python"><span class="hljs-comment"># 1. Tuple - immutable, but no names</span>
point = (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
x, y = point  <span class="hljs-comment"># Unpack to use</span>
print(point[<span class="hljs-number">0</span>])  <span class="hljs-comment"># 3 - access by index (not great)</span>

<span class="hljs-comment"># 2. NamedTuple - immutable with names</span>
<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
Point = namedtuple(<span class="hljs-string">'Point'</span>, [<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])

p = Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
print(p.x)  <span class="hljs-comment"># 3 - access by name (better!)</span>
print(p[<span class="hljs-number">0</span>])  <span class="hljs-comment"># 3 - can still access by index</span>
<span class="hljs-comment"># p.x = 10  # Error - immutable</span>

<span class="hljs-comment"># 3. Dataclass - mutable, more features</span>
<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass

<span class="hljs-meta">@dataclass</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Point</span>:</span>
    x: int
    y: int

p = Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
print(p.x)  <span class="hljs-comment"># 3</span>
p.x = <span class="hljs-number">10</span>  <span class="hljs-comment"># OK - mutable by default</span>

<span class="hljs-comment"># Can make immutable</span>
<span class="hljs-meta">@dataclass(frozen=True)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Point</span>:</span>
    x: int
    y: int
</code></pre>
<p><strong>When to use which:</strong></p>
<ul>
<li><strong>Tuple</strong>: Simple, temporary grouping (like returning multiple values)</li>
<li><strong>NamedTuple</strong>: Immutable data with named fields</li>
<li><strong>Dataclass</strong>: When you need mutability or more features (default values, methods, etc.)</li>
</ul>
<p><strong>Go - Structs</strong></p>
<p>Go only has structs - simple and consistent:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Define struct</span>
<span class="hljs-keyword">type</span> Point <span class="hljs-keyword">struct</span> {
    X <span class="hljs-keyword">int</span>
    Y <span class="hljs-keyword">int</span>
}

<span class="hljs-comment">// Create instances</span>
p1 := Point{X: <span class="hljs-number">3</span>, Y: <span class="hljs-number">4</span>}
p2 := Point{<span class="hljs-number">3</span>, <span class="hljs-number">4</span>}  <span class="hljs-comment">// Positional (less clear)</span>

<span class="hljs-comment">// Access fields</span>
fmt.Println(p1.X)  <span class="hljs-comment">// 3</span>

<span class="hljs-comment">// Modify (structs are mutable)</span>
p1.X = <span class="hljs-number">10</span>

<span class="hljs-comment">// Can't reassign the whole struct if using :=</span>
<span class="hljs-comment">// But can modify fields</span>
</code></pre>
<p><strong>Go structs are simple:</strong></p>
<ul>
<li>No inheritance</li>
<li>No methods on the struct definition (methods are separate)</li>
<li>Can be passed by value or by pointer</li>
<li>Uppercase = exported (public), lowercase = private</li>
</ul>
<hr />
<h2 id="heading-methods-and-parameters">Methods and Parameters</h2>
<h3 id="heading-how-to-declare-methods">How to declare methods</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Basic method</strong></td><td><code>void greet(String name) { }</code></td><td><code>function greet(name) { }</code></td><td><code>def greet(name):</code></td><td><code>func greet(name string) { }</code></td></tr>
<tr>
<td><strong>With return type</strong></td><td><code>String getName() { return "John"; }</code></td><td><code>function getName() { return "John"; }</code></td><td><code>def get_name() -&gt; str: return "John"</code></td><td><code>func getName() string { return "John" }</code></td></tr>
<tr>
<td><strong>Multiple return values</strong></td><td>Not supported</td><td>Not supported</td><td><code>return name, age</code> (tuple)</td><td><code>func getUser() (string, int) { }</code></td></tr>
</tbody>
</table>
</div><h3 id="heading-how-parameters-work-pass-by-value">How parameters work (pass by value)</h3>
<p><strong>All four languages pass parameters by value</strong> - but this means different things for primitives vs objects.</p>
<p><strong>The rule:</strong></p>
<ul>
<li><strong>Primitives</strong> (int, string, bool): Copy of the value is passed</li>
<li><strong>Objects/References</strong>: Copy of the reference is passed (you can modify the object, but can't replace it)</li>
</ul>
<h4 id="heading-java">Java</h4>
<pre><code class="lang-java"><span class="hljs-comment">// Primitives - pass by value (copy)</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">increment</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x)</span> </span>{
    x = x + <span class="hljs-number">1</span>;  <span class="hljs-comment">// Only changes local copy</span>
}

<span class="hljs-keyword">int</span> count = <span class="hljs-number">5</span>;
increment(count);
System.out.println(count);  <span class="hljs-comment">// Still 5 - unchanged</span>

<span class="hljs-comment">// Objects - pass by value of the reference</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">changeName</span><span class="hljs-params">(Person p)</span> </span>{
    p.name = <span class="hljs-string">"Jane"</span>;  <span class="hljs-comment">// Modifies the object ✓</span>
    p = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Bob"</span>);  <span class="hljs-comment">// Only changes local reference, not original ✗</span>
}

Person person = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Alice"</span>);
changeName(person);
System.out.println(person.name);  <span class="hljs-comment">// "Jane" - object was modified</span>
</code></pre>
<h4 id="heading-javascript">JavaScript</h4>
<pre><code class="lang-javascript"><span class="hljs-comment">// Primitives - pass by value (copy)</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">increment</span>(<span class="hljs-params">x</span>) </span>{
    x = x + <span class="hljs-number">1</span>;  <span class="hljs-comment">// Only changes local copy</span>
}

<span class="hljs-keyword">let</span> count = <span class="hljs-number">5</span>;
increment(count);
<span class="hljs-built_in">console</span>.log(count);  <span class="hljs-comment">// Still 5 - unchanged</span>

<span class="hljs-comment">// Objects - pass by value of the reference</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">changeName</span>(<span class="hljs-params">p</span>) </span>{
    p.name = <span class="hljs-string">"Jane"</span>;  <span class="hljs-comment">// Modifies the object ✓</span>
    p = { <span class="hljs-attr">name</span>: <span class="hljs-string">"Bob"</span> };  <span class="hljs-comment">// Only changes local reference, not original ✗</span>
}

<span class="hljs-keyword">let</span> person = { <span class="hljs-attr">name</span>: <span class="hljs-string">"Alice"</span> };
changeName(person);
<span class="hljs-built_in">console</span>.log(person.name);  <span class="hljs-comment">// "Jane" - object was modified</span>
</code></pre>
<h4 id="heading-python">Python</h4>
<pre><code class="lang-python"><span class="hljs-comment"># Primitives (immutable) - pass by value (copy)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">increment</span>(<span class="hljs-params">x</span>):</span>
    x = x + <span class="hljs-number">1</span>  <span class="hljs-comment"># Only changes local copy</span>

count = <span class="hljs-number">5</span>
increment(count)
print(count)  <span class="hljs-comment"># Still 5 - unchanged</span>

<span class="hljs-comment"># Objects (mutable) - pass by value of the reference</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">change_name</span>(<span class="hljs-params">p</span>):</span>
    p[<span class="hljs-string">"name"</span>] = <span class="hljs-string">"Jane"</span>  <span class="hljs-comment"># Modifies the object ✓</span>
    p = {<span class="hljs-string">"name"</span>: <span class="hljs-string">"Bob"</span>}  <span class="hljs-comment"># Only changes local reference, not original ✗</span>

person = {<span class="hljs-string">"name"</span>: <span class="hljs-string">"Alice"</span>}
change_name(person)
print(person[<span class="hljs-string">"name"</span>])  <span class="hljs-comment"># "Jane" - object was modified</span>
</code></pre>
<h4 id="heading-go">Go</h4>
<pre><code class="lang-go"><span class="hljs-comment">// Primitives - pass by value (copy)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">increment</span><span class="hljs-params">(x <span class="hljs-keyword">int</span>)</span></span> {
    x = x + <span class="hljs-number">1</span>  <span class="hljs-comment">// Only changes local copy</span>
}

count := <span class="hljs-number">5</span>
increment(count)
fmt.Println(count)  <span class="hljs-comment">// Still 5 - unchanged</span>

<span class="hljs-comment">// Structs - pass by value (ENTIRE STRUCT IS COPIED!)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">changeName</span><span class="hljs-params">(p Person)</span></span> {
    p.Name = <span class="hljs-string">"Jane"</span>  <span class="hljs-comment">// Only changes the COPY, not original ✗</span>
}

person := Person{Name: <span class="hljs-string">"Alice"</span>}
changeName(person)
fmt.Println(person.Name)  <span class="hljs-comment">// Still "Alice" - unchanged!</span>

<span class="hljs-comment">// Pointers - pass by value of the pointer (can modify original)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">changeNamePtr</span><span class="hljs-params">(p *Person)</span></span> {
    p.Name = <span class="hljs-string">"Jane"</span>  <span class="hljs-comment">// Modifies the original ✓</span>
    p = &amp;Person{Name: <span class="hljs-string">"Bob"</span>}  <span class="hljs-comment">// Only changes local pointer, not original ✗</span>
}

changeNamePtr(&amp;person)
fmt.Println(person.Name)  <span class="hljs-comment">// "Jane" - original was modified</span>
</code></pre>
<h3 id="heading-the-go-difference-pointers">The Go difference: Pointers</h3>
<p><strong>Go is the only language here with explicit pointers</strong> (like C/C++). The other languages hide pointers from you.</p>
<p>When you pass a struct in Go, <strong>the entire struct is copied</strong> - but it's a <strong>shallow copy</strong>:</p>
<ul>
<li>Primitive fields (int, string, bool) are copied</li>
<li>Slices, maps, pointers inside the struct are <strong>NOT</strong> deep copied - you get copies of the references</li>
</ul>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    Name <span class="hljs-keyword">string</span>
    Age  <span class="hljs-keyword">int</span>
}

<span class="hljs-comment">// Bad - copies entire struct</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">celebrateBirthday</span><span class="hljs-params">(p Person)</span></span> {
    p.Age = p.Age + <span class="hljs-number">1</span>  <span class="hljs-comment">// Modifies copy only</span>
}

<span class="hljs-comment">// Good - uses pointer, no copying</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">celebrateBirthday</span><span class="hljs-params">(p *Person)</span></span> {
    p.Age = p.Age + <span class="hljs-number">1</span>  <span class="hljs-comment">// Modifies original</span>
}

person := Person{Name: <span class="hljs-string">"Alice"</span>, Age: <span class="hljs-number">30</span>}
celebrateBirthday(&amp;person)  <span class="hljs-comment">// Pass pointer with &amp;</span>
fmt.Println(person.Age)  <span class="hljs-comment">// 31</span>
</code></pre>
<p><strong>Shallow copy example:</strong></p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Team <span class="hljs-keyword">struct</span> {
    Name    <span class="hljs-keyword">string</span>
    Members []<span class="hljs-keyword">string</span>  <span class="hljs-comment">// Slice is a reference</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">addMember</span><span class="hljs-params">(t Team, member <span class="hljs-keyword">string</span>)</span></span> {
    <span class="hljs-comment">// t is a copy, but t.Members points to the SAME underlying array</span>
    t.Members = <span class="hljs-built_in">append</span>(t.Members, member)  <span class="hljs-comment">// Might modify original slice!</span>
    t.Name = <span class="hljs-string">"New Name"</span>  <span class="hljs-comment">// Only changes the copy</span>
}

team := Team{Name: <span class="hljs-string">"A-Team"</span>, Members: []<span class="hljs-keyword">string</span>{<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>}}
addMember(team, <span class="hljs-string">"Charlie"</span>)
fmt.Println(team.Name)     <span class="hljs-comment">// "A-Team" - unchanged</span>
fmt.Println(team.Members)  <span class="hljs-comment">// Might be ["Alice", "Bob", "Charlie"] - CHANGED!</span>
                          <span class="hljs-comment">// (if append didn't reallocate)</span>
</code></pre>
<p><strong>This is confusing! Better to use pointers:</strong></p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">addMember</span><span class="hljs-params">(t *Team, member <span class="hljs-keyword">string</span>)</span></span> {
    t.Members = <span class="hljs-built_in">append</span>(t.Members, member)  <span class="hljs-comment">// Clear that we're modifying</span>
    t.Name = <span class="hljs-string">"New Name"</span>  <span class="hljs-comment">// Also modifies original</span>
}

addMember(&amp;team, <span class="hljs-string">"Charlie"</span>)  <span class="hljs-comment">// Explicit: we're passing a reference</span>
</code></pre>
<p><strong>Why Go has explicit pointers:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Java/JavaScript/Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Pointers</strong></td><td>Hidden (automatic references)</td><td>Explicit (<code>*</code> and <code>&amp;</code>)</td></tr>
<tr>
<td><strong>When you see it</strong></td><td>Never - objects "just work"</td><td>Always - <code>*Person</code> or <code>&amp;person</code></td></tr>
<tr>
<td><strong>Control</strong></td><td>No choice - objects are always references</td><td>You choose: value or pointer</td></tr>
<tr>
<td><strong>Performance</strong></td><td>Everything is a reference (pointer overhead)</td><td>Small structs by value (no pointer overhead), large structs by pointer</td></tr>
<tr>
<td><strong>Safety</strong></td><td>Easy to accidentally mutate</td><td>Must explicitly use pointers to mutate</td></tr>
</tbody>
</table>
</div><p><strong>Performance explained:</strong></p>
<p>In Java/JavaScript/Python, all objects are heap-allocated and passed by reference. Even a tiny object like a 2D point needs:</p>
<ul>
<li>Heap allocation</li>
<li>Garbage collection tracking</li>
<li>Pointer dereferencing to access fields</li>
</ul>
<pre><code class="lang-java"><span class="hljs-comment">// Java - even simple objects are on the heap</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Point</span> </span>{
    <span class="hljs-keyword">int</span> x, y;  <span class="hljs-comment">// Just 8 bytes of data</span>
}

Point p = <span class="hljs-keyword">new</span> Point();  <span class="hljs-comment">// Heap allocation, GC overhead</span>
p.x = <span class="hljs-number">10</span>;  <span class="hljs-comment">// Pointer dereference to access field</span>

<span class="hljs-comment">// Java Records (Java 14+) - still heap-allocated!</span>
<span class="hljs-function">record <span class="hljs-title">Point</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x, <span class="hljs-keyword">int</span> y)</span> </span>{ }
Point p = <span class="hljs-keyword">new</span> Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>);  <span class="hljs-comment">// Still on heap, still GC'd, still a reference</span>
</code></pre>
<p><strong>What about Java Records and Python NamedTuples?</strong></p>
<p>Yes, Java has <strong>records</strong> (Java 14+) and Python has <strong>namedtuples</strong> and <strong>dataclasses</strong>, but they don't solve the performance issue:</p>
<pre><code class="lang-java"><span class="hljs-comment">// Java Record - less code, but STILL heap-allocated</span>
<span class="hljs-function">record <span class="hljs-title">Point</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x, <span class="hljs-keyword">int</span> y)</span> </span>{ }

Point p1 = <span class="hljs-keyword">new</span> Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>);  <span class="hljs-comment">// Heap allocation</span>
Point p2 = p1;  <span class="hljs-comment">// p2 is a reference to the same object</span>
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># Python NamedTuple</span>
<span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple
Point = namedtuple(<span class="hljs-string">'Point'</span>, [<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])

p1 = Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)  <span class="hljs-comment"># Heap allocation</span>
p2 = p1  <span class="hljs-comment"># p2 is a reference to the same object</span>

<span class="hljs-comment"># Python dataclass (similar)</span>
<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass

<span class="hljs-meta">@dataclass</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Point</span>:</span>
    x: int
    y: int

p = Point(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)  <span class="hljs-comment"># Still heap-allocated</span>
</code></pre>
<p><strong>Key difference:</strong></p>
<ul>
<li><strong>Java Records/Python NamedTuples</strong>: Less boilerplate code, but still heap-allocated objects</li>
<li><strong>Go structs by value</strong>: Can be stack-allocated, no heap, no GC overhead</li>
</ul>
<p>Records and NamedTuples are about <strong>developer convenience</strong> (less code), not performance.</p>
<p>In Go, you can choose:</p>
<ul>
<li><strong>Small structs by value</strong>: Passed on the stack, no allocation, no GC, direct access</li>
<li><strong>Large structs by pointer</strong>: Avoid expensive copies</li>
</ul>
<pre><code class="lang-go"><span class="hljs-comment">// Small struct - pass by value is FASTER</span>
<span class="hljs-keyword">type</span> Point <span class="hljs-keyword">struct</span> {
    X, Y <span class="hljs-keyword">int</span>  <span class="hljs-comment">// Just 16 bytes</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">distance</span><span class="hljs-params">(p Point)</span> <span class="hljs-title">float64</span></span> {  <span class="hljs-comment">// Passed on stack, no allocation!</span>
    <span class="hljs-keyword">return</span> math.Sqrt(<span class="hljs-keyword">float64</span>(p.X*p.X + p.Y*p.Y))
}

p := Point{X: <span class="hljs-number">3</span>, Y: <span class="hljs-number">4</span>}
d := distance(p)  <span class="hljs-comment">// No heap allocation, very fast</span>

<span class="hljs-comment">// Large struct - pass by pointer</span>
<span class="hljs-keyword">type</span> Image <span class="hljs-keyword">struct</span> {
    Pixels [<span class="hljs-number">1920</span>][<span class="hljs-number">1080</span>]<span class="hljs-keyword">byte</span>  <span class="hljs-comment">// 2MB of data!</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">process</span><span class="hljs-params">(img *Image)</span></span> {  <span class="hljs-comment">// Pointer: no copying 2MB</span>
    <span class="hljs-comment">// work with img</span>
}
</code></pre>
<p><strong>Go's escape analysis: Stack vs Heap</strong></p>
<p>Go is unique - it can allocate structs on the <strong>stack</strong> instead of the heap:</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">createOnStack</span><span class="hljs-params">()</span> <span class="hljs-title">Point</span></span> {
    p := Point{X: <span class="hljs-number">3</span>, Y: <span class="hljs-number">4</span>}
    <span class="hljs-keyword">return</span> p  <span class="hljs-comment">// Returned by VALUE - stays on stack</span>
    <span class="hljs-comment">// Automatically cleaned when function returns - no GC needed!</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">createOnHeap</span><span class="hljs-params">()</span> *<span class="hljs-title">Point</span></span> {
    p := Point{X: <span class="hljs-number">3</span>, Y: <span class="hljs-number">4</span>}
    <span class="hljs-keyword">return</span> &amp;p  <span class="hljs-comment">// Returns POINTER - "escapes" to heap</span>
    <span class="hljs-comment">// Must be GC'd later</span>
}
</code></pre>
<p><strong>The Go compiler analyzes your code ("escape analysis") and decides:</strong></p>
<ul>
<li><strong>Stack allocation</strong>: If struct doesn't escape the function (not returned as pointer, not stored globally)<ul>
<li>Faster allocation</li>
<li>Automatic cleanup when function returns</li>
<li>No GC pressure</li>
<li>Better cache locality</li>
</ul>
</li>
<li><strong>Heap allocation</strong>: If struct escapes (returned as pointer, stored in field, sent to channel)<ul>
<li>Must be garbage collected</li>
<li>Slower but necessary when lifetime extends beyond function</li>
</ul>
</li>
</ul>
<p><strong>The other languages:</strong></p>
<ul>
<li><strong>Java</strong>: ALL objects are heap-allocated (except primitives like <code>int</code>)</li>
<li><strong>JavaScript</strong>: ALL objects are heap-allocated</li>
<li><strong>Python</strong>: ALL objects are heap-allocated</li>
</ul>
<p>Even a tiny <code>Point</code> object goes on the heap in Java/JS/Python, requiring GC tracking.</p>
<p><strong>Why this matters:</strong></p>
<ul>
<li><strong>Go</strong>: Small structs (like Point, Color, Rectangle) can be stack-allocated - faster than Java/Python</li>
<li><strong>Java/Python</strong>: ALL objects have heap allocation + GC overhead, even tiny ones</li>
<li><strong>Go</strong>: You optimize by choosing value vs pointer, and the compiler optimizes further with escape analysis</li>
</ul>
<p><strong>Why Go does this:</strong></p>
<ul>
<li><strong>Explicit</strong>: You can see when you're passing a pointer (<code>&amp;</code>) or dereferencing (<code>*</code>)</li>
<li><strong>Performance</strong>: You control when to copy vs reference (small structs by value, large structs by pointer)</li>
<li><strong>Safety</strong>: Accidental mutations are harder (need explicit pointer)</li>
<li><strong>Memory control</strong>: You know exactly what's on the stack vs heap</li>
</ul>
<p><strong>In Java/JavaScript/Python:</strong></p>
<ul>
<li>Objects are always passed by reference (automatic)</li>
<li>More convenient, but less explicit</li>
<li>Can lead to unexpected mutations</li>
<li>No way to pass objects "by value" (make a defensive copy if you need it)</li>
</ul>
<hr />
<h2 id="heading-classes-and-methods">Classes and Methods</h2>
<h3 id="heading-how-to-define-classes-with-methods">How to define classes with methods</h3>
<p><strong>Java - Traditional OOP</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-comment">// Fields</span>
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;

    <span class="hljs-comment">// Constructor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span><span class="hljs-params">(String name, <span class="hljs-keyword">int</span> age)</span> </span>{
        <span class="hljs-keyword">this</span>.name = name;
        <span class="hljs-keyword">this</span>.age = age;
    }

    <span class="hljs-comment">// Getter</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> name;
    }

    <span class="hljs-comment">// Method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">celebrate</span><span class="hljs-params">()</span> </span>{
        age++;
        System.out.println(name + <span class="hljs-string">" is now "</span> + age);
    }

    <span class="hljs-comment">// Static method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Person <span class="hljs-title">createDefault</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Unknown"</span>, <span class="hljs-number">0</span>);
    }
}

<span class="hljs-comment">// Usage</span>
Person p = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>);
p.celebrate();
</code></pre>
<p><strong>JavaScript - Prototype-based with class syntax</strong></p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-comment">// Constructor</span>
    <span class="hljs-keyword">constructor</span>(name, age) {
        <span class="hljs-built_in">this</span>.name = name;
        <span class="hljs-built_in">this</span>.age = age;
    }

    <span class="hljs-comment">// Method</span>
    celebrate() {
        <span class="hljs-built_in">this</span>.age++;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span> is now <span class="hljs-subst">${<span class="hljs-built_in">this</span>.age}</span>`</span>);
    }

    <span class="hljs-comment">// Getter</span>
    <span class="hljs-keyword">get</span> <span class="hljs-title">birthYear</span>() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getFullYear() - <span class="hljs-built_in">this</span>.age;
    }

    <span class="hljs-comment">// Static method</span>
    <span class="hljs-keyword">static</span> createDefault() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Unknown"</span>, <span class="hljs-number">0</span>);
    }
}

<span class="hljs-comment">// Usage</span>
<span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>);
p.celebrate();

<span class="hljs-comment">// JavaScript also has prototype-based approach (pre-ES6)</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name, age</span>) </span>{
    <span class="hljs-built_in">this</span>.name = name;
    <span class="hljs-built_in">this</span>.age = age;
}

Person.prototype.celebrate = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">this</span>.age++;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">this</span>.name + <span class="hljs-string">" is now "</span> + <span class="hljs-built_in">this</span>.age);
};
</code></pre>
<p><strong>Python - Simple and clean</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-comment"># Constructor</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name: str, age: int</span>):</span>
        self.name = name
        self.age = age

    <span class="hljs-comment"># Method</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">celebrate</span>(<span class="hljs-params">self</span>):</span>
        self.age += <span class="hljs-number">1</span>
        print(<span class="hljs-string">f"<span class="hljs-subst">{self.name}</span> is now <span class="hljs-subst">{self.age}</span>"</span>)

    <span class="hljs-comment"># Property (getter)</span>
<span class="hljs-meta">    @property</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">birth_year</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime
        <span class="hljs-keyword">return</span> datetime.now().year - self.age

    <span class="hljs-comment"># Static method</span>
<span class="hljs-meta">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_default</span>():</span>
        <span class="hljs-keyword">return</span> Person(<span class="hljs-string">"Unknown"</span>, <span class="hljs-number">0</span>)

    <span class="hljs-comment"># Class method</span>
<span class="hljs-meta">    @classmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">from_birth_year</span>(<span class="hljs-params">cls, name, birth_year</span>):</span>
        <span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime
        age = datetime.now().year - birth_year
        <span class="hljs-keyword">return</span> cls(name, age)

<span class="hljs-comment"># Usage</span>
p = Person(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>)
p.celebrate()
</code></pre>
<p><strong>Go - Structs with methods attached</strong></p>
<p>Go doesn't have classes - it has <strong>structs</strong> and <strong>methods are defined separately</strong>:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Define the struct (data)</span>
<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    Name <span class="hljs-keyword">string</span>
    age  <span class="hljs-keyword">int</span>  <span class="hljs-comment">// lowercase = private</span>
}

<span class="hljs-comment">// Constructor function (convention)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">NewPerson</span><span class="hljs-params">(name <span class="hljs-keyword">string</span>, age <span class="hljs-keyword">int</span>)</span> *<span class="hljs-title">Person</span></span> {
    <span class="hljs-keyword">return</span> &amp;Person{
        Name: name,
        age:  age,
    }
}

<span class="hljs-comment">// Method with pointer receiver (can modify the struct)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p *Person)</span> <span class="hljs-title">Celebrate</span><span class="hljs-params">()</span></span> {
    p.age++
    fmt.Printf(<span class="hljs-string">"%s is now %d\n"</span>, p.Name, p.age)
}

<span class="hljs-comment">// Method with value receiver (read-only)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">GetAge</span><span class="hljs-params">()</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">return</span> p.age
}

<span class="hljs-comment">// Method that returns something</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">IsAdult</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> {
    <span class="hljs-keyword">return</span> p.age &gt;= <span class="hljs-number">18</span>
}

<span class="hljs-comment">// "Static" method (just a regular function)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">CreateDefaultPerson</span><span class="hljs-params">()</span> *<span class="hljs-title">Person</span></span> {
    <span class="hljs-keyword">return</span> &amp;Person{Name: <span class="hljs-string">"Unknown"</span>, age: <span class="hljs-number">0</span>}
}

<span class="hljs-comment">// Usage</span>
p := NewPerson(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>)
p.Celebrate()
</code></pre>
<h3 id="heading-the-go-difference-receiver-functions">The Go difference: Receiver functions</h3>
<p><strong>Go methods are functions with a receiver parameter:</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// This method...</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p *Person)</span> <span class="hljs-title">Celebrate</span><span class="hljs-params">()</span></span> {
    p.age++
}

<span class="hljs-comment">// ...is just syntactic sugar for this:</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">Celebrate</span><span class="hljs-params">(p *Person)</span></span> {
    p.age++
}

<span class="hljs-comment">// Call it like a method:</span>
p.Celebrate()

<span class="hljs-comment">// Or like a function (but don't do this):</span>
(*Person).Celebrate(p)
</code></pre>
<p><strong>Pointer receiver vs value receiver:</strong></p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Counter <span class="hljs-keyword">struct</span> {
    count <span class="hljs-keyword">int</span>
}

<span class="hljs-comment">// Pointer receiver - modifies the original</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(c *Counter)</span> <span class="hljs-title">Increment</span><span class="hljs-params">()</span></span> {
    c.count++  <span class="hljs-comment">// Changes the original</span>
}

<span class="hljs-comment">// Value receiver - receives a copy</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(c Counter)</span> <span class="hljs-title">GetCount</span><span class="hljs-params">()</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">return</span> c.count  <span class="hljs-comment">// Just reads, doesn't modify</span>
}

<span class="hljs-comment">// What happens?</span>
c := Counter{count: <span class="hljs-number">0</span>}
c.Increment()  <span class="hljs-comment">// count is now 1</span>
c.Increment()  <span class="hljs-comment">// count is now 2</span>
fmt.Println(c.GetCount())  <span class="hljs-comment">// 2</span>

<span class="hljs-comment">// Go is smart - you can call pointer methods on values</span>
c.Increment()  <span class="hljs-comment">// Go automatically does (&amp;c).Increment()</span>
</code></pre>
<p><strong>When to use pointer vs value receivers:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Use pointer receiver (<code>*T</code>)</td><td>Use value receiver (<code>T</code>)</td></tr>
</thead>
<tbody>
<tr>
<td>Method modifies the struct</td><td>Method only reads</td></tr>
<tr>
<td>Large struct (avoid copying)</td><td>Small struct (a few fields)</td></tr>
<tr>
<td>Consistency (if any method uses <code>*T</code>, use it for all)</td><td>Immutable behavior wanted</td></tr>
</tbody>
</table>
</div><p><strong>Go vs other languages:</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// Java/JavaScript/Python: methods are INSIDE the class</span>
class Person {
    private <span class="hljs-keyword">int</span> age;

    void celebrate() {  <span class="hljs-comment">// Method defined INSIDE class</span>
        age++;
    }
}

<span class="hljs-comment">// Go: methods are OUTSIDE the struct</span>
<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    age <span class="hljs-keyword">int</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p *Person)</span> <span class="hljs-title">celebrate</span><span class="hljs-params">()</span></span> {  <span class="hljs-comment">// Method defined OUTSIDE struct</span>
    p.age++
}
</code></pre>
<p><strong>Why Go does this:</strong></p>
<ul>
<li><strong>Separation of data and behavior</strong>: Struct defines data, methods define behavior</li>
<li><strong>Methods on any type</strong>: You can add methods to ANY type, even primitives:</li>
</ul>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> MyInt <span class="hljs-keyword">int</span>

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(m MyInt)</span> <span class="hljs-title">IsEven</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> {
    <span class="hljs-keyword">return</span> m%<span class="hljs-number">2</span> == <span class="hljs-number">0</span>
}

<span class="hljs-keyword">var</span> x MyInt = <span class="hljs-number">42</span>
fmt.Println(x.IsEven())  <span class="hljs-comment">// true</span>
</code></pre>
<ul>
<li><strong>No hidden coupling</strong>: All behavior is explicitly defined outside the struct</li>
<li><strong>Interfaces are satisfied implicitly</strong>: No need to declare "implements"</li>
</ul>
<p><strong>Interfaces in Go:</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// Define an interface</span>
<span class="hljs-keyword">type</span> Greeter <span class="hljs-keyword">interface</span> {
    Greet() <span class="hljs-keyword">string</span>
}

<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    Name <span class="hljs-keyword">string</span>
}

<span class="hljs-comment">// Person automatically implements Greeter if it has a Greet() method</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">Greet</span><span class="hljs-params">()</span> <span class="hljs-title">string</span></span> {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello, I'm "</span> + p.Name
}

<span class="hljs-comment">// Any type with Greet() satisfies the interface</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">SayHello</span><span class="hljs-params">(g Greeter)</span></span> {
    fmt.Println(g.Greet())
}

p := Person{Name: <span class="hljs-string">"Alice"</span>}
SayHello(p)  <span class="hljs-comment">// Works! Person implements Greeter implicitly</span>
</code></pre>
<p><strong>In Java/Python</strong>, you must explicitly declare <code>implements</code> or inherit. <strong>In Go</strong>, if your type has the right methods, it implements the interface - no declaration needed.</p>
<h3 id="heading-naming-conventions-for-visibility">Naming conventions for visibility</h3>
<p><strong>Java:</strong></p>
<ul>
<li>All access via keywords</li>
<li>Naming convention: <code>camelCase</code> for methods/fields, <code>PascalCase</code> for classes</li>
</ul>
<p><strong>JavaScript:</strong></p>
<ul>
<li><code>#</code> for private (modern)</li>
<li>Convention: <code>_privateField</code> (older code)</li>
<li>Naming: <code>camelCase</code> for everything</li>
</ul>
<p><strong>Python:</strong></p>
<ul>
<li><code>public_method()</code> - no prefix</li>
<li><code>_internal_method()</code> - single underscore (don't use outside)</li>
<li><code>__private_method()</code> - double underscore (name mangling)</li>
<li>Naming: <code>snake_case</code> for everything</li>
</ul>
<p><strong>Go:</strong></p>
<ul>
<li><code>ExportedFunction()</code> - uppercase</li>
<li><code>notExported()</code> - lowercase</li>
<li>Naming: <code>MixedCaps</code> (no underscores!)</li>
</ul>
<hr />
<h2 id="heading-packages-and-imports">Packages and Imports</h2>
<h3 id="heading-how-imports-work">How imports work</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Import statement</strong></td><td><code>import com.example.MyClass;</code></td><td><code>import { myFunc } from './module';</code></td><td><code>import mymodule</code></td><td><code>import "github.com/user/package"</code></td></tr>
<tr>
<td><strong>Import everything</strong></td><td><code>import com.example.*;</code></td><td><code>import * as lib from './module';</code></td><td><code>from mymodule import *</code></td><td>Automatic (all exported symbols)</td></tr>
<tr>
<td><strong>Alias</strong></td><td>N/A (use full name)</td><td><code>import { old as new }</code></td><td><code>import module as m</code></td><td><code>import m "package"</code></td></tr>
<tr>
<td><strong>Package declaration</strong></td><td><code>package com.example;</code></td><td><code>export</code> keyword</td><td>N/A (file/directory based)</td><td><code>package mypackage</code></td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-5">Why they're different</h3>
<p><strong>Java - Package structure mirrors directory structure</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// File: src/com/example/myapp/Person.java</span>
<span class="hljs-keyword">package</span> com.example.myapp;  <span class="hljs-comment">// Must match directory structure!</span>

<span class="hljs-keyword">import</span> java.util.List;
<span class="hljs-keyword">import</span> java.util.ArrayList;
<span class="hljs-comment">// Or: import java.util.*;</span>

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">private</span> String name;
}
</code></pre>
<p><strong>Directory structure:</strong></p>
<pre><code>src/
  com/
    example/
      myapp/
        Person.java
        Main.java
</code></pre><p><strong>Importing:</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// File: src/com/example/myapp/Main.java</span>
<span class="hljs-keyword">package</span> com.example.myapp;

<span class="hljs-keyword">import</span> com.example.myapp.Person;  <span class="hljs-comment">// Import from same package (optional)</span>
<span class="hljs-keyword">import</span> com.example.other.Helper;   <span class="hljs-comment">// Import from different package</span>

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Person p = <span class="hljs-keyword">new</span> Person();
    }
}
</code></pre>
<p><strong>Key points:</strong></p>
<ul>
<li>Package name MUST match directory structure</li>
<li>One public class per file (class name = file name)</li>
<li>Classes in same package can access package-private members</li>
<li>Use reverse domain naming: <code>com.company.project</code></li>
</ul>
<p><strong>JavaScript - Module-based (ES6)</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// File: person.js</span>
<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">constructor</span>(name) {
        <span class="hljs-built_in">this</span>.name = name;
    }
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createPerson</span>(<span class="hljs-params">name</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Person(name);
}

<span class="hljs-comment">// Default export</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Person;
</code></pre>
<p><strong>Importing:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// File: main.js</span>

<span class="hljs-comment">// Named imports</span>
<span class="hljs-keyword">import</span> { Person, createPerson } <span class="hljs-keyword">from</span> <span class="hljs-string">'./person.js'</span>;

<span class="hljs-comment">// Default import</span>
<span class="hljs-keyword">import</span> Person <span class="hljs-keyword">from</span> <span class="hljs-string">'./person.js'</span>;

<span class="hljs-comment">// Import everything</span>
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> PersonModule <span class="hljs-keyword">from</span> <span class="hljs-string">'./person.js'</span>;

<span class="hljs-comment">// Alias</span>
<span class="hljs-keyword">import</span> { createPerson <span class="hljs-keyword">as</span> makePerson } <span class="hljs-keyword">from</span> <span class="hljs-string">'./person.js'</span>;

<span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Alice"</span>);
</code></pre>
<p><strong>Directory structure:</strong></p>
<pre><code>project/
  src/
    person.js
    main.js
    utils/
      helper.js
</code></pre><p><strong>Key points:</strong></p>
<ul>
<li>No strict directory requirements</li>
<li>Relative paths (<code>./</code>, <code>../</code>) or package names (<code>lodash</code>)</li>
<li>Can mix named and default exports</li>
<li>CommonJS (<code>require()</code>) still common in Node.js</li>
</ul>
<p><strong>Python - Module = file, Package = directory with <code>__init__.py</code></strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># File: myapp/person.py</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        self.name = name

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_person</span>(<span class="hljs-params">name</span>):</span>
    <span class="hljs-keyword">return</span> Person(name)
</code></pre>
<p><strong>Importing:</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># File: main.py</span>

<span class="hljs-comment"># Import module</span>
<span class="hljs-keyword">import</span> myapp.person
p = myapp.person.Person(<span class="hljs-string">"Alice"</span>)

<span class="hljs-comment"># Import specific items</span>
<span class="hljs-keyword">from</span> myapp.person <span class="hljs-keyword">import</span> Person, create_person
p = Person(<span class="hljs-string">"Alice"</span>)

<span class="hljs-comment"># Import with alias</span>
<span class="hljs-keyword">import</span> myapp.person <span class="hljs-keyword">as</span> person_module
<span class="hljs-keyword">from</span> myapp <span class="hljs-keyword">import</span> person <span class="hljs-keyword">as</span> pm

<span class="hljs-comment"># Import everything (avoid!)</span>
<span class="hljs-keyword">from</span> myapp.person <span class="hljs-keyword">import</span> *
</code></pre>
<p><strong>Directory structure:</strong></p>
<pre><code>project/
  myapp/
    __init__.py      # Makes myapp a package (can be empty)
    person.py
    utils/
      __init__.py
      helper.py
  main.py
</code></pre><p><strong>Key points:</strong></p>
<ul>
<li><code>__init__.py</code> makes a directory a package (can be empty)</li>
<li>Module name = file name (without <code>.py</code>)</li>
<li>Python looks in <code>sys.path</code> for modules</li>
<li>Relative imports: <code>from . import module</code> (same package)</li>
</ul>
<p><strong>Go - Package per directory, import by path</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// File: myapp/person.go</span>
<span class="hljs-keyword">package</span> myapp  <span class="hljs-comment">// Package name (doesn't have to match directory!)</span>

<span class="hljs-comment">// Exported (uppercase)</span>
<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    Name <span class="hljs-keyword">string</span>
}

<span class="hljs-comment">// Not exported (lowercase)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">helper</span><span class="hljs-params">()</span></span> {
    <span class="hljs-comment">// ...</span>
}

<span class="hljs-comment">// Exported function</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">NewPerson</span><span class="hljs-params">(name <span class="hljs-keyword">string</span>)</span> *<span class="hljs-title">Person</span></span> {
    <span class="hljs-keyword">return</span> &amp;Person{Name: name}
}
</code></pre>
<p><strong>Importing:</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// File: main.go</span>
<span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
    <span class="hljs-string">"fmt"</span>                           <span class="hljs-comment">// Standard library</span>
    <span class="hljs-string">"github.com/user/project/myapp"</span> <span class="hljs-comment">// Your package</span>
    m <span class="hljs-string">"github.com/user/other"</span>       <span class="hljs-comment">// Alias</span>
)

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
    p := myapp.NewPerson(<span class="hljs-string">"Alice"</span>)
    fmt.Println(p.Name)
}
</code></pre>
<p><strong>Directory structure:</strong></p>
<pre><code>project/
  go.mod                    # Module definition
  main.go
  myapp/
    person.go
    helper.go               # Same package!
  utils/
    tools.go
</code></pre><p><strong>Key points:</strong></p>
<ul>
<li><strong>All files in same directory = same package</strong> (must have same <code>package</code> declaration)</li>
<li><strong>One package per directory</strong> (directory can have any name)</li>
<li>Import path is the directory path, not the package name</li>
<li>Exported = starts with uppercase, not exported = lowercase</li>
<li>No circular dependencies allowed!</li>
</ul>
<h3 id="heading-the-go-difference-visibility-and-package-structure">The Go difference: Visibility and package structure</h3>
<p><strong>Go's unique rules:</strong></p>
<ol>
<li><strong>Visibility by capitalization:</strong>
```go
type Person struct {
 Name string   // Exported (public) - uppercase
 age  int      // Not exported (private) - lowercase
}</li>
</ol>
<p>func NewPerson() *Person { }  // Exported
func helper() { }             // Not exported</p>
<pre><code>
<span class="hljs-number">2.</span> **All files <span class="hljs-keyword">in</span> directory share package:**
<span class="hljs-string">``</span><span class="hljs-string">`go
// File: myapp/person.go
package myapp

type Person struct { }

// File: myapp/team.go
package myapp  // MUST be same package!

type Team struct {
    Members []Person  // Can use Person directly
}</span>
</code></pre><ol start="3">
<li><strong>Import by path, use by package name:</strong>
```go
import "github.com/user/project/myapp"  // Import PATH</li>
</ol>
<p>p := myapp.NewPerson()  // Use package NAME</p>
<pre><code>
<span class="hljs-number">4.</span> **No circular imports:**
<span class="hljs-string">``</span><span class="hljs-string">`go
// This is NOT allowed:
// package a imports package b
// package b imports package a
// Go will refuse to compile!</span>
</code></pre><h3 id="heading-organizing-code">Organizing code</h3>
<p><strong>Java:</strong></p>
<pre><code>src/main/java/
  com/
    company/
      project/
        domain/
          User.java
          Order.java
        service/
          UserService.java
        repository/
          UserRepository.java
</code></pre><p><strong>JavaScript:</strong></p>
<pre><code>src/
  components/
    User.js
    Order.js
  services/
    userService.js
  utils/
    helpers.js
</code></pre><p><strong>Python:</strong></p>
<pre><code>myproject/
  domain/
    __init__.py
    user.py
    order.py
  services/
    __init__.py
    user_service.py
  utils/
    __init__.py
    helpers.py
</code></pre><p><strong>Go:</strong></p>
<pre><code>myproject/
  go.mod
  cmd/
    myapp/
      main.go
  internal/           # Only importable by <span class="hljs-built_in">this</span> project
    domain/
      user.go
      order.go
    service/
      user.go
  pkg/                # Can be imported by others
    helpers/
      util.go
</code></pre><p><strong>Go conventions:</strong></p>
<ul>
<li><code>cmd/</code> - Application entry points</li>
<li><code>internal/</code> - Private code (Go enforces this!)</li>
<li><code>pkg/</code> - Public libraries</li>
<li>Flat structure preferred (avoid deep nesting)</li>
</ul>
<hr />
<h2 id="heading-access-control-and-visibility">Access Control and Visibility</h2>
<h3 id="heading-how-visibility-works">How visibility works</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Level</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Public (accessible everywhere)</strong></td><td><code>public</code> keyword</td><td>All exports</td><td>Everything by default</td><td>Uppercase name</td></tr>
<tr>
<td><strong>Private (same class/package only)</strong></td><td><code>private</code> keyword</td><td><code>#</code> prefix (ES2022)</td><td><code>_</code> prefix (convention)</td><td>Lowercase name</td></tr>
<tr>
<td><strong>Protected (subclasses)</strong></td><td><code>protected</code> keyword</td><td>N/A</td><td>N/A</td><td>N/A</td></tr>
<tr>
<td><strong>Package-private</strong></td><td>No modifier (default)</td><td>N/A</td><td>N/A</td><td>Lowercase (same package)</td></tr>
<tr>
<td><strong>Module-private</strong></td><td>N/A</td><td>Not exported</td><td><code>_</code> prefix</td><td>N/A</td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-6">Why they're different</h3>
<p><strong>Java - Explicit keywords</strong></p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">public</span> String name;           <span class="hljs-comment">// Anyone can access</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;              <span class="hljs-comment">// Only this class</span>
    <span class="hljs-keyword">protected</span> String email;       <span class="hljs-comment">// This class + subclasses</span>
    String address;               <span class="hljs-comment">// Package-private (default)</span>

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> name;
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">helper</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Only callable within Person class</span>
    }
}

<span class="hljs-comment">// In same package - can access package-private</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Helper</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">doSomething</span><span class="hljs-params">(Person p)</span> </span>{
        p.address = <span class="hljs-string">"123 Main"</span>;  <span class="hljs-comment">// OK - package-private</span>
        <span class="hljs-comment">// p.age = 30;  // Error - private</span>
    }
}
</code></pre>
<p><strong>JavaScript - Limited visibility</strong></p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-comment">// Public by default</span>
    name;

    <span class="hljs-comment">// Private field (ES2022)</span>
    #age;

    <span class="hljs-comment">// Private method (ES2022)</span>
    #helper() {
        <span class="hljs-comment">// Only callable within Person class</span>
    }

    <span class="hljs-keyword">constructor</span>(name, age) {
        <span class="hljs-built_in">this</span>.name = name;
        <span class="hljs-built_in">this</span>.#age = age;
    }

    getAge() {
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.#age;  <span class="hljs-comment">// Can access private within class</span>
    }
}

<span class="hljs-keyword">const</span> p = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>);
<span class="hljs-built_in">console</span>.log(p.name);  <span class="hljs-comment">// OK - public</span>
<span class="hljs-comment">// console.log(p.#age);  // Error - private</span>

<span class="hljs-comment">// Older pattern - closure for privacy</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createPerson</span>(<span class="hljs-params">name, age</span>) </span>{
    <span class="hljs-comment">// Private variable (closure)</span>
    <span class="hljs-keyword">let</span> privateAge = age;

    <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">name</span>: name,  <span class="hljs-comment">// Public</span>
        getAge() {
            <span class="hljs-keyword">return</span> privateAge;  <span class="hljs-comment">// Can access private</span>
        }
    };
}
</code></pre>
<p><strong>Python - Convention-based (no enforcement)</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, age</span>):</span>
        self.name = name          <span class="hljs-comment"># Public (by convention)</span>
        self._age = age           <span class="hljs-comment"># "Private" by convention (single _)</span>
        self.__secret = <span class="hljs-string">"shh"</span>     <span class="hljs-comment"># Name mangling (double __)</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_age</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self._age

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_helper</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-comment"># "Private" method by convention</span>
        <span class="hljs-keyword">pass</span>

<span class="hljs-comment"># Python doesn't enforce privacy!</span>
p = Person(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>)
print(p.name)     <span class="hljs-comment"># OK</span>
print(p._age)     <span class="hljs-comment"># "Should not" but can access</span>
print(p._Person__secret)  <span class="hljs-comment"># Name mangling - can still access!</span>
</code></pre>
<p><strong>Python conventions:</strong></p>
<ul>
<li>No <code>_</code> prefix: Public</li>
<li>Single <code>_</code> prefix: "Internal" (don't use outside, but Python won't stop you)</li>
<li>Double <code>__</code> prefix: Name mangling (becomes <code>_ClassName__attribute</code>)</li>
</ul>
<p><strong>Python philosophy:</strong> "We're all consenting adults here" - no enforcement, just conventions.</p>
<p><strong>Go - Capitalization determines visibility</strong></p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> myapp

<span class="hljs-comment">// Exported (public) - starts with uppercase</span>
<span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    Name  <span class="hljs-keyword">string</span>  <span class="hljs-comment">// Exported field</span>
    Email <span class="hljs-keyword">string</span>  <span class="hljs-comment">// Exported field</span>
    age   <span class="hljs-keyword">int</span>     <span class="hljs-comment">// Not exported (private to package)</span>
}

<span class="hljs-comment">// Exported function</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">NewPerson</span><span class="hljs-params">(name <span class="hljs-keyword">string</span>, age <span class="hljs-keyword">int</span>)</span> *<span class="hljs-title">Person</span></span> {
    <span class="hljs-keyword">return</span> &amp;Person{Name: name, age: age}
}

<span class="hljs-comment">// Not exported (private to package)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">helper</span><span class="hljs-params">()</span></span> {
    <span class="hljs-comment">// ...</span>
}

<span class="hljs-comment">// Exported method</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p *Person)</span> <span class="hljs-title">GetAge</span><span class="hljs-params">()</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">return</span> p.age  <span class="hljs-comment">// Can access private field in same package</span>
}

<span class="hljs-comment">// Not exported method</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p *Person)</span> <span class="hljs-title">validate</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> {
    <span class="hljs-keyword">return</span> p.age &gt; <span class="hljs-number">0</span>
}
</code></pre>
<p><strong>From another package:</strong></p>
<pre><code class="lang-go"><span class="hljs-keyword">import</span> <span class="hljs-string">"myproject/myapp"</span>

p := myapp.NewPerson(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">30</span>)  <span class="hljs-comment">// OK - exported</span>
fmt.Println(p.Name)                <span class="hljs-comment">// OK - exported field</span>
<span class="hljs-comment">// fmt.Println(p.age)              // Error - not exported</span>
age := p.GetAge()                  <span class="hljs-comment">// OK - exported method</span>
<span class="hljs-comment">// p.validate()                    // Error - not exported</span>
</code></pre>
<p><strong>Go has only two levels:</strong></p>
<ol>
<li><strong>Exported</strong> (uppercase) - accessible everywhere</li>
<li><strong>Not exported</strong> (lowercase) - only within the same package</li>
</ol>
<p><strong>Special: <code>internal/</code> directory</strong></p>
<pre><code>myproject/
  internal/
    utils/
      helper.go  <span class="hljs-comment">// Only importable by myproject</span>
</code></pre><p>Go enforces that packages under <code>internal/</code> can only be imported by the parent project.</p>
<h3 id="heading-comparison-table">Comparison table</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Public syntax</strong></td><td><code>public</code></td><td>Default or <code>export</code></td><td>Default</td><td>Uppercase</td></tr>
<tr>
<td><strong>Private syntax</strong></td><td><code>private</code></td><td><code>#field</code></td><td><code>_field</code> (convention)</td><td>lowercase</td></tr>
<tr>
<td><strong>Enforcement</strong></td><td>Compile-time</td><td>Compile-time (for <code>#</code>)</td><td>None (convention only)</td><td>Compile-time</td></tr>
<tr>
<td><strong>Package-private</strong></td><td>Default (no modifier)</td><td>No concept</td><td>No concept</td><td>lowercase</td></tr>
<tr>
<td><strong>Protected</strong></td><td><code>protected</code></td><td>No</td><td>No</td><td>No</td></tr>
<tr>
<td><strong>Levels</strong></td><td>4 (public, protected, package, private)</td><td>2 (public, private)</td><td>1 (all public, convention)</td><td>2 (exported, not exported)</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-concurrency-async-threads-and-channels">Concurrency: Async, Threads, and Channels</h2>
<h3 id="heading-how-concurrency-works">How concurrency works</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>What</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Concurrency model</strong></td><td>Threads (OS threads)</td><td>Event loop (single-threaded)</td><td>Threading + GIL, asyncio</td><td>Goroutines (lightweight threads)</td></tr>
<tr>
<td><strong>Basic unit</strong></td><td><code>Thread</code></td><td>Promise/async function</td><td><code>threading.Thread</code></td><td><code>goroutine</code></td></tr>
<tr>
<td><strong>Async syntax</strong></td><td><code>CompletableFuture</code></td><td><code>async/await</code></td><td><code>async/await</code></td><td>Native (just <code>go</code>)</td></tr>
<tr>
<td><strong>Communication</strong></td><td>Shared memory + locks</td><td>Callbacks/Promises</td><td>Queues, shared memory</td><td>Channels</td></tr>
<tr>
<td><strong>Cost per unit</strong></td><td>~1MB stack (heavy)</td><td>Very light</td><td>~1MB stack (heavy)</td><td>~2KB stack (very light)</td></tr>
</tbody>
</table>
</div><h3 id="heading-why-theyre-different-7">Why they're different</h3>
<p><strong>Java - Traditional threads with virtual threads (Java 21+)</strong></p>
<p>Java has OS threads (expensive) and now virtual threads (lightweight):</p>
<pre><code class="lang-java"><span class="hljs-comment">// Traditional threads (heavyweight)</span>
Thread thread = <span class="hljs-keyword">new</span> Thread(() -&gt; {
    System.out.println(<span class="hljs-string">"Running in thread"</span>);
});
thread.start();
thread.join();  <span class="hljs-comment">// Wait for completion</span>

<span class="hljs-comment">// ExecutorService for thread pools</span>
ExecutorService executor = Executors.newFixedThreadPool(<span class="hljs-number">10</span>);
executor.submit(() -&gt; {
    <span class="hljs-comment">// Do work</span>
});
executor.shutdown();

<span class="hljs-comment">// CompletableFuture for async operations</span>
CompletableFuture.supplyAsync(() -&gt; {
    <span class="hljs-keyword">return</span> fetchData();
}).thenApply(data -&gt; {
    <span class="hljs-keyword">return</span> processData(data);
}).thenAccept(result -&gt; {
    System.out.println(result);
});

<span class="hljs-comment">// Virtual Threads (Java 21+) - lightweight!</span>
Thread.startVirtualThread(() -&gt; {
    System.out.println(<span class="hljs-string">"Running in virtual thread"</span>);
});

<span class="hljs-comment">// Or with ExecutorService</span>
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
executor.submit(() -&gt; {
    <span class="hljs-comment">// Each task gets its own virtual thread (cheap!)</span>
});
</code></pre>
<p><strong>Why Java has virtual threads:</strong></p>
<ul>
<li>Traditional threads are OS threads (expensive, ~1MB each)</li>
<li>Can't create millions of threads</li>
<li>Virtual threads are lightweight (like Go goroutines)</li>
<li>Managed by JVM, not OS</li>
<li>Can create millions of them</li>
<li>Makes writing concurrent code easier (no callbacks!)</li>
</ul>
<p><strong>JavaScript - Event loop with async/await</strong></p>
<p>JavaScript is single-threaded with an event loop:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Promises</span>
fetch(<span class="hljs-string">'https://api.example.com/data'</span>)
    .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> response.json())
    .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data))
    .catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">console</span>.error(error));

<span class="hljs-comment">// Async/await (modern way)</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fetchData</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">'https://api.example.com/data'</span>);
        <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.json();
        <span class="hljs-built_in">console</span>.log(data);
    } <span class="hljs-keyword">catch</span> (error) {
        <span class="hljs-built_in">console</span>.error(error);
    }
}

<span class="hljs-comment">// Multiple concurrent operations</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fetchMultiple</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// Run in parallel</span>
    <span class="hljs-keyword">const</span> [users, posts] = <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.all([
        fetch(<span class="hljs-string">'/api/users'</span>).then(<span class="hljs-function"><span class="hljs-params">r</span> =&gt;</span> r.json()),
        fetch(<span class="hljs-string">'/api/posts'</span>).then(<span class="hljs-function"><span class="hljs-params">r</span> =&gt;</span> r.json())
    ]);

    <span class="hljs-keyword">return</span> { users, posts };
}

<span class="hljs-comment">// setTimeout for delayed execution</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"After 1 second"</span>);
}, <span class="hljs-number">1000</span>);
</code></pre>
<p><strong>Why JavaScript is single-threaded:</strong></p>
<ul>
<li>Originally designed for browsers (DOM manipulation isn't thread-safe)</li>
<li>Event loop handles I/O concurrency</li>
<li>No threads = no race conditions, no locks</li>
<li>Great for I/O-heavy tasks (servers, APIs)</li>
<li>Poor for CPU-heavy tasks (can't use multiple cores)</li>
</ul>
<p><strong>Python - Threading with GIL, asyncio for async</strong></p>
<p>Python has two models:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Threading (limited by GIL - Global Interpreter Lock)</span>
<span class="hljs-keyword">import</span> threading

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">worker</span>():</span>
    print(<span class="hljs-string">"Running in thread"</span>)

thread = threading.Thread(target=worker)
thread.start()
thread.join()  <span class="hljs-comment"># Wait for completion</span>

<span class="hljs-comment"># Multiple threads</span>
threads = []
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()

<span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> threads:
    t.join()

<span class="hljs-comment"># Asyncio (for I/O-bound tasks)</span>
<span class="hljs-keyword">import</span> asyncio

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fetch_data</span>():</span>
    <span class="hljs-keyword">await</span> asyncio.sleep(<span class="hljs-number">1</span>)  <span class="hljs-comment"># Simulated I/O</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Data"</span>

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span>
    <span class="hljs-comment"># Run concurrently</span>
    results = <span class="hljs-keyword">await</span> asyncio.gather(
        fetch_data(),
        fetch_data(),
        fetch_data()
    )
    print(results)

asyncio.run(main())

<span class="hljs-comment"># Multiprocessing (for CPU-bound tasks, bypasses GIL)</span>
<span class="hljs-keyword">from</span> multiprocessing <span class="hljs-keyword">import</span> Process

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cpu_work</span>():</span>
    <span class="hljs-comment"># Heavy computation</span>
    <span class="hljs-keyword">pass</span>

p = Process(target=cpu_work)
p.start()
p.join()
</code></pre>
<p><strong>The GIL problem:</strong></p>
<ul>
<li><strong>GIL</strong> (Global Interpreter Lock) = only one thread executes Python bytecode at a time</li>
<li>Threading is good for I/O-bound tasks (waiting on network, disk)</li>
<li>Threading is BAD for CPU-bound tasks (can't use multiple cores)</li>
<li>Use <code>multiprocessing</code> for CPU-bound tasks (separate processes, no GIL)</li>
<li>Use <code>asyncio</code> for I/O-bound tasks (efficient, single-threaded)</li>
</ul>
<p><strong>Go - Goroutines and channels</strong></p>
<p>Go has built-in lightweight concurrency:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Goroutine - just add 'go'</span>
<span class="hljs-keyword">go</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">()</span></span> {
    fmt.Println(<span class="hljs-string">"Running in goroutine"</span>)
}()

<span class="hljs-comment">// Wait for goroutines with WaitGroup</span>
<span class="hljs-keyword">var</span> wg sync.WaitGroup

<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++ {
    wg.Add(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">go</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(id <span class="hljs-keyword">int</span>)</span></span> {
        <span class="hljs-keyword">defer</span> wg.Done()
        fmt.Printf(<span class="hljs-string">"Goroutine %d\n"</span>, id)
    }(i)
}

wg.Wait()  <span class="hljs-comment">// Wait for all goroutines</span>

<span class="hljs-comment">// Channels for communication</span>
ch := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">string</span>)

<span class="hljs-comment">// Send to channel (in goroutine)</span>
<span class="hljs-keyword">go</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">()</span></span> {
    ch &lt;- <span class="hljs-string">"Hello from goroutine"</span>
}()

<span class="hljs-comment">// Receive from channel</span>
msg := &lt;-ch
fmt.Println(msg)

<span class="hljs-comment">// Buffered channels</span>
ch := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>, <span class="hljs-number">3</span>)  <span class="hljs-comment">// Can hold 3 values</span>
ch &lt;- <span class="hljs-number">1</span>
ch &lt;- <span class="hljs-number">2</span>
ch &lt;- <span class="hljs-number">3</span>
<span class="hljs-comment">// ch &lt;- 4  // Would block until someone reads</span>

<span class="hljs-comment">// Select for multiple channels</span>
<span class="hljs-keyword">select</span> {
<span class="hljs-keyword">case</span> msg := &lt;-ch1:
    fmt.Println(<span class="hljs-string">"From ch1:"</span>, msg)
<span class="hljs-keyword">case</span> msg := &lt;-ch2:
    fmt.Println(<span class="hljs-string">"From ch2:"</span>, msg)
<span class="hljs-keyword">case</span> &lt;-time.After(<span class="hljs-number">1</span> * time.Second):
    fmt.Println(<span class="hljs-string">"Timeout"</span>)
}

<span class="hljs-comment">// Worker pool pattern</span>
jobs := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>, <span class="hljs-number">100</span>)
results := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>, <span class="hljs-number">100</span>)

<span class="hljs-comment">// Start workers</span>
<span class="hljs-keyword">for</span> w := <span class="hljs-number">0</span>; w &lt; <span class="hljs-number">3</span>; w++ {
    <span class="hljs-keyword">go</span> worker(jobs, results)
}

<span class="hljs-comment">// Send jobs</span>
<span class="hljs-keyword">for</span> j := <span class="hljs-number">0</span>; j &lt; <span class="hljs-number">10</span>; j++ {
    jobs &lt;- j
}
<span class="hljs-built_in">close</span>(jobs)

<span class="hljs-comment">// Collect results</span>
<span class="hljs-keyword">for</span> r := <span class="hljs-number">0</span>; r &lt; <span class="hljs-number">10</span>; r++ {
    &lt;-results
}
</code></pre>
<p><strong>Why Go uses goroutines and channels:</strong></p>
<ul>
<li><strong>Goroutines are cheap</strong>: ~2KB stack vs ~1MB for OS threads</li>
<li>Can create millions of them</li>
<li><strong>Channels</strong>: "Don't communicate by sharing memory; share memory by communicating"</li>
<li>Type-safe communication between goroutines</li>
<li>Prevents many race conditions</li>
<li><code>select</code> statement for multiplexing channels</li>
</ul>
<p><strong>Go's scheduler:</strong></p>
<ul>
<li>Go runtime multiplexes goroutines onto OS threads (M:N scheduling)</li>
<li>Automatically uses all CPU cores</li>
<li>Built into the language (not a library)</li>
</ul>
<h3 id="heading-comparison">Comparison</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>True parallelism</strong></td><td>Yes (threads)</td><td>No (single-threaded)</td><td>No (GIL limits)</td><td>Yes (goroutines)</td></tr>
<tr>
<td><strong>Lightweight</strong></td><td>Virtual threads (Java 21+)</td><td>Very (event loop)</td><td>No (threads heavy)</td><td>Yes (goroutines)</td></tr>
<tr>
<td><strong>CPU-bound tasks</strong></td><td>Good</td><td>Poor</td><td>Use multiprocessing</td><td>Excellent</td></tr>
<tr>
<td><strong>I/O-bound tasks</strong></td><td>Good</td><td>Excellent</td><td>asyncio good</td><td>Excellent</td></tr>
<tr>
<td><strong>Learning curve</strong></td><td>Medium</td><td>Easy (async/await)</td><td>Medium (GIL confusion)</td><td>Medium (channels)</td></tr>
<tr>
<td><strong>Max concurrent units</strong></td><td>~Thousands (OS threads), millions (virtual threads)</td><td>Unlimited (event loop)</td><td>Limited (GIL)</td><td>Millions (goroutines)</td></tr>
</tbody>
</table>
</div><h3 id="heading-when-to-use-what">When to use what</h3>
<p><strong>Java:</strong></p>
<ul>
<li>Use virtual threads (Java 21+) for simple concurrent code</li>
<li>Use <code>CompletableFuture</code> for async operations</li>
<li>Use thread pools for controlled concurrency</li>
<li>Good for both CPU and I/O bound tasks</li>
</ul>
<p><strong>JavaScript:</strong></p>
<ul>
<li>Perfect for I/O-bound tasks (web servers, APIs)</li>
<li>Use <code>async/await</code> for everything</li>
<li>Don't use for CPU-heavy tasks (blocks the event loop)</li>
<li>Worker threads exist but rarely used</li>
</ul>
<p><strong>Python:</strong></p>
<ul>
<li>Use <code>asyncio</code> for I/O-bound tasks (network, disk)</li>
<li>Use <code>multiprocessing</code> for CPU-bound tasks (bypasses GIL)</li>
<li>Threading only useful for I/O-bound tasks</li>
<li>Avoid threading for CPU-bound work</li>
</ul>
<p><strong>Go:</strong></p>
<ul>
<li>Use goroutines for everything (cheap and simple)</li>
<li>Use channels for communication between goroutines</li>
<li>Excellent for both CPU and I/O bound tasks</li>
<li>Built-in concurrency is Go's main strength</li>
</ul>
<hr />
<h2 id="heading-error-handling">Error Handling</h2>
<p>Error handling is where Go takes a <strong>radically different approach</strong> from the other three languages. Java, JavaScript, and Python all use exceptions - errors that "bubble up" the call stack until caught. Go uses explicit error return values instead.</p>
<h3 id="heading-the-fundamental-difference">The fundamental difference</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Language</td><td>Approach</td><td>Philosophy</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Java</strong></td><td>Exceptions (checked &amp; unchecked)</td><td>Exceptions for exceptional cases, forces handling</td></tr>
<tr>
<td><strong>JavaScript</strong></td><td>Exceptions (unchecked only)</td><td>Exceptions everywhere, but easy to ignore</td></tr>
<tr>
<td><strong>Python</strong></td><td>Exceptions (unchecked only)</td><td>"Easier to ask forgiveness than permission" (EAFP)</td></tr>
<tr>
<td><strong>Go</strong></td><td>Error return values</td><td>Errors are values, handle explicitly at each step</td></tr>
</tbody>
</table>
</div><h3 id="heading-why-go-chose-this-approach">Why Go chose this approach</h3>
<p><strong>Java, JavaScript, and Python</strong>: Use exceptions that can skip multiple layers of your code until someone catches them. This is convenient but can make control flow hard to follow - an error could come from anywhere deep in the call stack.</p>
<p><strong>Go</strong>: Treats errors as normal return values. Every function that can fail returns an <code>error</code> as its last return value. You check it immediately, right where the error occurs.</p>
<pre><code class="lang-go"><span class="hljs-comment">// Go - explicit error checking at every step</span>
file, err := os.Open(<span class="hljs-string">"data.txt"</span>)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    <span class="hljs-keyword">return</span> fmt.Errorf(<span class="hljs-string">"failed to open file: %w"</span>, err)
}
<span class="hljs-keyword">defer</span> file.Close()

data, err := io.ReadAll(file)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    <span class="hljs-keyword">return</span> fmt.Errorf(<span class="hljs-string">"failed to read file: %w"</span>, err)
}
</code></pre>
<p>This is verbose, but it's <strong>very clear</strong> what can fail and where you're handling it.</p>
<h3 id="heading-the-exceptions-approach-java-js-python">The exceptions approach (Java, JS, Python)</h3>
<pre><code class="lang-java"><span class="hljs-comment">// Java - try/catch blocks</span>
<span class="hljs-keyword">try</span> {
    String content = Files.readString(Path.of(<span class="hljs-string">"data.txt"</span>));
    processData(content);
} <span class="hljs-keyword">catch</span> (IOException e) {
    System.err.println(<span class="hljs-string">"File error: "</span> + e.getMessage());
} <span class="hljs-keyword">catch</span> (ProcessException e) {
    System.err.println(<span class="hljs-string">"Processing error: "</span> + e.getMessage());
}
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">// JavaScript - try/catch with async/await</span>
<span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> content = <span class="hljs-keyword">await</span> fs.readFile(<span class="hljs-string">'data.txt'</span>, <span class="hljs-string">'utf8'</span>);
    <span class="hljs-keyword">await</span> processData(content);
} <span class="hljs-keyword">catch</span> (error) {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Error:'</span>, error.message);
}
</code></pre>
<pre><code class="lang-python"><span class="hljs-comment"># Python - try/except blocks</span>
<span class="hljs-keyword">try</span>:
    <span class="hljs-keyword">with</span> open(<span class="hljs-string">'data.txt'</span>, <span class="hljs-string">'r'</span>) <span class="hljs-keyword">as</span> file:
        content = file.read()
    process_data(content)
<span class="hljs-keyword">except</span> FileNotFoundError:
    print(<span class="hljs-string">"File not found"</span>)
<span class="hljs-keyword">except</span> ProcessError <span class="hljs-keyword">as</span> e:
    print(<span class="hljs-string">f"Processing error: <span class="hljs-subst">{e}</span>"</span>)
</code></pre>
<h3 id="heading-creating-and-handling-errors">Creating and handling errors</h3>
<p><strong>Java:</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// Checked exception - must be declared or caught</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">readFile</span><span class="hljs-params">(String path)</span> <span class="hljs-keyword">throws</span> IOException </span>{
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IOException(<span class="hljs-string">"File not found"</span>);
}

<span class="hljs-comment">// Unchecked exception - no declaration needed</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">validateInput</span><span class="hljs-params">(String input)</span> </span>{
    <span class="hljs-keyword">if</span> (input == <span class="hljs-keyword">null</span>) {
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"Input cannot be null"</span>);
    }
}

<span class="hljs-comment">// Custom exception</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ValidationException</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Exception</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ValidationException</span><span class="hljs-params">(String message)</span> </span>{
        <span class="hljs-keyword">super</span>(message);
    }
}
</code></pre>
<p><strong>JavaScript:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Throwing errors</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">validateAge</span>(<span class="hljs-params">age</span>) </span>{
    <span class="hljs-keyword">if</span> (age &lt; <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Age cannot be negative'</span>);
    }
}

<span class="hljs-comment">// Custom error class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ValidationError</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Error</span> </span>{
    <span class="hljs-keyword">constructor</span>(message) {
        <span class="hljs-built_in">super</span>(message);
        <span class="hljs-built_in">this</span>.name = <span class="hljs-string">'ValidationError'</span>;
    }
}

<span class="hljs-comment">// Promise rejection</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fetchData</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Network error'</span>);
}

fetchData().catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Caught:'</span>, error);
});
</code></pre>
<p><strong>Python:</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># Raising exceptions</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">validate_age</span>(<span class="hljs-params">age</span>):</span>
    <span class="hljs-keyword">if</span> age &lt; <span class="hljs-number">0</span>:
        <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">"Age cannot be negative"</span>)

<span class="hljs-comment"># Custom exception</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ValidationError</span>(<span class="hljs-params">Exception</span>):</span>
    <span class="hljs-keyword">pass</span>

<span class="hljs-comment"># Multiple exception handling</span>
<span class="hljs-keyword">try</span>:
    result = risky_operation()
<span class="hljs-keyword">except</span> ValueError <span class="hljs-keyword">as</span> e:
    print(<span class="hljs-string">f"Value error: <span class="hljs-subst">{e}</span>"</span>)
<span class="hljs-keyword">except</span> KeyError <span class="hljs-keyword">as</span> e:
    print(<span class="hljs-string">f"Key error: <span class="hljs-subst">{e}</span>"</span>)
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:  <span class="hljs-comment"># Catch-all</span>
    print(<span class="hljs-string">f"Unexpected error: <span class="hljs-subst">{e}</span>"</span>)
<span class="hljs-keyword">finally</span>:
    cleanup()  <span class="hljs-comment"># Always runs</span>
</code></pre>
<p><strong>Go:</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// Creating errors</span>
<span class="hljs-keyword">import</span> (
    <span class="hljs-string">"errors"</span>
    <span class="hljs-string">"fmt"</span>
)

<span class="hljs-comment">// Simple error</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">validateAge</span><span class="hljs-params">(age <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">error</span></span> {
    <span class="hljs-keyword">if</span> age &lt; <span class="hljs-number">0</span> {
        <span class="hljs-keyword">return</span> errors.New(<span class="hljs-string">"age cannot be negative"</span>)
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}

<span class="hljs-comment">// Formatted error</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">readConfig</span><span class="hljs-params">(path <span class="hljs-keyword">string</span>)</span> <span class="hljs-title">error</span></span> {
    <span class="hljs-keyword">return</span> fmt.Errorf(<span class="hljs-string">"failed to read config from %s"</span>, path)
}

<span class="hljs-comment">// Custom error type</span>
<span class="hljs-keyword">type</span> ValidationError <span class="hljs-keyword">struct</span> {
    Field <span class="hljs-keyword">string</span>
    Value <span class="hljs-keyword">interface</span>{}
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(e *ValidationError)</span> <span class="hljs-title">Error</span><span class="hljs-params">()</span> <span class="hljs-title">string</span></span> {
    <span class="hljs-keyword">return</span> fmt.Sprintf(<span class="hljs-string">"validation failed for %s: %v"</span>, e.Field, e.Value)
}

<span class="hljs-comment">// Wrapping errors (Go 1.13+)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">processFile</span><span class="hljs-params">(path <span class="hljs-keyword">string</span>)</span> <span class="hljs-title">error</span></span> {
    data, err := os.ReadFile(path)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-keyword">return</span> fmt.Errorf(<span class="hljs-string">"processing failed: %w"</span>, err)
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}

<span class="hljs-comment">// Checking wrapped errors</span>
err := processFile(<span class="hljs-string">"data.txt"</span>)
<span class="hljs-keyword">if</span> errors.Is(err, os.ErrNotExist) {
    fmt.Println(<span class="hljs-string">"File doesn't exist"</span>)
}
</code></pre>
<h3 id="heading-the-go-verbosity-problem">The Go verbosity problem</h3>
<p>Here's what frustrates many developers coming to Go:</p>
<pre><code class="lang-go"><span class="hljs-comment">// This pattern repeats EVERYWHERE in Go</span>
result1, err := step1()
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    <span class="hljs-keyword">return</span> err
}

result2, err := step2(result1)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    <span class="hljs-keyword">return</span> err
}

result3, err := step3(result2)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    <span class="hljs-keyword">return</span> err
}

<span class="hljs-keyword">return</span> result3, <span class="hljs-literal">nil</span>
</code></pre>
<p>Compare to Java:</p>
<pre><code class="lang-java"><span class="hljs-comment">// Clean and linear - exceptions bubble up automatically</span>
<span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">var</span> result1 = step1();
    <span class="hljs-keyword">var</span> result2 = step2(result1);
    <span class="hljs-keyword">var</span> result3 = step3(result2);
    <span class="hljs-keyword">return</span> result3;
} <span class="hljs-keyword">catch</span> (Exception e) {
    <span class="hljs-comment">// Handle all errors in one place</span>
    logger.error(<span class="hljs-string">"Operation failed"</span>, e);
    <span class="hljs-keyword">throw</span> e;
}
</code></pre>
<p><strong>The Go argument for this:</strong> You always know exactly where errors can occur and how they're handled. There's no hidden control flow.</p>
<p><strong>The counter-argument:</strong> It's extremely repetitive and makes code harder to read. The "happy path" gets lost in error checking.</p>
<h3 id="heading-error-handling-patterns">Error handling patterns</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Pattern</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Ignore errors</strong></td><td>Not possible for checked exceptions</td><td>Easy (don't catch)</td><td>Easy (don't catch)</td><td>Easy (<code>err</code> is just a value)</td></tr>
<tr>
<td><strong>Handle at call site</strong></td><td>try/catch around call</td><td>try/catch around call</td><td>try/except around call</td><td><code>if err != nil</code></td></tr>
<tr>
<td><strong>Bubble up</strong></td><td>Re-throw or declare <code>throws</code></td><td>Don't catch (automatic)</td><td>Don't catch (automatic)</td><td><code>return err</code> (explicit)</td></tr>
<tr>
<td><strong>Wrap with context</strong></td><td><code>throw new Exception("msg", cause)</code></td><td><code>throw new Error(msg)</code></td><td><code>raise NewError(msg) from e</code></td><td><code>fmt.Errorf("msg: %w", err)</code></td></tr>
<tr>
<td><strong>Multiple error types</strong></td><td>Multiple catch blocks</td><td>Single catch, check type</td><td>Multiple except blocks</td><td>Type assertion or <code>errors.As()</code></td></tr>
</tbody>
</table>
</div><h3 id="heading-panic-and-recover-in-go-the-exception-escape-hatch">Panic and recover in Go (the exception escape hatch)</h3>
<p>Go does have a panic/recover mechanism, but it's meant for <strong>truly exceptional situations</strong> (like out-of-bounds array access), not normal error handling:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Panic - like throwing an exception (but don't do this!)</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">riskyOperation</span><span class="hljs-params">()</span></span> {
    <span class="hljs-built_in">panic</span>(<span class="hljs-string">"something went terribly wrong"</span>)
}

<span class="hljs-comment">// Recover - like catching an exception</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">safeOperation</span><span class="hljs-params">()</span></span> {
    <span class="hljs-keyword">defer</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">()</span></span> {
        <span class="hljs-keyword">if</span> r := <span class="hljs-built_in">recover</span>(); r != <span class="hljs-literal">nil</span> {
            fmt.Println(<span class="hljs-string">"Recovered from panic:"</span>, r)
        }
    }()

    riskyOperation() <span class="hljs-comment">// This will panic</span>
    fmt.Println(<span class="hljs-string">"This won't execute"</span>)
}
</code></pre>
<p><strong>Go community consensus:</strong> Don't use panic/recover for normal error handling. Use error return values.</p>
<h3 id="heading-defer-gos-cleanup-mechanism">Defer: Go's cleanup mechanism</h3>
<p>Go has a unique feature called <code>defer</code> that schedules a function call to run when the surrounding function returns. It's primarily used for cleanup (closing files, releasing locks, etc.) and is executed <strong>even if the function panics</strong>.</p>
<p><strong>Is defer related to finally?</strong> Yes! Both solve the same problem: ensuring cleanup code runs no matter how a function exits. The key difference is that <code>defer</code> lets you write cleanup code RIGHT AFTER acquiring a resource, while <code>finally</code> requires cleanup code at the END of a try/catch block, separated from where you acquired the resource.</p>
<p><strong>Go:</strong></p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">readFile</span><span class="hljs-params">(path <span class="hljs-keyword">string</span>)</span> <span class="hljs-title">error</span></span> {
    file, err := os.Open(path)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-keyword">return</span> err
    }
    <span class="hljs-keyword">defer</span> file.Close()  <span class="hljs-comment">// Runs when function returns, no matter what</span>

    <span class="hljs-comment">// Do work with file...</span>
    <span class="hljs-comment">// Even if this code returns early or panics, file.Close() will run</span>
    data, err := io.ReadAll(file)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-keyword">return</span> err  <span class="hljs-comment">// defer will still execute before this return</span>
    }

    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>  <span class="hljs-comment">// defer executes here too</span>
}
</code></pre>
<p>The <code>defer</code> statement is evaluated immediately (the arguments are evaluated), but the function call is delayed until the surrounding function returns.</p>
<p><strong>Multiple defers execute in LIFO order (last in, first out):</strong></p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">example</span><span class="hljs-params">()</span></span> {
    <span class="hljs-keyword">defer</span> fmt.Println(<span class="hljs-string">"1"</span>)
    <span class="hljs-keyword">defer</span> fmt.Println(<span class="hljs-string">"2"</span>)
    <span class="hljs-keyword">defer</span> fmt.Println(<span class="hljs-string">"3"</span>)
    fmt.Println(<span class="hljs-string">"function body"</span>)
}
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// function body</span>
<span class="hljs-comment">// 3</span>
<span class="hljs-comment">// 2</span>
<span class="hljs-comment">// 1</span>
</code></pre>
<p>This no cmakes sense for cleanup: you want to clean up in reverse order of acquisition (close the last thing opened first).</p>
<h3 id="heading-how-other-languages-handle-cleanup">How other languages handle cleanup</h3>
<p><strong>Java - try-with-resources:</strong></p>
<pre><code class="lang-java"><span class="hljs-comment">// Automatic resource management (Java 7+)</span>
<span class="hljs-keyword">try</span> (BufferedReader reader = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> FileReader(<span class="hljs-string">"file.txt"</span>))) {
    String line = reader.readLine();
    <span class="hljs-comment">// reader.close() called automatically at end of try block</span>
} <span class="hljs-keyword">catch</span> (IOException e) {
    System.err.println(<span class="hljs-string">"Error: "</span> + e.getMessage());
}

<span class="hljs-comment">// Older approach - finally block</span>
BufferedReader reader = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">try</span> {
    reader = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> FileReader(<span class="hljs-string">"file.txt"</span>));
    String line = reader.readLine();
} <span class="hljs-keyword">catch</span> (IOException e) {
    System.err.println(<span class="hljs-string">"Error: "</span> + e.getMessage());
} <span class="hljs-keyword">finally</span> {
    <span class="hljs-comment">// Always executes, even if exception thrown</span>
    <span class="hljs-keyword">if</span> (reader != <span class="hljs-keyword">null</span>) {
        <span class="hljs-keyword">try</span> {
            reader.close();
        } <span class="hljs-keyword">catch</span> (IOException e) {
            <span class="hljs-comment">// Handle close error</span>
        }
    }
}
</code></pre>
<p><strong>JavaScript - finally and cleanup:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// finally block</span>
<span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> result = riskyOperation();
} <span class="hljs-keyword">catch</span> (error) {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Error:'</span>, error);
} <span class="hljs-keyword">finally</span> {
    <span class="hljs-comment">// Always runs, like defer</span>
    cleanup();
}

<span class="hljs-comment">// No automatic resource management in JavaScript</span>
<span class="hljs-comment">// You just have to remember to clean up</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">readFile</span>(<span class="hljs-params">path</span>) </span>{
    <span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> openFile(path);
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> file.read();
        <span class="hljs-keyword">return</span> data;
    } <span class="hljs-keyword">finally</span> {
        <span class="hljs-keyword">await</span> file.close();  <span class="hljs-comment">// Runs even if error thrown</span>
    }
}
</code></pre>
<p><strong>Python - context managers (with statement):</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># The 'with' statement handles cleanup automatically</span>
<span class="hljs-keyword">with</span> open(<span class="hljs-string">'file.txt'</span>, <span class="hljs-string">'r'</span>) <span class="hljs-keyword">as</span> file:
    data = file.read()
    <span class="hljs-comment"># file.close() called automatically when exiting the block</span>
    <span class="hljs-comment"># Even if exception is raised</span>

<span class="hljs-comment"># You can also use finally</span>
file = <span class="hljs-literal">None</span>
<span class="hljs-keyword">try</span>:
    file = open(<span class="hljs-string">'file.txt'</span>, <span class="hljs-string">'r'</span>)
    data = file.read()
<span class="hljs-keyword">finally</span>:
    <span class="hljs-keyword">if</span> file:
        file.close()  <span class="hljs-comment"># Always executes</span>

<span class="hljs-comment"># Custom context manager</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DatabaseConnection</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__enter__</span>(<span class="hljs-params">self</span>):</span>
        self.conn = create_connection()
        <span class="hljs-keyword">return</span> self.conn

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__exit__</span>(<span class="hljs-params">self, exc_type, exc_val, exc_tb</span>):</span>
        <span class="hljs-comment"># Called automatically when exiting 'with' block</span>
        self.conn.close()

<span class="hljs-keyword">with</span> DatabaseConnection() <span class="hljs-keyword">as</span> conn:
    conn.execute(<span class="hljs-string">"SELECT * FROM users"</span>)
</code></pre>
<h3 id="heading-comparison-of-cleanup-mechanisms">Comparison of cleanup mechanisms</h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Java</td><td>JavaScript</td><td>Python</td><td>Go</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Similar to defer?</strong></td><td><code>finally</code> block (less convenient)</td><td><code>finally</code> block (less convenient)</td><td><code>finally</code> block (less convenient)</td><td><code>defer</code> itself</td></tr>
<tr>
<td><strong>Automatic cleanup</strong></td><td>try-with-resources</td><td>No built-in</td><td><code>with</code> statement</td><td><code>defer</code></td></tr>
<tr>
<td><strong>Cleanup on error</strong></td><td><code>finally</code> (always)</td><td><code>finally</code> (always)</td><td><code>finally</code> (always)</td><td><code>defer</code> (always)</td></tr>
<tr>
<td><strong>When cleanup runs</strong></td><td>End of try block</td><td>End of try/catch</td><td>End of <code>with</code> block</td><td>End of function</td></tr>
<tr>
<td><strong>Cleanup location</strong></td><td>At end of try/catch</td><td>At end of try/catch</td><td>At end of <code>with</code></td><td>Right after acquisition</td></tr>
<tr>
<td><strong>Multiple cleanups</strong></td><td>Nested try-with-resources</td><td>Nested try/finally</td><td>Multiple <code>with</code> or finally</td><td>Multiple <code>defer</code> (LIFO)</td></tr>
<tr>
<td><strong>Requires interface</strong></td><td>Yes (AutoCloseable)</td><td>No</td><td>Yes (<code>__enter__</code>/<code>__exit__</code>)</td><td>No</td></tr>
</tbody>
</table>
</div><p><strong>Do JavaScript and Python have defer?</strong> No, but they have <code>finally</code> blocks that serve a similar purpose. The key difference: <code>finally</code> runs at the end of a try/catch block, while <code>defer</code> runs at the end of the entire function. Python's <code>with</code> statement is closer to <code>defer</code> in spirit (automatic cleanup), but it's block-scoped, not function-scoped.</p>
<h3 id="heading-why-defer-is-nice">Why defer is nice</h3>
<p><strong>1. Keep cleanup code next to acquisition:</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// Opening and closing are visually close</span>
file, err := os.Open(<span class="hljs-string">"data.txt"</span>)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    <span class="hljs-keyword">return</span> err
}
<span class="hljs-keyword">defer</span> file.Close()  <span class="hljs-comment">// Right here, immediately after opening</span>

<span class="hljs-comment">// Now focus on the actual work</span>
<span class="hljs-comment">// You don't have to scroll down to find the cleanup code</span>
</code></pre>
<p>Compare to Java finally:</p>
<pre><code class="lang-java">File file = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">try</span> {
    file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">"data.txt"</span>);
    <span class="hljs-comment">// ... lots of code here ...</span>
    <span class="hljs-comment">// ... you might forget you need to close ...</span>
} <span class="hljs-keyword">finally</span> {
    <span class="hljs-comment">// Cleanup code far away from acquisition</span>
    <span class="hljs-keyword">if</span> (file != <span class="hljs-keyword">null</span>) {
        file.close();
    }
}
</code></pre>
<p><strong>2. Multiple defers are easy:</strong></p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">complexOperation</span><span class="hljs-params">()</span> <span class="hljs-title">error</span></span> {
    conn, err := database.Connect()
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-keyword">return</span> err
    }
    <span class="hljs-keyword">defer</span> conn.Close()

    tx, err := conn.BeginTransaction()
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-keyword">return</span> err
    }
    <span class="hljs-keyword">defer</span> tx.Rollback()  <span class="hljs-comment">// Safe: rollback does nothing if already committed</span>

    lock := acquireLock()
    <span class="hljs-keyword">defer</span> lock.Release()

    <span class="hljs-comment">// Do complex work...</span>

    tx.Commit()
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}
<span class="hljs-comment">// All defers execute in reverse order: Release(), Rollback(), Close()</span>
</code></pre>
<p>In Java, this would require nested try-with-resources or a complex finally block.</p>
<p><strong>3. Defer works with any function call:</strong></p>
<pre><code class="lang-go">mu.Lock()
<span class="hljs-keyword">defer</span> mu.Unlock()  <span class="hljs-comment">// Unlock a mutex</span>

startTime := time.Now()
<span class="hljs-keyword">defer</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">()</span></span> {
    <span class="hljs-comment">// Log how long the function took</span>
    fmt.Printf(<span class="hljs-string">"Function took %v\n"</span>, time.Since(startTime))
}()

<span class="hljs-keyword">defer</span> logFunctionExit()  <span class="hljs-comment">// Log when function exits</span>

<span class="hljs-comment">// Anything can be deferred, not just AutoCloseable resources</span>
</code></pre>
<h3 id="heading-common-defer-gotchas-in-go">Common defer gotchas in Go</h3>
<p><strong>1. Defer in a loop (potential leak):</strong></p>
<pre><code class="lang-go"><span class="hljs-comment">// BAD: defers accumulate until function returns</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">processFiles</span><span class="hljs-params">(paths []<span class="hljs-keyword">string</span>)</span> <span class="hljs-title">error</span></span> {
    <span class="hljs-keyword">for</span> _, path := <span class="hljs-keyword">range</span> paths {
        file, err := os.Open(path)
        <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
            <span class="hljs-keyword">return</span> err
        }
        <span class="hljs-keyword">defer</span> file.Close()  <span class="hljs-comment">// Won't close until function ends!</span>

        <span class="hljs-comment">// Process file...</span>
    }
    <span class="hljs-comment">// All files are still open here!</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}

<span class="hljs-comment">// GOOD: Use a separate function</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">processFiles</span><span class="hljs-params">(paths []<span class="hljs-keyword">string</span>)</span> <span class="hljs-title">error</span></span> {
    <span class="hljs-keyword">for</span> _, path := <span class="hljs-keyword">range</span> paths {
        <span class="hljs-keyword">if</span> err := processFile(path); err != <span class="hljs-literal">nil</span> {
            <span class="hljs-keyword">return</span> err
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">processFile</span><span class="hljs-params">(path <span class="hljs-keyword">string</span>)</span> <span class="hljs-title">error</span></span> {
    file, err := os.Open(path)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-keyword">return</span> err
    }
    <span class="hljs-keyword">defer</span> file.Close()  <span class="hljs-comment">// Closes when THIS function returns</span>

    <span class="hljs-comment">// Process file...</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">nil</span>
}
</code></pre>
<p><strong>2. Defer evaluates arguments immediately:</strong></p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">example</span><span class="hljs-params">()</span></span> {
    x := <span class="hljs-number">1</span>
    <span class="hljs-keyword">defer</span> fmt.Println(x)  <span class="hljs-comment">// Will print 1, not 3</span>

    x = <span class="hljs-number">2</span>
    x = <span class="hljs-number">3</span>
}

<span class="hljs-comment">// To capture the final value, use a closure:</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">example</span><span class="hljs-params">()</span></span> {
    x := <span class="hljs-number">1</span>
    <span class="hljs-keyword">defer</span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">()</span></span> {
        fmt.Println(x)  <span class="hljs-comment">// Will print 3</span>
    }()

    x = <span class="hljs-number">2</span>
    x = <span class="hljs-number">3</span>
}
</code></pre>
<h3 id="heading-when-to-use-what-1">When to use what</h3>
<p><strong>Use defer (Go) when:</strong></p>
<ul>
<li>You need to guarantee cleanup runs (even on panic)</li>
<li>You want cleanup code next to acquisition</li>
<li>You have multiple resources to clean up</li>
</ul>
<p><strong>Use try-with-resources (Java) when:</strong></p>
<ul>
<li>Working with AutoCloseable resources</li>
<li>You want automatic cleanup at end of try block</li>
<li>You need the compiler to enforce cleanup</li>
</ul>
<p><strong>Use context managers (Python) when:</strong></p>
<ul>
<li>You want automatic setup and teardown</li>
<li>Working with files, locks, database connections</li>
<li>You want to create reusable resource patterns</li>
</ul>
<p><strong>Use finally (all languages) when:</strong></p>
<ul>
<li>You need cleanup code that runs whether or not exception occurs</li>
<li>You have complex error handling needs</li>
<li>You're working in JavaScript (no other good option)</li>
</ul>
<h3 id="heading-multiple-return-values-make-it-bearable">Multiple return values make it bearable</h3>
<p>One thing that makes Go's approach less painful: functions can return multiple values easily:</p>
<pre><code class="lang-go"><span class="hljs-comment">// Return both result and error</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">divide</span><span class="hljs-params">(a, b <span class="hljs-keyword">float64</span>)</span> <span class="hljs-params">(<span class="hljs-keyword">float64</span>, error)</span></span> {
    <span class="hljs-keyword">if</span> b == <span class="hljs-number">0</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>, errors.New(<span class="hljs-string">"division by zero"</span>)
    }
    <span class="hljs-keyword">return</span> a / b, <span class="hljs-literal">nil</span>
}

result, err := divide(<span class="hljs-number">10</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
    fmt.Println(<span class="hljs-string">"Error:"</span>, err)
} <span class="hljs-keyword">else</span> {
    fmt.Println(<span class="hljs-string">"Result:"</span>, result)
}
</code></pre>
<p>In other languages, you'd either need to throw an exception or return a special wrapper object.</p>
<h3 id="heading-when-to-use-what-approach">When to use what approach</h3>
<p><strong>Exceptions (Java, JS, Python) are better when:</strong></p>
<ul>
<li>You want clean "happy path" code</li>
<li>Errors should bubble up multiple layers</li>
<li>You're okay with hidden control flow</li>
<li>You want detailed stack traces</li>
</ul>
<p><strong>Explicit errors (Go) are better when:</strong></p>
<ul>
<li>You want to see exactly where errors can occur</li>
<li>You need very clear control flow</li>
<li>You want to force callers to think about errors</li>
<li>Performance matters (no stack unwinding overhead)</li>
</ul>
<h3 id="heading-my-opinion-on-gos-approach">My opinion on Go's approach</h3>
<p>Coming from Java, Go's error handling feels like <strong>death by a thousand paper cuts</strong>. The <code>if err != nil</code> pattern repeats so often that it drowns out the actual logic. Code that would be 10 lines in Java becomes 30 lines in Go.</p>
<p><strong>However</strong>, I'll admit: when debugging, it's very clear where errors come from and how they're handled. There's no mystery about what exceptions might be thrown or caught. In large codebases, this explicit approach can actually make code easier to understand.</p>
<p>Is it worth the verbosity? That depends on your priorities. Go chose simplicity and explicitness over convenience - for better or worse.</p>
<hr />
<h2 id="heading-closing-thoughts">Closing Thoughts</h2>
<p>I'm still learning Go, so there's a good chance I've made mistakes or missed nuances in my explanations. If you spot something wrong, I'd appreciate the feedback.</p>
<p><strong>This article is a work in progress.</strong> As I continue working with these languages, I'll keep enhancing and editing based on my evolving understanding. The goal isn't to master one language - it's to be able to code effectively in all of them, switching contexts as needed.</p>
<p>That's the real challenge: not just learning syntax, but understanding the <em>why</em> behind each language's design decisions. Hopefully this cheatsheet helps you (and future me) make those mental shifts a little easier.</p>
]]></content:encoded></item><item><title><![CDATA[From Backend to Frontend: My Journey Building a Web App with Svelte and ChatGPT]]></title><description><![CDATA[Between jobs, I found myself with some unexpected free time, and instead of just relaxing, I decided to turn it into an opportunity to learn something new. The result? A side project that pushed me out of my comfort zone and taught me a lot about fro...]]></description><link>https://blog.blockingqueue.com/from-backend-to-frontend-my-journey-building-a-web-app-with-svelte-and-chatgpt</link><guid isPermaLink="true">https://blog.blockingqueue.com/from-backend-to-frontend-my-journey-building-a-web-app-with-svelte-and-chatgpt</guid><category><![CDATA[UI]]></category><category><![CDATA[Svelte]]></category><category><![CDATA[side project]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Fri, 01 Aug 2025 10:33:53 GMT</pubDate><content:encoded><![CDATA[<p>Between jobs, I found myself with some unexpected free time, and instead of just relaxing, I decided to turn it into an opportunity to learn something new. The result? A side project that pushed me out of my comfort zone and taught me a lot about frontend development. Here’s what I built, the challenges I faced, and the lessons I learned along the way.</p>
<h2 id="heading-why-frontend-a-backend-developers-dilemma"><strong>Why Frontend? A Backend Developer’s Dilemma</strong></h2>
<p>Throughout my career, I’ve been a backend developer and I love it. The challenges of optimizing databases, designing APIs, and solving complex logic problems are incredibly rewarding. But one limitation always nagged at me: when you work purely on the backend, your creations are mostly invisible. You build tools for other developers, write scripts, or work on systems that users never directly interact with.</p>
<p>I wanted to build something <em>real</em>—something people could see, touch, and use. That meant learning frontend development.</p>
<h2 id="heading-my-struggles-with-css-and-how-chatgpt-saved-me"><strong>My Struggles with CSS (and How ChatGPT Saved Me)</strong></h2>
<p>I’ve tried learning frontend before, but CSS always stopped me in my tracks. No matter how much I read about flexbox, grids, or responsive design, my layouts ended up looking like they were built in the early 2000s. This time, though, I had a not so secret weapon: ChatGPT.</p>
<p>Instead of spending hours debugging why my <code>div</code>s refused to align properly, I could describe what I wanted in plain English, and ChatGPT would generate the CSS for me. It wasn’t perfect, if you look closely at my app, you might notice some inconsistencies (a telltale sign of AI assistance). But it was a game-changer. I could focus on functionality while still ending up with a decent-looking UI.</p>
<h2 id="heading-choosing-svelte-over-react"><strong>Choosing Svelte Over React</strong></h2>
<p>Another hurdle? The steep learning curve of <strong>React</strong>. I’ve dabbled in it before, but as a backend developer, the mental model of hooks, state management, and JSX felt overwhelming.</p>
<p>So I decided to try something different: <strong>Svelte</strong>. With its major update (Svelte 5) released last year, it seemed like the perfect time to jump in. And I’m glad I did—Svelte is <em>fast</em>, intuitive, and much easier to grasp for someone coming from a backend background. The syntax is cleaner, there’s less boilerplate, and the reactivity model just makes sense.</p>
<h2 id="heading-what-i-built-a-time-tracking-web-app"><strong>What I Built: A Time-Tracking Web App</strong></h2>
<p>After weeks of learning, experimenting, and (many) CSS frustrations, I finally built <a target="_blank" href="https://www.ivontime.com"><strong>Ivontime</strong></a>—a simple yet functional time-tracking application. It’s not the most polished product out there, but it’s mine, and I’m proud of it.</p>
<h3 id="heading-key-takeaways"><strong>Key Takeaways:</strong></h3>
<ol>
<li><p><strong>Frontend development is harder than it looks</strong> especially when you’re used to backend logic.</p>
</li>
<li><p><strong>AI tools like ChatGPT can bridge the gap</strong> when you’re struggling with styling or syntax.</p>
</li>
<li><p><strong>Svelte is a fantastic alternative to React</strong> especially for backend devs looking for a smoother transition into frontend.</p>
</li>
<li><p><strong>Building something real is the best way to learn</strong>. Tutorials are great, but nothing beats hands-on experience.</p>
</li>
</ol>
<h2 id="heading-whats-next"><strong>What’s Next?</strong></h2>
<p>This project was just the beginning. Now that I’ve dipped my toes into frontend development, I’m excited to keep improving my skills maybe even tackling another side project soon. Who knows? Maybe one day I’ll call myself a full-stack developer.</p>
<p>If you’re a backend dev curious about frontend, I encourage you to give it a shot. It’s frustrating at times, but incredibly rewarding when you see your code come to life in the browser.</p>
<p><strong>Check out my project here:</strong> <a target="_blank" href="https://www.ivontime.com"><strong>Ivontime</strong></a></p>
<p>Have you made a similar transition? What tools helped you the most? Let me know—I’d love to hear your story!</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Blockchains Through Java Code]]></title><description><![CDATA[This project is just a java implementation for an educative block chain. The full code for this article is committed here
A blockchain is essentially a digital ledger that maintains a continuously growing list of records, called blocks, which are lin...]]></description><link>https://blog.blockingqueue.com/understanding-blockchains-through-java-code</link><guid isPermaLink="true">https://blog.blockingqueue.com/understanding-blockchains-through-java-code</guid><category><![CDATA[Blockchain]]></category><category><![CDATA[Blockchain technology]]></category><category><![CDATA[Java]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Mon, 14 Jul 2025 10:27:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752488802182/5d53519b-7e91-4956-bcd8-3ca5c3d45543.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This project is just a java implementation for an educative block chain. The full code for this article is committed <a target="_blank" href="https://github.com/mydevalias/java-chain">here</a></p>
<p>A blockchain is essentially a digital ledger that maintains a continuously growing list of records, called blocks, which are linked and secured using cryptography. Think of it as a chain of digital containers, where each container holds some data and is cryptographically connected to the previous one, making it nearly impossible to alter past records without detection.</p>
<h2 id="heading-core-components-of-a-block">Core Components of a Block</h2>
<p>Every block in a blockchain contains several essential pieces of information:</p>
<ul>
<li><p>Hash: A unique digital fingerprint that identifies the block</p>
</li>
<li><p>Block Height: The position of the block in the chain (starting from 0)</p>
</li>
<li><p>Previous Hash: The hash of the previous block, creating the "chain" connection</p>
</li>
<li><p>Timestamp: When the block was created</p>
</li>
<li><p>Data: The actual information stored in the block</p>
</li>
<li><p>Nonce: A number used in the mining process to find a valid hash</p>
</li>
</ul>
<h3 id="heading-the-genesis-block">The Genesis Block</h3>
<p>Every blockchain starts with a special first block called the "genesis block." This block has no previous block to reference, so its previous hash is empty. In our Java implementation, the genesis block is created with <code>genesisBlock()</code> method</p>
<h2 id="heading-creating-new-blocks">Creating New Blocks</h2>
<p>When adding a new block to the chain, it must reference the previous block's hash. This creates the fundamental "chain" structure that gives blockchain its name. Each new block contains:</p>
<ul>
<li><p>An incremented block height</p>
</li>
<li><p>The hash of the previous block</p>
</li>
<li><p>A current timestamp</p>
</li>
<li><p>New data to be stored</p>
</li>
</ul>
<h2 id="heading-proof-of-work-mining">Proof of Work Mining</h2>
<p>One of blockchain's most important features is its security mechanism called "Proof of Work" This process, known as mining, requires computational effort to add new blocks to the chain.</p>
<h3 id="heading-how-mining-works">How Mining Works</h3>
<p>The mining process involves finding a special number called a "nonce" that, when combined with the block's data and hashed using SHA-256, produces a hash that meets certain criteria. In our example, we're looking for hashes that start with "00":</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">minerProofOfWork</span><span class="hljs-params">(Block block)</span> <span class="hljs-keyword">throws</span> NoSuchAlgorithmException </span>{
    <span class="hljs-keyword">int</span> nonce = <span class="hljs-number">0</span>;
    MessageDigest digest = MessageDigest.getInstance(<span class="hljs-string">"SHA-256"</span>);
    <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {
        block.nonce = Long.toString(nonce);
        String encodedData = block.encodedData();
        <span class="hljs-keyword">byte</span>[] encodedHash = digest.digest(encodedData.getBytes(StandardCharsets.UTF_8));
        String hash = bytesToHex(encodedHash);
        <span class="hljs-keyword">if</span> (hash.startsWith(<span class="hljs-string">"00"</span>)) {  <span class="hljs-comment">// This is our "difficulty"</span>
            block.hash = hash;
            <span class="hljs-keyword">return</span>;
        }
        nonce++;
    }
}
</code></pre>
<h3 id="heading-why-mining-matters">Why Mining Matters</h3>
<p>Mining serves several critical purposes:</p>
<ul>
<li><p>Security: It makes tampering with the blockchain computationally expensive</p>
</li>
<li><p>Consensus: It provides a way for the network to agree on which blocks are valid</p>
</li>
<li><p>Immutability: Changing a past block would require redoing all the computational work for that block and every subsequent block</p>
</li>
</ul>
<h3 id="heading-sha-256">SHA-256</h3>
<p>The SHA-256 hashing algorithm is central to blockchain security. This function takes any input and produces a fixed-length (256-bit) output that appears random. However, it's deterministic, meaning the same input always produces the same hash. This makes it easier for the network to verify that the hash meets the required conditions.</p>
<p>The proof of work process adjusts the nonce until the hash meets these conditions. Multiple miners race each other to find the correct nonce, so if someone wants to change something in a block, they would have to redo all the miners' work.</p>
<p>Each block contains the hash of the previous block, creating an unbreakable chain. If someone tries to alter data in an old block, its hash would change, invalidating all subsequent blocks since they reference the original hash. This is what makes blockchains tamper-evident.</p>
<p>Here is a sample output of the code:</p>
<pre><code class="lang-plaintext">Block 2025-07-02T15:09:20.192Z (Height: 0)
├─ Hash: 007e64bcb32305c4185a289d9a2963e8c004938f3246188f753e54c4131b423c...
├─ Previous Hash: ""
└─ Data: "Genesis"
        │
        ▼
Block 2025-07-02T15:09:20.347Z (Height: 1)
├─ Hash: 00cbd9262419c115d59df13e28f2fd8e3f348154511840d453794c6cefb4287d...
├─ Previous Hash: "007e64bcb32305c4185a289d9a2963e8c004938f3246188f753e54c4131b423c"
└─ Data: "QuestPortal"
        │
        ▼
Block 2025-07-02T15:09:22.470Z (Height: 2)
├─ Hash: 004da73e26b626cb4223af9173ecaf60f848642087092c83c4647e11c0271851...
├─ Previous Hash: "00cbd9262419c115d59df13e28f2fd8e3f348154511840d453794c6cefb4287d"
└─ Data: "ActionSystem"
        │
        ▼
Block 2025-07-02T15:09:24.493Z (Height: 3)
├─ Hash: 00db9f75009ba36f3c5f040fc06766304092786f39632427a6feb32cb9517ffa...
├─ Previous Hash: "004da73e26b626cb4223af9173ecaf60f848642087092c83c4647e11c0271851"
└─ Data: "PowerfulPlatform"
</code></pre>
<p>While our example uses random text as data, real blockchains store transaction records like transfers and contract calls.</p>
<p>Hope this helps you understand how things work, and I might continue this project with other small code examples that will show how blockchain works.</p>
]]></content:encoded></item><item><title><![CDATA[Detecting Virtual Thread Pinning in Java]]></title><description><![CDATA[Virtual threads represent a significant leap forward for Java. I believe they will rejuvenate the language. Combined with all the recent advancements, this will further solidify Java's dominance in areas where it already leads, such as enterprise sof...]]></description><link>https://blog.blockingqueue.com/detecting-virtual-thread-pinning-in-java</link><guid isPermaLink="true">https://blog.blockingqueue.com/detecting-virtual-thread-pinning-in-java</guid><category><![CDATA[Java]]></category><category><![CDATA[virtual threads]]></category><category><![CDATA[Threads]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Sun, 29 Jun 2025 12:51:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751369987959/d2437ca9-4084-41b7-a79f-30396e2d4d83.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Virtual threads represent a significant leap forward for Java. I believe they will rejuvenate the language. Combined with all the recent advancements, this will further solidify Java's dominance in areas where it already leads, such as enterprise software.</p>
<p>I was aware that Java 21 had issues with thread pinning when entering a synchronized block. To verify whether this problem has been resolved in Java 24, I needed a way of knowing if a thread is pinned and used the following jvm flag: <code>-Djdk.tracePinnedThreads=full</code> This flag should help track if virtual threads still get pinned.</p>
<p>For testing purposes, we have this basic code:</p>
<pre><code class="lang-java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Object lock = <span class="hljs-keyword">new</span> Object();

<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
    Thread virtualThread = Thread.ofVirtual().start(() -&gt; {
        <span class="hljs-keyword">synchronized</span> (lock) {  
            System.out.println(<span class="hljs-string">"vt"</span>);   
            <span class="hljs-keyword">try</span> {
                Thread.sleep(<span class="hljs-number">1000</span>);
            } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> RuntimeException(e);
            }

        }
    });
}
</code></pre>
<p>When running it with Java 21, I got the following log:</p>
<pre><code class="lang-plaintext">Virtual thread is pinned inside synchronized block
Thread[#22,ForkJoinPool-1-worker-1,5,CarrierThreads]
java.base/java.lang.VirtualThread$VThreadContinuation.onPinned(VirtualThread.java:183)
java.base/jdk.internal.vm.Continuation.onPinned0(Continuation.java:393)
java.base/java.lang.VirtualThread.parkNanos(VirtualThread.java:621)
java.base/java.lang.VirtualThread.sleepNanos(VirtualThread.java:791)
java.base/java.lang.Thread.sleep(Thread.java:507)
playground.thread.Echo.lambda$main$0(Echo.java:21) &lt;== monitors:1
java.base/java.lang.VirtualThread.run(VirtualThread.java:309)
</code></pre>
<p>When running the same code with Java 24, no pinning occurs. While I understand it can still happen when calling native code, for most use cases the issue is gone.</p>
]]></content:encoded></item><item><title><![CDATA[Events in Node.js]]></title><description><![CDATA[I heard multiple times that javascript is event-based but never really thought about how it works.
According to Wikipedia event-driven programming "is a programming paradigm in which the flow of the program is determined by events"
This doesn't say m...]]></description><link>https://blog.blockingqueue.com/events-in-nodejs</link><guid isPermaLink="true">https://blog.blockingqueue.com/events-in-nodejs</guid><category><![CDATA[Node.js]]></category><category><![CDATA[node]]></category><category><![CDATA[events]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Tue, 19 Jul 2022 09:48:42 GMT</pubDate><content:encoded><![CDATA[<p>I heard multiple times that javascript is event-based but never really thought about how it works.</p>
<p>According to Wikipedia event-driven programming "is a programming paradigm in which the flow of the program is determined by events"</p>
<p>This doesn't say much but once I saw a simple example and that brought a lot of clarity. Will help a lot to understand it and know its internals.</p>
<p>Node.js has a module that you can use to create fire and listen for your own events. The essence is that we will listen for specific events and register listeners for each event. Listeners are just functions. Then we can emit events and the registered listeners will be executed. Each event is identified by a string or symbol.</p>
<p>Tthe simplest code will look like this:</p>
<pre><code class="lang-plaintext">const EventEmitter = require("events");
const emitter = new EventEmitter();
emitter.on("testEvent", () =&gt; {
  console.log("test event");
})
emitter.emit("testEvent");
</code></pre>
<p>What was so good about it was the "on" method. I have seen it in https/http and in ethersjs provider.on and jquery, it's almost everywhere and much of the Node.js core API is built around it.</p>
]]></content:encoded></item><item><title><![CDATA[Use python in shell, as if it were awk]]></title><description><![CDATA[Use python in shell, as if it were awk
My first technical article was about how to use awk.
For some reason, I like learning bash even if it doesn't always make sense.
I posted an article on hacker news and somebody suggested using one-liners, Perl.
...]]></description><link>https://blog.blockingqueue.com/use-python-in-shell-as-if-it-were-awk</link><guid isPermaLink="true">https://blog.blockingqueue.com/use-python-in-shell-as-if-it-were-awk</guid><category><![CDATA[Python]]></category><category><![CDATA[python projects]]></category><category><![CDATA[Bash]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Sat, 05 Feb 2022 21:39:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644097349991/Hf5vJHlQy.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-use-python-in-shell-as-if-it-were-awk">Use python in shell, as if it were awk</h3>
<p>My first technical article was about how to use awk.
For some reason, I like learning bash even if it doesn't always make sense.
I posted an article on hacker news and somebody suggested using one-liners, Perl.
I thought that was a good idea but with Python, not Perl.</p>
<p>After starting to work on it I realized by default python is not that fitted for this.
Unfortunately, python is not that useful for command-line one-liner programs.
As soon as you need an indented block (like if) there is no way around line break, but even so, I think it will be a good exercise.
Awk also allows you to provide a file with the script so it might not be a big deal in the end.</p>
<h3 id="heading-basic-usage">Basic usage</h3>
<p>The first thing you need is to run python with some input as code.
This is easily achievable using -c</p>
<pre><code class="lang-bash">python -c <span class="hljs-string">'print(1 + 1)'</span>
</code></pre>
<p>The most common usage of awk is to print a few fields out of a file, for example:</p>
<pre><code class="lang-bash">$ cat file.in
Name, Population(2020), Population(2020), Land area(sq mi),
Los Angeles, 3898747, 3792621, 469.49
San Diego, 1386932, 1307402, 325.88
San Jose, 1013240,945942, 178.26
San Francisco, 873965, 805235, 46.91

$ awk  -F, <span class="hljs-string">'{print $1 $3}'</span> file.in
Name Population(2020)
Los Angeles 3792621
San Diego 1307402
San Jose945942
San Francisco 805235
</code></pre>
<p>The same thing in Python will look like:</p>
<pre><code>$ python -<span class="hljs-built_in">c</span> '<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> <span class="hljs-keyword">open</span>(<span class="hljs-string">"file.in"</span>): fields=line.<span class="hljs-built_in">split</span>(<span class="hljs-string">","</span>); <span class="hljs-built_in">print</span> (fields[<span class="hljs-number">0</span>].strip(),fields[<span class="hljs-number">3</span>].strip())'
<span class="hljs-type">Name</span>    <span class="hljs-type">Land</span> area(sq mi)
<span class="hljs-type">Los</span> <span class="hljs-type">Angeles</span>    <span class="hljs-number">469.49</span>
<span class="hljs-type">San</span> <span class="hljs-type">Diego</span>    <span class="hljs-number">325.88</span>
<span class="hljs-type">San</span> <span class="hljs-type">Jose</span>    <span class="hljs-number">178.26</span>
<span class="hljs-type">San</span> <span class="hljs-type">Francisco</span>    <span class="hljs-number">46.91</span>
</code></pre><p>Python looks a bit more complex and since it is longer it will be more complicated to type if this is not part of a script.
Another inconvenience is that each field needs to be stripped.
We also made use of ";" and wrote multiple instructions on the same line.</p>
<h3 id="heading-begin-end">Begin end</h3>
<p>Awk has begin and end actions.
These are very useful if you need to do sum or averages.</p>
<pre><code class="lang-bash">$ seq 99 101 &gt;numbers.txt
$ awk <span class="hljs-string">'{sum=sum+$1} END {print "Sum: "sum" Avg: "sum/NR}'</span> numbers.tx
Sum: 300 Avg: 100
</code></pre>
<p>Python version will look like:</p>
<pre><code class="lang-bash">$ python -c <span class="hljs-string">'sum=0; NR=0
for line in open("numbers.tx"): sum+=int(line); NR+=1
print("Sum:",sum," Avg:",sum/NR)'</span>
Sum: 300  Avg: 100.0
</code></pre>
<p>Awk can be written as a one-line while the python version will need multiple lines and initialization for variables.
At this point, I started to think that even if I do these multi-line commands it will be better to have a small utility function that will deal with the parsing and initialize standard awk variables.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> sys

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">arg_param_value</span>(<span class="hljs-params">i</span>):</span>
    <span class="hljs-keyword">if</span> i &lt; len(sys.argv):
        <span class="hljs-keyword">return</span> sys.argv[i]
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-string">""</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">parse_params</span>():</span>
    i = <span class="hljs-number">1</span>
    <span class="hljs-keyword">while</span> i &lt; len(sys.argv):
        arg = sys.argv[i]
        <span class="hljs-keyword">if</span> arg.startswith(<span class="hljs-string">'-'</span>):
            i += <span class="hljs-number">1</span>
            args[arg] = arg_param_value(i)
        <span class="hljs-keyword">else</span>:
            inputs.append(sys.argv[i])
        i += <span class="hljs-number">1</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">code</span>():</span>
    <span class="hljs-keyword">if</span> <span class="hljs-string">'-f'</span> <span class="hljs-keyword">in</span> args:
        <span class="hljs-keyword">with</span> open(args[<span class="hljs-string">'-f'</span>], <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> f:
            <span class="hljs-keyword">return</span> f.readlines()
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> inputs.pop(<span class="hljs-number">0</span>)  <span class="hljs-comment"># removes first element from the list</span>


<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">process_file</span>():</span>
    <span class="hljs-keyword">global</span> NR
    FNR = <span class="hljs-number">0</span>
    <span class="hljs-keyword">with</span> open(FILENAME, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> file:
        <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> file:
            line = line.strip()
            <span class="hljs-keyword">if</span> len(line) == <span class="hljs-number">0</span>:
                <span class="hljs-keyword">continue</span>
            <span class="hljs-keyword">if</span> FS == <span class="hljs-string">''</span>:
                fields = line.split()
            <span class="hljs-keyword">else</span>:
                fields = line.split(FS)
            NF = len(fields)
            FNR += <span class="hljs-number">1</span>
            NR += <span class="hljs-number">1</span>
            exec(code)

args = {}
inputs = []

parse_params()
code = code()
NR = <span class="hljs-number">0</span>
FS = <span class="hljs-string">''</span>
<span class="hljs-keyword">if</span> <span class="hljs-string">'-F'</span> <span class="hljs-keyword">in</span> args: FS = args[<span class="hljs-string">'-F'</span>]

<span class="hljs-keyword">for</span> FILENAME <span class="hljs-keyword">in</span> inputs:
    process_file()
</code></pre>
<p>And I run it like:</p>
<pre><code class="lang-bash">$ python main.py -F <span class="hljs-string">";"</span> <span class="hljs-string">"print(fields[2], fields[3])"</span> in1.csv in2.csv
</code></pre>
<p>Of course, there is more functionality in awk, I will slowly add it to my script and hopefully, I can turn it into something usable.
There won't be a $0 $1 $2 .. $n, instead, I will use line for $0 and fields[] for $1 $2 and all the others.</p>
<p>After doing all this I think python is useful for these types of tasks especially if you already use it in other places.
The main advantage would be that you don't have to learn awk if you already know python.
The GitHub with a more complete version is here: <a target="_blank" href="Link">https://github.com/liviusd/pawk</a></p>
<p>Please let me know what you think about it!</p>
]]></content:encoded></item><item><title><![CDATA[AWS Lambda Tutorial]]></title><description><![CDATA[The first time I heard about AWS lambda it make me doubt my career.
"Less" is an adjective suffix meaning “without” so I thought serverless is a way of getting rid of the servers.
I'm a backend developer so I imagine this is a way of generating backe...]]></description><link>https://blog.blockingqueue.com/aws-lambda-tutorial</link><guid isPermaLink="true">https://blog.blockingqueue.com/aws-lambda-tutorial</guid><category><![CDATA[Java]]></category><category><![CDATA[AWS]]></category><category><![CDATA[aws lambda]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Wed, 05 Jan 2022 22:56:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1641423228817/jAA6Q5ncD.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The first time I heard about AWS lambda it make me doubt my career.
"Less" is an adjective suffix meaning “without” so I thought serverless is a way of getting rid of the servers.
I'm a backend developer so I imagine this is a way of generating backend code.
In reality serverless is just a way of making your backend easier to scale.
Actually, you don't have to worry about how to scale it. </p>
<p>A few weeks ago I read again about it, it was something about how to build a SaaS with 0$, and lambda was mentioned because you get 1 000 000 free calls.
I liked the idea, seeing some benefits in it and I thought it would be useful to try all sorts of things, so I will need an easy deployment process that I could use to quickly run side projects.
In order to follow this, you will need an AWS account and aws cli working.
I will be using java but it should be easy to adapt to any other language because I will deploy it using Docker container.
I did this using the command line because it is easier to replicate.</p>
<h3 id="heading-how-lambda-works">How lambda works</h3>
<p>In theory, things are simple. You write a function that is ran based on some event.
In this tutorial, the event is a REST call.
Each such function will be executed in a container.
This means lambda has its own isolated execution environment.
The container is created when the function is executed for the first time.
After some time the container is destroyed.
Because of this, you might get into cold start issues.
This happens because your container may take some time to start.
Sometimes AWS will reuse your container so you won't notice it but if your function is rarely called you might see it takes a lot of time to execute.</p>
<h3 id="heading-build-the-project">Build the project</h3>
<p>I used the IntelliJ Idea and created an empty maven project.
I added the  AWS lambda java core dependency to the project and build a simple method that handles a request.</p>
<p>The java code looks like this:</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> bq;

<span class="hljs-keyword">import</span> com.amazonaws.services.lambda.runtime.Context;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Object <span class="hljs-title">handleRequest</span><span class="hljs-params">(Object arg, Context context)</span> </span>{
        <span class="hljs-keyword">return</span> arg;
    }
}
</code></pre>
<p>The maven pom to package this is:</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://maven.apache.org/POM/4.0.0"</span>
         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>bq<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>bq<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">build</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">plugins</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>maven-compiler-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.5.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">source</span>&gt;</span>11<span class="hljs-tag">&lt;/<span class="hljs-name">source</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">target</span>&gt;</span>11<span class="hljs-tag">&lt;/<span class="hljs-name">target</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">plugin</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>maven-dependency-plugin<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.1.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">includeScope</span>&gt;</span>runtime<span class="hljs-tag">&lt;/<span class="hljs-name">includeScope</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">executions</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">execution</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>copy-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">phase</span>&gt;</span>package<span class="hljs-tag">&lt;/<span class="hljs-name">phase</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">goals</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">goal</span>&gt;</span>copy-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">goal</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">goals</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">execution</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">executions</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">plugin</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">plugins</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">build</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.amazonaws<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>aws-lambda-java-core<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span>
</code></pre>
<p>We need maven-dependency-plugin because we will copy all the jar files into the docker container.
This plugin will make sure we have them in the target/dependency directory. 
Maybe there is a way to push everything as a jar but this is good enough to start. </p>
<p>Make sure everything works</p>
<pre><code class="lang-bash">$ mvn package
</code></pre>
<p>This is the project structure:</p>
<pre><code>.
├── Dockerfile
├── lambda<span class="hljs-operator">-</span>iam.json
├── lambda.iml
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── bq
│   │   │       └── App.java
│   │   └── resources
│   └── test
│       └── java
└── target
    ├── bq<span class="hljs-number">-1.0</span><span class="hljs-operator">-</span>SNAPSHOT.jar
    ├── classes
    │   └── bq
    │       └── App.class
    ├── dependency
    │   └── aws<span class="hljs-operator">-</span>lambda<span class="hljs-operator">-</span>java<span class="hljs-operator">-</span>core<span class="hljs-number">-1.2</span><span class="hljs-number">.1</span>.jar
    ├── generated<span class="hljs-operator">-</span>sources
    │   └── annotations
    ├── maven<span class="hljs-operator">-</span>archiver
        └── pom.properties
    <span class="hljs-comment">//removed the rest</span>
</code></pre><h3 id="heading-push-the-container">Push the container</h3>
<p>As previously mentioned the lambda will start a docker container.
This uses AWS base images for Lambda.
More details about it here https://gallery.ecr.aws/lambda/java
The usage tab also shows you how to build your lambda container if you need more instructions. </p>
<p>The docker file:</p>
<pre><code><span class="hljs-attribute">FROM</span> public.ecr.aws/lambda/java:<span class="hljs-number">11</span>

<span class="hljs-attribute">COPY</span> target/classes <span class="hljs-variable">${LAMBDA_TASK_ROOT}</span>
<span class="hljs-attribute">COPY</span> target/dependency/* <span class="hljs-variable">${LAMBDA_TASK_ROOT}</span>/lib/

<span class="hljs-attribute">CMD</span><span class="hljs-meta"> [ "bq.App::handleRequest" ]</span>

<span class="hljs-attribute">RUN</span> chmod <span class="hljs-number">644</span> $(find . -type f)
<span class="hljs-attribute">RUN</span> chmod <span class="hljs-number">755</span> $(find . -type d)
</code></pre><p>Build the container</p>
<pre><code>mvn package
docker build . -t java<span class="hljs-operator">-</span>aws<span class="hljs-operator">-</span>echo:<span class="hljs-number">1</span>
</code></pre><p>And tested it:</p>
<pre><code>docker run  <span class="hljs-operator">-</span>p <span class="hljs-number">9000</span>:<span class="hljs-number">8080</span> java<span class="hljs-operator">-</span>aws<span class="hljs-operator">-</span>echo:<span class="hljs-number">1</span>
curl <span class="hljs-operator">-</span>XPOST <span class="hljs-string">"http://localhost:9000/2015-03-31/functions/function/invocations"</span> <span class="hljs-operator">-</span>d <span class="hljs-string">'{}'</span>
</code></pre><p>The docker image will be hosted in  Elastic Container Registry (ecr)
After I saw everything worked on localhost I created an ecr repository and pushed the image:</p>
<pre><code>aws ecr create<span class="hljs-operator">-</span>repository <span class="hljs-operator">-</span><span class="hljs-operator">-</span>repository<span class="hljs-operator">-</span>name java<span class="hljs-operator">-</span>aws<span class="hljs-operator">-</span>echo

aws ecr get<span class="hljs-operator">-</span>login<span class="hljs-operator">-</span>password <span class="hljs-operator">-</span><span class="hljs-operator">-</span>region us<span class="hljs-operator">-</span>west<span class="hljs-number">-2</span> <span class="hljs-operator">|</span> docker login <span class="hljs-operator">-</span><span class="hljs-operator">-</span>username AWS <span class="hljs-operator">-</span><span class="hljs-operator">-</span>password<span class="hljs-operator">-</span>stdin YOUR_AWS_ACCOUNT_ID.dkr.ecr.us-west<span class="hljs-number">-2</span>.amazonaws.com
docker tag java<span class="hljs-operator">-</span>aws<span class="hljs-operator">-</span>echo:<span class="hljs-number">1</span> YOUR_AWS_ACCOUNT_ID.dkr.ecr.us-west<span class="hljs-number">-2</span>.amazonaws.com/java<span class="hljs-operator">-</span>aws<span class="hljs-operator">-</span>echo:<span class="hljs-number">1</span>
docker push YOUR_AWS_ACCOUNT_ID.dkr.ecr.us-west<span class="hljs-number">-2</span>.amazonaws.com/java<span class="hljs-operator">-</span>aws<span class="hljs-operator">-</span>echo:<span class="hljs-number">1</span>
</code></pre><h3 id="heading-create-the-lambda">Create the lambda</h3>
<p>Before we create the lambda we need to create a role that lambda will assume.
This is the minimal policy I found somewhere in AWS docs:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"Version"</span>: <span class="hljs-string">"2012-10-17"</span>,
  <span class="hljs-attr">"Statement"</span>: [
    {
      <span class="hljs-attr">"Effect"</span>: <span class="hljs-string">"Allow"</span>,
      <span class="hljs-attr">"Principal"</span>: {
        <span class="hljs-attr">"Service"</span>: <span class="hljs-string">"lambda.amazonaws.com"</span>
      },
      <span class="hljs-attr">"Action"</span>: <span class="hljs-string">"sts:AssumeRole"</span>
    }
  ]
}
</code></pre>
<pre><code class="lang-bash">aws iam create-role --role-name lambda --assume-role-policy-document file://lambda.json
</code></pre>
<p>Creating the lambda from the command line is fairly simple.</p>
<pre><code class="lang-bash">aws lambda create-function --function-name java-lambda --code ImageUri=YOUR_AWS_ACCOUNT_ID.dkr.ecr.us-west-2.amazonaws.com/java-aws-echo:1 --role arn:aws:iam::YOUR_AWS_ACCOUNT_ID:role/lambda --package-type Image
</code></pre>
<p>And voila that is all for your function. The only thing is that is not yet exposed on the internet. You can give it a try from AWS console UI and trigger a test.</p>
<h4 id="heading-exposing-the-lambda-into-internet">Exposing the lambda into internet.</h4>
<p>This was the part I wasn't expecting to be that complicated.
Once you have your lambda deployed you are probably asking yourself, where do I find the URL?
My assumption was that once you created it by default it will receive a default URL.
It is not the case and you need to do a few more steps and your function will be exposed using the AWS API Gateway.
Here is a bash script I created that will create an API GW for your lambda:</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env bash</span>
NAME=<span class="hljs-string">"Lambda API GW"</span>
METHOD=<span class="hljs-string">"POST"</span>
<span class="hljs-comment">#you can find the arn if you go on Lambda &gt; Functions &gt; java-lambda -&gt;Copy ARN</span>
LAMBDA_ARN=<span class="hljs-string">"PROVIDE THE LAMBDA ARN!!!!"</span>
REGION=<span class="hljs-string">"us-west-2"</span>

aws apigateway create-rest-api --name <span class="hljs-string">"<span class="hljs-variable">$NAME</span>"</span> &gt; apigateway-create-rest-api.tmp

API_ID=$(cat apigateway-create-rest-api.tmp |jq <span class="hljs-string">".id"</span> -r)
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$API_ID</span>

ROOT_ID=$(aws apigateway get-resources --rest-api-id <span class="hljs-variable">$API_ID</span>|jq <span class="hljs-string">'.items|first.id'</span> -r)
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$ROOT_ID</span>

<span class="hljs-comment">#request</span>
aws apigateway put-method --rest-api-id <span class="hljs-variable">$API_ID</span> --resource-id <span class="hljs-variable">$ROOT_ID</span> --http-method <span class="hljs-variable">$METHOD</span> --authorization-type NONE --no-api-key-required
<span class="hljs-comment">#Response</span>
aws apigateway put-method-response --rest-api-id <span class="hljs-variable">$API_ID</span> --resource-id <span class="hljs-variable">$ROOT_ID</span> --http-method <span class="hljs-variable">$METHOD</span> --status-code 200 --response-models <span class="hljs-string">"{\"application/json\": \"Empty\"}"</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"aws apigateway put-method --rest-api-id <span class="hljs-variable">$API_ID</span> --resource-id <span class="hljs-variable">$ROOT_ID</span> --http-method <span class="hljs-variable">$METHOD</span>"</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Integrations"</span>
URI=<span class="hljs-string">"arn:aws:apigateway:<span class="hljs-variable">$REGION</span>:lambda:path/2015-03-31/functions/<span class="hljs-variable">$LAMBDA_ARN</span>/invocations"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$URI</span>
aws apigateway put-integration --rest-api-id <span class="hljs-variable">$API_ID</span> --resource-id <span class="hljs-variable">$ROOT_ID</span> --<span class="hljs-built_in">type</span> AWS --http-method <span class="hljs-variable">$METHOD</span> --integration-http-method <span class="hljs-variable">$METHOD</span> --uri <span class="hljs-variable">$URI</span>

aws apigateway put-integration-response --rest-api-id <span class="hljs-variable">$API_ID</span> --resource-id <span class="hljs-variable">$ROOT_ID</span> --http-method <span class="hljs-variable">$METHOD</span> --status-code 200 --content-handling CONVERT_TO_TEXT

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Permissions"</span>
FUNCTION_NAME=$(aws lambda get-function --function-name <span class="hljs-variable">$LAMBDA_ARN</span>|jq <span class="hljs-string">'.Configuration.FunctionName'</span> -r)
ACCOUNT_ID=$(aws sts get-caller-identity|jq <span class="hljs-string">'.Account'</span> -r)
SOURCE_ARN=<span class="hljs-string">"arn:aws:execute-api:<span class="hljs-variable">$REGION</span>:<span class="hljs-variable">$ACCOUNT_ID</span>:<span class="hljs-variable">$API_ID</span>/*/<span class="hljs-variable">$METHOD</span>/"</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$FUNCTION_NAME</span> <span class="hljs-variable">$SOURCE_ARN</span>"</span>
aws lambda add-permission --function-name <span class="hljs-variable">$FUNCTION_NAME</span> --statement-id STTT --action lambda:InvokeFunction --principal apigateway.amazonaws.com --source-arn <span class="hljs-variable">$SOURCE_ARN</span>

aws apigateway create-deployment --rest-api-id <span class="hljs-variable">$API_ID</span> --stage-name atm1
</code></pre>
<p>I then searched for the lambda URL in the AWS console.
Log into your account and select API Gateway.
The URL for it is: https://us-west-2.console.aws.amazon.com/apigateway/main/apis
Then click on the newly created API and it should show the Resources and the POST method.
In the left menu, there should be "Stages", click on it and then select the atm1.<br />On a blue background, there should be "Invoke URL" in the stage editor.
Give it a test:</p>
<pre><code class="lang-bash">curl -XPOST <span class="hljs-string">"Your Invoke URL"</span> -d <span class="hljs-string">'{}'</span>
</code></pre>
<p>If you followed this tutorial and you have any issues please let me know. 
Hope this helps!</p>
]]></content:encoded></item><item><title><![CDATA[Yet Another AWK Cheat Sheet]]></title><description><![CDATA[I like bash, you can achieve a lot of things with some basic commands. I also like to make scripts for my work because it is the perfect documentation.
A long time ago I learned the basics of sed and grep and this helped me a lot while doing support....]]></description><link>https://blog.blockingqueue.com/yet-another-awk-cheat-sheet</link><guid isPermaLink="true">https://blog.blockingqueue.com/yet-another-awk-cheat-sheet</guid><category><![CDATA[Bash]]></category><category><![CDATA[linux-basics]]></category><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Wed, 15 Dec 2021 21:04:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1639602457915/d2TDmuM3U.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I like bash, you can achieve a lot of things with some basic commands. I also like to make scripts for my work because it is the perfect documentation.</p>
<p>A long time ago I learned the basics of sed and grep and this helped me a lot while doing support. Before having fancy tools like Kibana and CloudWatch knowing to grep was paying off. Even now if I need to debug something very complicated I prefer having the logs in a console. </p>
<p>Another thing that I still do in the console is some sanity checks between different sources. For example, I have a customer's file that I need to match to another file that has database records and see they are in sync.  This is how I got to learn AWK and write about how I use it.</p>
<p>I could add a proper introduction to AWK here but I assume you have some experience with it so I'll keep it short. AWK is a text-processing language and it is used as a data extraction tool or other similar tasks.</p>
<h3 id="heading-how-it-works">How it works</h3>
<p>Awk is a scripting language, requires no compilation, and has variables, numeric and string functions, and logical operators. </p>
<p>The basic thing to remember about it is that it deals with records and fields. Records are read one at a time and then they are split into fields. You can imagine it like reading a simple CSV file where a record is a line and a field is a cell.</p>
<h3 id="heading-basic-usage">Basic usage</h3>
<p>The most basic usage of awk is to get just a few fields out of each line. </p>
<pre><code>$ echo <span class="hljs-string">"a b c"</span> | awk <span class="hljs-string">'{ print <span class="hljs-subst">$2</span>" "<span class="hljs-subst">$3</span>}'</span>
b c
</code></pre><p>A few things to remember from here. Each field has its own variable: $1, $2, $3, and so on ($0 represents the entire record). 
Other important to remember variables (not all of them):</p>
<ul>
<li>NR: Number of Records</li>
<li>NF: Number of Fields</li>
<li>FNR refers to the record number (typically the line number) in the current file different than NR refers to the total record number. This has an interesting usage because NR==FNR is true only for the first file, and is used a lot in file comparison</li>
</ul>
<h3 id="heading-separator">Separator</h3>
<p>By default, awk will split records based on whitespace.  If you have a different separator use -F. The delimiter can be a regular expression: -F'[,:]' so you can use multiple delimiters.</p>
<pre><code>$ echo <span class="hljs-string">"a,b,c"</span> | awk -F<span class="hljs-string">','</span>  <span class="hljs-string">'{print <span class="hljs-subst">$1</span>" "<span class="hljs-subst">$2</span>}'</span>
a b
</code></pre><h3 id="heading-input">Input</h3>
<p>Most probably you won't use awk with echo or commands from the standard input, reading from a file would look like this:</p>
<pre><code>$ echo <span class="hljs-string">"a b c"</span> <span class="hljs-operator">&gt;</span> f1.txt
$ awk <span class="hljs-string">'{ print $2" "$3}'</span> f1.txt 
b c
</code></pre><p>Awk accepts multiple input files:</p>
<pre><code>$  echo <span class="hljs-string">"1 2 3"</span> <span class="hljs-operator">&gt;</span> f2.txt
$ awk <span class="hljs-string">'{ print $2" "$3}'</span> f1.txt f2.txt 
b c
<span class="hljs-number">2</span> <span class="hljs-number">3</span>
</code></pre><p>Program source code can also be read from a file, for this we use the -f option:</p>
<pre><code>$ echo <span class="hljs-string">'{ print $2" "$3}'</span> <span class="hljs-operator">&gt;</span> aw
$ awk <span class="hljs-operator">-</span>f aw f1.txt 
b c
</code></pre><h3 id="heading-selection-criteria">Selection Criteria</h3>
<p>Before doing the processing for each record you can use selection criteria. This can be a pattern or a condition.  This is useful if you want to process the lines which match the given pattern or respect condition.  </p>
<p>awk '/manager/ {print}' f1.txt </p>
<h3 id="heading-beginend">Begin/End</h3>
<p>You can also add begin and end actions.</p>
<pre><code>$awk '      
<span class="hljs-type">BEGIN</span> {<span class="hljs-built_in">print</span> <span class="hljs-string">"start"</span>}
{<span class="hljs-built_in">print</span> $<span class="hljs-number">1</span><span class="hljs-string">" "</span>$<span class="hljs-number">3</span>}
<span class="hljs-type">END</span> {<span class="hljs-built_in">print</span> <span class="hljs-string">"end"</span>}' f1.txt 

start
a <span class="hljs-built_in">c</span>
end
</code></pre><p>These are very useful if you need to do an average or print a sum. </p>
<pre><code><span class="hljs-string">$</span> <span class="hljs-string">seq</span> <span class="hljs-number">99</span> <span class="hljs-number">101</span> <span class="hljs-string">&gt;numbers.txt</span>
<span class="hljs-string">$</span> <span class="hljs-string">awk</span> <span class="hljs-string">'
{sum=sum+$1}
END {print "Sum: "sum" Avg: "sum/NR}'</span> <span class="hljs-string">numbers.txt</span> 

<span class="hljs-attr">Sum: 300 Avg:</span> <span class="hljs-number">100</span>
</code></pre><p>seq 99 101 &gt;numbers.txt will create a sequence of numbers, one per line in a file</p>
<h3 id="heading-matching-2-files">Matching 2 files</h3>
<p>With these examples in mind, we can try the main task, match 2 files based on one of the fields. You can think of it as a database inner join. </p>
<p>There will be 2 files: names.txt and license.txt. In names.txt the first column is the id while in license.txt the second column is the id. The script will be in the matchScript file.   We want to match each name to its license. </p>
<p>Awk will iterate over both files. As mentioned earlier, FNR is the record number in the current file while NR is the record number out of the total. The condition NR==FNR will hold true only while iterating records from the first files, this means details from the first file will be stored in a map. The map will be initialized on the first usage.
Next will end current rerond processing moving to the next one.</p>
<p>The second part will check if the id is already in the map and print if the condition matched. </p>
<pre><code>$ cat names.txt 
<span class="hljs-number">1</span>,Sleepy
<span class="hljs-number">3</span>,Dopey
<span class="hljs-number">2</span>,Grumpy

$ cat license.txt 
BASIC,<span class="hljs-number">2</span>
BASIC,<span class="hljs-number">1</span>
PRO,<span class="hljs-number">3</span>

$ cat matchScript 
(NR<span class="hljs-operator">=</span><span class="hljs-operator">=</span>FNR){
   names[$1]<span class="hljs-operator">=</span>$2; 
   next
}
($2 in names){
   print names[$2]<span class="hljs-string">","</span>$1
}

$ awk <span class="hljs-operator">-</span>f matchScript <span class="hljs-operator">-</span>F <span class="hljs-string">','</span> names.txt license.txt 
Grumpy,BASIC
Sleepy,BASIC
Dopey,PRO
</code></pre><h3 id="heading-other">Other</h3>
<p>If you need more info or you want to go into deep details  <a target="_blank" href="https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents">this</a>  is the best place for it. </p>
]]></content:encoded></item><item><title><![CDATA[Hello world]]></title><description><![CDATA[Here we go, my first blog post. In my head, I wanted to do this for a very long time but never really give it a try. Not sure why, one reason might be my not-so-good writing skills. Then I realized there is no better way to exercise your writing skil...]]></description><link>https://blog.blockingqueue.com/hello-world</link><guid isPermaLink="true">https://blog.blockingqueue.com/hello-world</guid><dc:creator><![CDATA[Liviu Stirb]]></dc:creator><pubDate>Mon, 06 Dec 2021 20:52:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1639254498957/jWau0DZB0.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Here we go, my first blog post. In my head, I wanted to do this for a very long time but never really give it a try. Not sure why, one reason might be my not-so-good writing skills. Then I realized there is no better way to exercise your writing skills than writing. It is like a tech skill, you can read as many tutorials as you want it won't work until you try it yourself. Practice makes perfect</p>
<p>My hopes are I will keep a log of tech things I'm doing and trying. From time to time I will also post nontech things I found interesting. </p>
<p>Why BlockingQueue? I really like StackOverflow but all bug names domain are already taken, so I was left with a data structure</p>
<p>That's all for now, read me later :)</p>
]]></content:encoded></item></channel></rss>