<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Calculator on 100 Exercises to Learn Go</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/</link><description>Recent content in Calculator on 100 Exercises to Learn Go</description><generator>Hugo</generator><language>en</language><atom:link href="https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/index.xml" rel="self" type="application/rss+xml"/><item><title>Integers</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/integers/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/integers/</guid><description>&lt;h1 id="integers"&gt;Integers&lt;a class="anchor" href="#integers"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Go doesn&amp;rsquo;t have &lt;em&gt;an&lt;/em&gt; integer type. It has a whole menu — and the compiler
makes you order precisely. This exercise&amp;rsquo;s stub looks innocent: multiply and
add. It doesn&amp;rsquo;t compile as written, and the reason is the entire lesson.&lt;/p&gt;
&lt;h2 id="the-menu"&gt;The menu&lt;a class="anchor" href="#the-menu"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;uint&lt;/span&gt; &lt;span style="color:#75715e"&gt;// platform-sized: 32 bits on 32-bit systems, 64 on 64-bit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;int8&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int16&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int32&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int64&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;uint8&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;uint16&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;uint32&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;uint64&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Signed or unsigned, then a width. &lt;code&gt;uint8&lt;/code&gt; holds 0–255 (you&amp;rsquo;ve met its
wrapping temper in the overflow exercise); &lt;code&gt;int64&lt;/code&gt; stretches past nine
quintillion in both directions. &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;uint&lt;/code&gt; follow the platform —
write &lt;code&gt;int&lt;/code&gt; for everyday counting and only reach for a width when the
protocol, the file format, or memory demands it.&lt;/p&gt;</description></item><item><title>Variables</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/variables/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/variables/</guid><description>&lt;h1 id="variables"&gt;Variables&lt;a class="anchor" href="#variables"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Every value lives somewhere. Go gives you two ways to say &amp;ldquo;a box called
&lt;code&gt;x&lt;/code&gt;&amp;rdquo; — and the difference between them is the difference between Go&amp;rsquo;s two
audiences: the compiler that must know everything, and the human who&amp;rsquo;d
rather not repeat it.&lt;/p&gt;
&lt;h2 id="the-two-declarations"&gt;The two declarations&lt;a class="anchor" href="#the-two-declarations"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;x&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; = &lt;span style="color:#ae81ff"&gt;21&lt;/span&gt; &lt;span style="color:#75715e"&gt;// long form: name, type, value — everything explicit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;y&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;x&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt; &lt;span style="color:#75715e"&gt;// short form: name and value; the type is inferred&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;var x int = 21&lt;/code&gt; states the type out loud. &lt;code&gt;y := x * 2&lt;/code&gt; lets the compiler
infer it — &lt;code&gt;y&lt;/code&gt; is an &lt;code&gt;int&lt;/code&gt; because &lt;code&gt;x * 2&lt;/code&gt; is one. Same result, less ink.
The short form exists because most of the time the right-hand side already
shouts the type; writing it twice helps nobody.&lt;/p&gt;</description></item><item><title>If Else</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/if_else/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/if_else/</guid><description>&lt;h1 id="if-else"&gt;If Else&lt;a class="anchor" href="#if-else"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;After the ceremony of types and declarations, a relief: Go&amp;rsquo;s &lt;code&gt;if&lt;/code&gt; works the
way you&amp;rsquo;d guess. Almost. The differences from the C family are small,
deliberate, and each one removes a historical bug class.&lt;/p&gt;
&lt;h2 id="the-shape"&gt;The shape&lt;a class="anchor" href="#the-shape"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: if &amp;lt;condition&amp;gt; { &amp;lt;then&amp;gt; } else { &amp;lt;otherwise&amp;gt; }&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Max&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Two things to notice. First, &lt;strong&gt;no parentheses&lt;/strong&gt; around the condition —
&lt;code&gt;if (a &amp;gt;= b)&lt;/code&gt; doesn&amp;rsquo;t compile. The braces do the grouping; parentheses
would be noise. Second, &lt;strong&gt;braces are mandatory&lt;/strong&gt;, even for one-line bodies.
Dangling-else ambiguity and the &amp;ldquo;added a second line outside the &lt;code&gt;if&lt;/code&gt;&amp;rdquo;
disaster simply can&amp;rsquo;t be written.&lt;/p&gt;</description></item><item><title>Panics</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/panics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/panics/</guid><description>&lt;h1 id="panics"&gt;Panics&lt;a class="anchor" href="#panics"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Some errors shouldn&amp;rsquo;t be handled. They should stop everything, loudly, with
a stack trace pointing at the crime scene. Go&amp;rsquo;s word for that is &lt;code&gt;panic&lt;/code&gt; —
and dividing by zero is its classic stage.&lt;/p&gt;
&lt;h2 id="the-emergency-brake"&gt;The emergency brake&lt;a class="anchor" href="#the-emergency-brake"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: panic(&amp;lt;value&amp;gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Divide&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; panic(&lt;span style="color:#e6db74"&gt;&amp;#34;division by zero&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;/&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;panic&lt;/code&gt; halts normal execution immediately and unwinds the stack, running
deferred cleanup on the way out. Unrecovered, it crashes the program. That
sounds violent because it is — which is why panics are reserved for
&lt;strong&gt;programmer errors&lt;/strong&gt;: broken invariants, impossible states, misuse of an
API. A zero divisor passed to integer division is exactly that: the caller
violated the contract, and pretending otherwise would compute garbage with
a straight face.&lt;/p&gt;</description></item><item><title>Factorial</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/factorial/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/factorial/</guid><description>&lt;h1 id="factorial"&gt;Factorial&lt;a class="anchor" href="#factorial"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Go has exactly one loop keyword: &lt;code&gt;for&lt;/code&gt;. No &lt;code&gt;while&lt;/code&gt;, no &lt;code&gt;do&lt;/code&gt;, no &lt;code&gt;foreach&lt;/code&gt;.
Every repetition in the language — counting, waiting, iterating — is some
shape of &lt;code&gt;for&lt;/code&gt;. This exercise teaches the first shape: the counting loop.&lt;/p&gt;
&lt;h2 id="dissecting-the-header"&gt;Dissecting the header&lt;a class="anchor" href="#dissecting-the-header"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: for &amp;lt;init&amp;gt;; &amp;lt;condition&amp;gt;; &amp;lt;post&amp;gt; { &amp;lt;body&amp;gt; }&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Factorial&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;result&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt;; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt;&lt;span style="color:#f92672"&gt;++&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;result&lt;/span&gt; &lt;span style="color:#f92672"&gt;*=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;i&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;result&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Three clauses separated by semicolons: declare the counter, state when to
keep going, say how to advance. &lt;code&gt;i++&lt;/code&gt; adds one — Go has &lt;code&gt;++&lt;/code&gt; and &lt;code&gt;--&lt;/code&gt; as
&lt;em&gt;statements only&lt;/em&gt;, so &lt;code&gt;x = i++&lt;/code&gt; doesn&amp;rsquo;t compile and nobody misses it.&lt;/p&gt;</description></item><item><title>Sum To</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/sum_to/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/sum_to/</guid><description>&lt;h1 id="sum-to"&gt;Sum To&lt;a class="anchor" href="#sum-to"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The previous exercise taught &lt;code&gt;for&lt;/code&gt; with all three clauses. Now meet the
shape Go uses &lt;em&gt;instead of&lt;/em&gt; &lt;code&gt;while&lt;/code&gt; — because there is no &lt;code&gt;while&lt;/code&gt; keyword to
reach for.&lt;/p&gt;
&lt;h2 id="the-loop-with-one-clause"&gt;The loop with one clause&lt;a class="anchor" href="#the-loop-with-one-clause"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: for &amp;lt;condition&amp;gt; { &amp;lt;body&amp;gt; }&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &amp;gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// runs until n reaches 0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Drop the init and post clauses and &lt;code&gt;for&lt;/code&gt; becomes a condition loop: keep
going while the condition holds. It&amp;rsquo;s the same keyword doing a different
job, which means one fewer concept to learn and one fewer keyword to
misremember at 2 AM.&lt;/p&gt;</description></item><item><title>Sum Range</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/sum_range/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/sum_range/</guid><description>&lt;h1 id="sum-range"&gt;Sum Range&lt;a class="anchor" href="#sum-range"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Counting loops count. But most real loops walk through &lt;em&gt;things&lt;/em&gt; — and for
that, Go gives &lt;code&gt;for&lt;/code&gt; a second companion: &lt;code&gt;range&lt;/code&gt;. This exercise also smuggles
in your first collection type. Two lessons, one stub.&lt;/p&gt;
&lt;h2 id="your-first-slice"&gt;Your first slice&lt;a class="anchor" href="#your-first-slice"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[]&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;{&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;} &lt;span style="color:#75715e"&gt;// a slice of ints: an ordered, growable sequence&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A &lt;strong&gt;slice&lt;/strong&gt; is Go&amp;rsquo;s workhorse list: &lt;code&gt;[]int&lt;/code&gt; reads &amp;ldquo;slice of int,&amp;rdquo; and the
literal in braces holds the elements. Slices appear everywhere ahead —
test tables already used them anonymously — so get comfortable at a glance.
One property matters today: a &lt;code&gt;nil&lt;/code&gt; slice (the zero value, no elements at
all) is perfectly valid, and ranging over it simply runs zero times. The
test&amp;rsquo;s &lt;code&gt;SumRange(nil)&lt;/code&gt; row proves it.&lt;/p&gt;</description></item><item><title>Overflow</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/overflow/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/overflow/</guid><description>&lt;h1 id="overflow"&gt;Overflow&lt;a class="anchor" href="#overflow"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Your calculator has been suspiciously well-behaved: every sum fit, every test
told the truth. Time to break that streak — on purpose.&lt;/p&gt;
&lt;p&gt;The exercise in &lt;code&gt;exercises/02_calculator/08_overflow&lt;/code&gt; asks an uncomfortable
question: what happens when the answer doesn&amp;rsquo;t fit the type? The full
integer model could fill a chapter on its own. You&amp;rsquo;ll get the slice that
matters — spotting the wrap and picking a fix — and leave the rest for the
day you meet it in the wild.&lt;/p&gt;</description></item><item><title>Saturating Add</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/saturating/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/saturating/</guid><description>&lt;h1 id="saturating-add"&gt;Saturating Add&lt;a class="anchor" href="#saturating-add"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The overflow exercise taught you to &lt;em&gt;report&lt;/em&gt; the wrap. But sometimes the
caller can&amp;rsquo;t do anything with a flag — a volume knob, a progress bar, a
pixel brightness has nowhere to put &amp;ldquo;error.&amp;rdquo; For those, there&amp;rsquo;s a third
answer between wrapping and panicking: &lt;strong&gt;clamp&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id="clamping-instead-of-wrapping"&gt;Clamping instead of wrapping&lt;a class="anchor" href="#clamping-instead-of-wrapping"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Saturating&lt;/strong&gt; arithmetic pins the result at the type&amp;rsquo;s boundary instead of
wrapping around it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;SaturatingAdd&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;10&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;20&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// 30 — fits, plain addition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;SaturatingAdd&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;200&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;100&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// 255 — would wrap to 44, clamped at MaxUint8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;200 + 100 is 300; the honest &lt;code&gt;uint8&lt;/code&gt; answer doesn&amp;rsquo;t exist, so 255 — the
nearest representable value — stands in. Audio mixers, image filters, and
game health bars all saturate: better slightly wrong in the right direction
than catastrophically wrapped.&lt;/p&gt;</description></item><item><title>Conversions</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/conversions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/conversions/</guid><description>&lt;h1 id="conversions"&gt;Conversions&lt;a class="anchor" href="#conversions"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The integers lesson laid down the law: no implicit conversion, ever. This
exercise is the other half of that law — the explicit machinery for moving
values between types, and the two very different fates awaiting them.&lt;/p&gt;
&lt;h2 id="the-one-syntax"&gt;The one syntax&lt;a class="anchor" href="#the-one-syntax"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Every numeric conversion in Go looks identical:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: &amp;lt;Type&amp;gt;(&amp;lt;value&amp;gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;int64(&lt;span style="color:#a6e22e"&gt;x&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// x, whatever its old type, viewed as int64&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;No keywords, no cast operators, no &amp;ldquo;convert&amp;rdquo; builtin — the type name &lt;em&gt;is&lt;/em&gt;
the operation. Once you&amp;rsquo;ve seen &lt;code&gt;uint32(multiplier)&lt;/code&gt;, you&amp;rsquo;ve seen them all.
Uniformity like this is deliberate: conversions should be visually boring,
because boring code gets reviewed correctly.&lt;/p&gt;</description></item><item><title>Div Mod</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/divmod/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/divmod/</guid><description>&lt;h1 id="div-mod"&gt;Div Mod&lt;a class="anchor" href="#div-mod"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The syntax lesson promised that multiple return values are normal in Go.
This exercise cashes that promise in: division produces &lt;em&gt;two&lt;/em&gt; answers, and
Go hands you both at once.&lt;/p&gt;
&lt;h2 id="one-call-two-results"&gt;One call, two results&lt;a class="anchor" href="#one-call-two-results"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Syntax: return &amp;lt;first&amp;gt;, &amp;lt;second&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;DivMod&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;) (&lt;span style="color:#a6e22e"&gt;quotient&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;remainder&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;/&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;%&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The caller unpacks them the same way:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;q&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;r&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;DivMod&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;7&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// q == 2, r == 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;No output parameters, no tuple type, no result struct for a two-value
answer. When a computation naturally yields a pair — quotient and
remainder, value and flag, result and error — the signature says so
directly. You&amp;rsquo;ve already used this shape three times over: &lt;code&gt;(sum, overflow)&lt;/code&gt;, &lt;code&gt;(value, ok)&lt;/code&gt;-style checks, and now &lt;code&gt;(quotient, remainder)&lt;/code&gt;.
Same rhythm, new verse.&lt;/p&gt;</description></item><item><title>Floats</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/floats/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/floats/</guid><description>&lt;h1 id="floats"&gt;Floats&lt;a class="anchor" href="#floats"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Integers had a strict chaperone: no mixing without conversion. Floats look
friendlier — until you notice the stub&amp;rsquo;s bug hiding in plain sight:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;func&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Half&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;float64&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; float64(&lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#f92672"&gt;/&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// looks right, halves wrong&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Half(5)&lt;/code&gt; returns 2, not 2.5. The conversion is correct and the division is
correct; the &lt;em&gt;order&lt;/em&gt; is the bug. Integer division runs first (&lt;code&gt;5/2 == 2&lt;/code&gt;,
truncated, remainder abandoned), and converting the corpse to &lt;code&gt;float64&lt;/code&gt;
can&amp;rsquo;t resurrect the &lt;code&gt;.5&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Runes</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/runes/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/runes/</guid><description>&lt;h1 id="runes"&gt;Runes&lt;a class="anchor" href="#runes"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Count the characters in &lt;code&gt;&amp;quot;héllo&amp;quot;&lt;/code&gt;. Five — obviously. Now ask Go:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;len(&lt;span style="color:#e6db74"&gt;&amp;#34;héllo&amp;#34;&lt;/span&gt;) &lt;span style="color:#75715e"&gt;// 6?!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Both answers are right; they&amp;rsquo;re answering different questions. &lt;code&gt;len&lt;/code&gt;
counts &lt;strong&gt;bytes&lt;/strong&gt;, and &lt;code&gt;é&lt;/code&gt; needs two bytes in UTF-8. The string holds six
bytes that render as five characters. Everything in this exercise follows
from that split.&lt;/p&gt;
&lt;h2 id="bytes-are-storage-runes-are-characters"&gt;Bytes are storage, runes are characters&lt;a class="anchor" href="#bytes-are-storage-runes-are-characters"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A Go &lt;code&gt;string&lt;/code&gt; is a read-only sequence of bytes. A &lt;strong&gt;&lt;code&gt;rune&lt;/code&gt;&lt;/strong&gt; is a Unicode
code point — what you mean by &amp;ldquo;character&amp;rdquo; — and it&amp;rsquo;s an alias for &lt;code&gt;int32&lt;/code&gt;:&lt;/p&gt;</description></item><item><title>Strings</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/strings/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/strings/</guid><description>&lt;h1 id="strings"&gt;Strings&lt;a class="anchor" href="#strings"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Strings support &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt;, slice like arrays — and then, just as you
reach for them, refuse to be changed. Try it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;s&lt;/span&gt; &lt;span style="color:#f92672"&gt;:=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;s&lt;/span&gt;[&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;] = &lt;span style="color:#e6db74"&gt;&amp;#39;H&amp;#39;&lt;/span&gt; &lt;span style="color:#75715e"&gt;// compile error: cannot assign to s[0]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Strings are immutable.&lt;/strong&gt; There is no edit-in-place, no character
replacement, no append-to-same-string. Every &amp;ldquo;modification&amp;rdquo; builds a brand
new string from pieces. Once that clicks, the whole standard library&amp;rsquo;s
shape makes sense.&lt;/p&gt;
&lt;h2 id="surgery-with-slices"&gt;Surgery with slices&lt;a class="anchor" href="#surgery-with-slices"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Reading a piece is free — slicing works exactly as on arrays:&lt;/p&gt;</description></item><item><title>Bitwise operators</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/bitwise/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/02_calculator/bitwise/</guid><description>&lt;h1 id="bitwise-operators"&gt;Bitwise operators&lt;a class="anchor" href="#bitwise-operators"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Fourteen exercises of arithmetic, and numbers have behaved like quantities.
Now they become &lt;em&gt;switchboards&lt;/em&gt;: individual bits flipped, combined, and
tested. The calculator section closes with the lowest-level arithmetic Go
offers — and the strangest-looking constants you&amp;rsquo;ve met.&lt;/p&gt;
&lt;h2 id="bits-as-flags"&gt;Bits as flags&lt;a class="anchor" href="#bits-as-flags"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;uint8&lt;/code&gt; holds eight independent on/off switches. The operators manipulate
them directly:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; | &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#75715e"&gt;// OR: a switch is on if it&amp;#39;s on in either side&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#75715e"&gt;// AND: a switch is on only if it&amp;#39;s on in both sides&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; ^ &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#75715e"&gt;// XOR: a switch is on if the sides disagree&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;amp;^&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;b&lt;/span&gt; &lt;span style="color:#75715e"&gt;// AND NOT: switches on in a but off in b (Go&amp;#39;s &amp;#34;clear&amp;#34; operator)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#75715e"&gt;// shift left n places (multiplies by 2ⁿ)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;a&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;n&lt;/span&gt; &lt;span style="color:#75715e"&gt;// shift right n places (divides by 2ⁿ)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;&amp;amp;^&lt;/code&gt; deserves a stare — most languages spell it &lt;code&gt;&amp;amp; ~&lt;/code&gt;, but Go mints a
dedicated operator so clearing bits reads as one thought. &lt;code&gt;p &amp;amp;^ Write&lt;/code&gt;
means &amp;ldquo;permissions minus write,&amp;rdquo; no parentheses required.&lt;/p&gt;</description></item></channel></rss>