<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Ticket v1 on 100 Exercises to Learn Go</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/</link><description>Recent content in Ticket v1 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/03_ticket_v1/index.xml" rel="self" type="application/rss+xml"/><item><title>Struct</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/struct/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/struct/</guid><description>&lt;h1 id="struct"&gt;Struct&lt;a class="anchor" href="#struct"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Until now a ticket has been two loose strings drifting through function
signatures. That arrangement works for exactly one exercise longer. The task
in &lt;code&gt;exercises/03_ticket_v1/01_struct&lt;/code&gt; gives those strings a home: a &lt;code&gt;Ticket&lt;/code&gt;
type with a shape the compiler can check.&lt;/p&gt;
&lt;p&gt;Structs repay deep study, but deep study can wait. Today is about three
skills only — declaring one, building one, and deciding which fields
outsiders may touch. Everything else structs can do will arrive, as always,
right when you need it.&lt;/p&gt;</description></item><item><title>Validation</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/validation/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/validation/</guid><description>&lt;h1 id="validation"&gt;Validation&lt;a class="anchor" href="#validation"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;A struct can hold anything — including nonsense. &lt;code&gt;Ticket{Title: &amp;quot;&amp;quot;, Description: &amp;quot;x&amp;quot;}&lt;/code&gt; compiles happily, and every function downstream pays for
it. This exercise attaches judgment to the type: a method that inspects a
ticket and reports what&amp;rsquo;s wrong with it.&lt;/p&gt;
&lt;h2 id="behavior-attaches-with-receivers"&gt;Behavior attaches with receivers&lt;a class="anchor" href="#behavior-attaches-with-receivers"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Functions live at package level. &lt;strong&gt;Methods&lt;/strong&gt; live on a type, declared with
a &lt;strong&gt;receiver&lt;/strong&gt; between &lt;code&gt;func&lt;/code&gt; and the name:&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: func (&amp;lt;recv&amp;gt; &amp;lt;Type&amp;gt;) &amp;lt;Name&amp;gt;(&amp;lt;params&amp;gt;) &amp;lt;results&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;t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt;) &lt;span style="color:#a6e22e"&gt;Validate&lt;/span&gt;() &lt;span style="color:#66d9ef"&gt;error&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// t is the ticket this was called on&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;t&lt;/code&gt; is just a parameter with a privileged position — the value before the
dot in &lt;code&gt;tk.Validate()&lt;/code&gt;. A value receiver like this one gets a &lt;em&gt;copy&lt;/em&gt;, which
is fine: validation only reads. (Mutation needs a different receiver, two
exercises from now.)&lt;/p&gt;</description></item><item><title>Constructor</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/constructor/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/constructor/</guid><description>&lt;h1 id="constructor"&gt;Constructor&lt;a class="anchor" href="#constructor"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The previous exercise taught tickets to judge themselves — after they
already exist. That&amp;rsquo;s backwards. By the time &lt;code&gt;Validate&lt;/code&gt; complains, the
invalid ticket has been passed around, stored, maybe rendered. This
exercise moves the checkpoint to the only place it can&amp;rsquo;t be skipped: the
moment of creation.&lt;/p&gt;
&lt;h2 id="reject-at-birth"&gt;Reject at birth&lt;a class="anchor" href="#reject-at-birth"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The convention you met in the structs lesson grows up here. &lt;code&gt;NewTicket&lt;/code&gt;
stops returning a bare &lt;code&gt;Ticket&lt;/code&gt; and starts returning a verdict:&lt;/p&gt;</description></item><item><title>Setters</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/setters/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/setters/</guid><description>&lt;h1 id="setters"&gt;Setters&lt;a class="anchor" href="#setters"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Methods with value receivers read. But a ticket&amp;rsquo;s title needs &lt;em&gt;changing&lt;/em&gt; —
and the previous lesson&amp;rsquo;s receiver hands every method a disposable copy.
Watch what happens if &lt;code&gt;SetTitle&lt;/code&gt; takes that copy and writes to it: the
assignment lands, the method returns, the copy evaporates. The caller&amp;rsquo;s
ticket never moves. This exercise fixes that with one character.&lt;/p&gt;
&lt;h2 id="the-copy-in-the-mirror"&gt;The copy in the mirror&lt;a class="anchor" href="#the-copy-in-the-mirror"&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;func&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt;) &lt;span style="color:#a6e22e"&gt;SetTitle&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;title&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt;) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;t&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Title&lt;/span&gt; = &lt;span style="color:#a6e22e"&gt;title&lt;/span&gt; &lt;span style="color:#75715e"&gt;// writes to the copy; caller sees nothing&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;Every method call copies the receiver into &lt;code&gt;t&lt;/code&gt;, exactly as if you&amp;rsquo;d passed
it as an argument. For readers like &lt;code&gt;Validate&lt;/code&gt; that&amp;rsquo;s harmless. For
writers it&amp;rsquo;s fatal — mutations die with the copy. Go&amp;rsquo;s answer isn&amp;rsquo;t
reference semantics or annotations. It&amp;rsquo;s a different receiver:&lt;/p&gt;</description></item><item><title>Value Receivers</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/receivers/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/receivers/</guid><description>&lt;h1 id="value-receivers"&gt;Value Receivers&lt;a class="anchor" href="#value-receivers"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The setters lesson ended with a rule: pointer receivers mutate. This one is
its mirror image — and its warning label. Sometimes the &lt;em&gt;point&lt;/em&gt; is to leave
the original untouched.&lt;/p&gt;
&lt;h2 id="change-without-changing"&gt;Change without changing&lt;a class="anchor" href="#change-without-changing"&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;func&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;t&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt;) &lt;span style="color:#a6e22e"&gt;Renamed&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;newTitle&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt;) &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;t&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;Title&lt;/span&gt; = &lt;span style="color:#a6e22e"&gt;newTitle&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;t&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;Same assignment as a setter, opposite fate. The value receiver copies the
ticket, the copy gets the new title, and the copy comes home as the return
value. The caller&amp;rsquo;s original never moves:&lt;/p&gt;</description></item><item><title>Zero Value</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/zero_value/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/zero_value/</guid><description>&lt;h1 id="zero-value"&gt;Zero Value&lt;a class="anchor" href="#zero-value"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;NewTicket&lt;/code&gt; promised that every ticket enters the world valid. That promise
has a hole: nobody is forced to use the constructor.&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;var&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;tk&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt; &lt;span style="color:#75715e"&gt;// compiles. No NewTicket in sight.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Any caller can declare a &lt;code&gt;Ticket&lt;/code&gt; directly and hold… what, exactly? In most
languages, an uninitialized object is a landmine — null fields, undefined
behavior, the constructor-or-die ultimatum. Go chose differently, and this
exercise is about designing &lt;em&gt;for&lt;/em&gt; that choice instead of against it.&lt;/p&gt;</description></item><item><title>String</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/string/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/string/</guid><description>&lt;h1 id="string"&gt;String&lt;a class="anchor" href="#string"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;fmt.Println(tk)&lt;/code&gt; prints something today. Something &lt;em&gt;useless&lt;/em&gt; — field
braces and raw values, the debug dump every Go struct gets for free. This
exercise teaches your type to introduce itself properly, and smuggles in
your first interface along the way.&lt;/p&gt;
&lt;h2 id="a-method-the-whole-standard-library-calls"&gt;A method the whole standard library calls&lt;a class="anchor" href="#a-method-the-whole-standard-library-calls"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;fmt&lt;/code&gt; package prints any value. But before falling back to the dump, it
asks one question: does this value have a &lt;code&gt;String() string&lt;/code&gt; method?&lt;/p&gt;</description></item><item><title>Embedding</title><link>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/embed/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://hamzahraihan.github.io/100-exercises-to-learn-go/03_ticket_v1/embed/</guid><description>&lt;h1 id="embedding"&gt;Embedding&lt;a class="anchor" href="#embedding"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Go has no inheritance. No parent classes, no &lt;code&gt;extends&lt;/code&gt;, no method override
chains to untangle at midnight. And yet real domains share structure —
every ticket, user, and comment in a system wants an ID and timestamps.
Without inheritance, how do fields get reused? By &lt;em&gt;containing&lt;/em&gt; instead of
&lt;em&gt;descending&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id="composition-literally"&gt;Composition, literally&lt;a class="anchor" href="#composition-literally"&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;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Meta&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;ID&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&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;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Ticket&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Meta&lt;/span&gt; &lt;span style="color:#75715e"&gt;// no field name — the type name is the field&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;Title&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&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;A field declared with only a type is &lt;strong&gt;embedded&lt;/strong&gt;. &lt;code&gt;Ticket&lt;/code&gt; doesn&amp;rsquo;t inherit
from &lt;code&gt;Meta&lt;/code&gt;; it &lt;em&gt;contains&lt;/em&gt; a &lt;code&gt;Meta&lt;/code&gt;, addressable as &lt;code&gt;tk.Meta&lt;/code&gt;. So far,
ordinary nesting. The magic is what Go adds on top: &lt;strong&gt;promotion&lt;/strong&gt;. The
embedded struct&amp;rsquo;s fields (and methods) are lifted to the outer type, so
both spellings work:&lt;/p&gt;</description></item></channel></rss>