<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Frihk Ian</title>
    <description>The latest articles on DEV Community by Frihk Ian (@frihk_ian).</description>
    <link>https://dev.to/frihk_ian</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3722900%2Fa0066d49-46b8-4c9c-aee1-170915f27351.jpg</url>
      <title>DEV Community: Frihk Ian</title>
      <link>https://dev.to/frihk_ian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/frihk_ian"/>
    <language>en</language>
    <item>
      <title>Structs in Go</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Thu, 04 Jun 2026 07:24:29 +0000</pubDate>
      <link>https://dev.to/frihk_ian/structs-in-go-1jff</link>
      <guid>https://dev.to/frihk_ian/structs-in-go-1jff</guid>
      <description>&lt;p&gt;Let's explore structs in Go. If you know Go's basic types like string, int, and bool, structs let you handle more complex data.&lt;br&gt;
Say you're building a store app. You need a product's name, price, and in-stock status. Using separate variables quickly becomes messy.&lt;br&gt;
Structs can simplify this. Picture structures are like a blueprint. It makes it possible to package all the related information in one neat and organized bundle.&lt;br&gt;
Now let's dive into creating and using custom data types in Go.&lt;/p&gt;
&lt;h2&gt;
  
  
  Declaring Structs
&lt;/h2&gt;

&lt;p&gt;Before using a struct, we first need to describe what it looks like. The result of defining a struct is not a piece of data, but the blueprint of data we want to store later on.&lt;br&gt;
Let's take a look at the declaration of the Product struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="c"&gt;// We use the 'type' keyword to define a new custom type.&lt;/span&gt;
&lt;span class="c"&gt;// We name our type 'Product' and specify that it is a 'struct'.&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;  &lt;span class="c"&gt;// Field for the product's name&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt; &lt;span class="c"&gt;// Field for the price&lt;/span&gt;
   &lt;span class="n"&gt;InStock&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;    &lt;span class="c"&gt;// Field to check if it's available&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating Struct Instances
&lt;/h2&gt;

&lt;p&gt;Using the struct we just created, let's create a product. Using the struct blueprint to create a piece of data is referred to as creating an instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt;
   &lt;span class="n"&gt;InStock&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c"&gt;// Creating an instance of Product using a "struct literal."&lt;/span&gt;
   &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Wireless Mouse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="m"&gt;25.99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;InStock&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c"&gt;// Creating an instance but leaving fields blank&lt;/span&gt;
   &lt;span class="n"&gt;item2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mousepad"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{Wireless Mouse 25.99 true}
{Mousepad 0 false}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be thinking Why does item2 show 0 and false when we didn’t set them?&lt;br&gt;
This is one of Go’s most important behaviours: zero values. In Go, when you declare a variable but don’t give it a value, Go doesn’t leave it empty or undefined (which causes bugs in many other languages). Instead, Go automatically fills it with the zero value for that type.&lt;br&gt;
Since we gave item2 a name, Go automatically sets the price to 0 and in stock to false . This is one of the features in Go that make it exceptional.&lt;br&gt;
Accessing and Modifying Fields&lt;br&gt;
When you want to modify or read information from a struct, you use a dot notation. Consider the dot to be opening up a particular drawer of your filing cabinet to view a folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt;
   &lt;span class="n"&gt;InStock&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Keyboard"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="m"&gt;45.00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;InStock&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c"&gt;// Reading a field&lt;/span&gt;
   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Original Price:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="c"&gt;// Modifying a field&lt;/span&gt;
   &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;39.99&lt;/span&gt;

   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Discounted Price:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Original Price: 45
Discounted Price: 39.99
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Anonymous Structs
&lt;/h2&gt;

&lt;p&gt;Sometimes we want to bundle up some information for one-time use. In such a case, we don’t want to have to go through the trouble of formally defining a whole blueprint. They are particularly useful when holding configuration settings, grouping test inputs, or temporarily organising data for a single function. We use anonymous structs in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c"&gt;// Declaring and initializing a struct at the exact same time&lt;/span&gt;
   &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}{&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Running on port:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running on port: 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a little confusing, if you are new to Go, because you create the blueprint, then use it to create an instance. Let's look at the code snippet again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;  &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;blueprint&lt;/span&gt; &lt;span class="n"&gt;opening&lt;/span&gt; &lt;span class="n"&gt;brace&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}{&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;closing&lt;/span&gt; &lt;span class="n"&gt;brace&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;blueprint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;followed&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;opening&lt;/span&gt; &lt;span class="n"&gt;brace&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;closing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of this like defining a shape, then filling it. Keep in mind that this is a non-reusable type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested (Embedded) Structs
&lt;/h2&gt;

&lt;p&gt;Unlike other programming languages, Go has no “classes” or “inheritance”. Instead, Go employs a technique of packing smaller structs into larger structs, similar to Russian nesting dolls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="c"&gt;// The smaller struct&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;City&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// The larger struct&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Contact&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="c"&gt;// Embedding the Address struct here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;player&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;City&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Nairobi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;Country&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Kenya"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c"&gt;// Accessing the nested data&lt;/span&gt;
   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"lives in"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;City&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice lives in Nairobi.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Struct Comparison
&lt;/h2&gt;

&lt;p&gt;A question might pop into your brain asking, “Are you able to compare two structs to determine whether they are equal or not?” Yes! Go supports the normal &lt;code&gt;==&lt;/code&gt; operator, but only if all the fields within the struct are comparable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12.50&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;item2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12.50&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;item3&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15.00&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Are item1 and item2 identical?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;item2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Are item1 and item3 identical?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;item3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Are item1 and item2 identical? true
Are item1 and item3 identical? false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go looks at every single field. If everything matches perfectly, the structs are considered equal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Photocopy vs. The Original
&lt;/h2&gt;

&lt;p&gt;This is the most essential concept for beginners in Go. Go’s default behavior is to send a struct by value into a function.&lt;br&gt;
If you were to give a friend a copy of your notes, how would they look? If they add a mustache to the photocopied version, you are fine! This is the default behavior of Go. To change an original struct, the function has to be passed a pointer to it (that is, the original notebook).&lt;br&gt;
Let’s observe both of them together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Pass by Value (The Photocopy)&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;failedDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;5.00&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Pass by Pointer (The Original)&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;successfulDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;5.00&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Book"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20.00&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;failedDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Trying to discount the photocopy&lt;/span&gt;
   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After failed discount:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="n"&gt;successfulDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Giving the function the original via '&amp;amp;'&lt;/span&gt;
   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After successful discount:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;After failed discount: 20
After successful discount: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices &amp;amp; Common Beginner Mistakes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In Go, names of fields that begin with a capital letter are “exported” and available to other packages (as opposed to names that start with a lowercase letter, which are “private”). If it begins with a lower-case letter (e.g., name), it belongs only to the package it is in. If in any doubt, make them capitalized so that you can use them freely.&lt;/li&gt;
&lt;li&gt;Pointers for Modification: Keep in mind the photocopy rule. If your function will call to change or update a struct, then you must pass a pointer (*).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary &amp;amp; Your Mini-Challenge
&lt;/h2&gt;

&lt;p&gt;You’ve made it! You’ve learned to structure unsorted variables into orderly, sensible structures. You know how to construct blueprints, make instances, navigate nested data, and update nested data correctly with pointers.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Data Types In Go</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Tue, 14 Apr 2026 08:23:50 +0000</pubDate>
      <link>https://dev.to/frihk_ian/data-types-in-go-5fm3</link>
      <guid>https://dev.to/frihk_ian/data-types-in-go-5fm3</guid>
      <description>&lt;p&gt;This is the first in a series of articles exploring data types in Go. The main goal of this series of articles is to provide detailed explanations and practical examples to help you build a clear and strong understanding of the different data types in Go. All the topics will be subdivided into manageable sections, each focusing on a specific aspect of data types, to facilitate gradual learning.&lt;/p&gt;

&lt;p&gt;There are many different data types in Go:&lt;br&gt;
The two main data types include:&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Basic(primitive) Types
&lt;/h2&gt;

&lt;p&gt;This type of data is made up of :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numbers(int, float64)&lt;/li&gt;
&lt;li&gt;Strings(string)&lt;/li&gt;
&lt;li&gt;Boolean(bool)&lt;/li&gt;
&lt;li&gt;Composite Types&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Composite Types
&lt;/h2&gt;

&lt;p&gt;These include &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Slices&lt;/li&gt;
&lt;li&gt;Maps&lt;/li&gt;
&lt;li&gt;Structs&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Other Important Types
&lt;/h2&gt;

&lt;p&gt;These are other very powerful and commonly used data types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function types (functions are treated as values)&lt;/li&gt;
&lt;li&gt;Pointer Types (used to work with memory)&lt;/li&gt;
&lt;li&gt;Interfaces Types(used to define a certain behavior and simplifying flexibility)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I will focus on basic data types, as they are fundamental to understanding Go and provide a solid foundation for more complex types. While the other types, such as composite, are not necessarily difficult, they involve broader concepts and all depend on the basic types. For a better understanding of each, I cover each in greater depth in separate, dedicated articles.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic(primitive) Types
&lt;/h2&gt;

&lt;p&gt;Let's delve directly into the details:&lt;/p&gt;
&lt;h3&gt;
  
  
  Numbers.
&lt;/h3&gt;

&lt;p&gt;Numbers are divided into : &lt;/p&gt;
&lt;h4&gt;
  
  
  Integers
&lt;/h4&gt;

&lt;p&gt;These are whole numbers. In  Go, we use them in two main ways :&lt;br&gt;
&lt;strong&gt;Int&lt;/strong&gt; - This is the most commonly used, including both positive and negative whole numbers.&lt;br&gt;
&lt;strong&gt;Uint&lt;/strong&gt; - Used to represent only positive whole numbers.&lt;/p&gt;
&lt;h4&gt;
  
  
  Floating-point
&lt;/h4&gt;

&lt;p&gt;These are real numbers (numbers with decimal points). They include both positive and negative numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="c"&gt;// 25 is an integer &lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="kt"&gt;float32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;186.8&lt;/span&gt; &lt;span class="c"&gt;// 186.8 is a float &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Go, Floating-point can either be float32 or float64. The choice lies in the level of precision you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strings
&lt;/h3&gt;

&lt;p&gt;In Go, strings are immutable sequences of UTF-8 encoded bytes, meaning once created, they cannot be altered, which ensures data integrity and consistency.&lt;/p&gt;

&lt;h4&gt;
  
  
  Immutability
&lt;/h4&gt;

&lt;p&gt;Once a string is created, it cannot be changed. Any attempt to change it creates a new string. &lt;/p&gt;

&lt;h4&gt;
  
  
  Indexing
&lt;/h4&gt;

&lt;p&gt;The individual bytes can be accessed with a given index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c"&gt;// 72 (byte value of 'H')&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print 72 as the strings store bytes and H is equal to the ASCII (byte) code of 72.&lt;/p&gt;

&lt;h4&gt;
  
  
  Length
&lt;/h4&gt;

&lt;p&gt;The number of bytes, not characters, is returned by the len() function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Unicode and Runes
&lt;/h4&gt;

&lt;p&gt;As the strings are UTF-8, some characters can occupy more than one byte. to act on characters (runes), use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output&lt;br&gt;
&lt;code&gt;72&lt;br&gt;
101&lt;br&gt;
108&lt;br&gt;
108&lt;br&gt;
111&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  String Concatenation
&lt;/h4&gt;

&lt;p&gt;The + operator can be used to add strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;full&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;  &lt;span class="c"&gt;// Hello World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To Byte Slice. &lt;br&gt;
You can convert a string to a slice of bytes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Boolean
&lt;/h3&gt;

&lt;p&gt;In Go, boolean values represent truth values; they can only be written as either true or false. They are used mostly for comparison purposes and control a program flow in if, for, or switch statements. In Go, by default, an uninitialized boolean has the value false, making it predictable and easy to work with in decision-making logic.&lt;/p&gt;

&lt;p&gt;The section has links to the data types used so far. A link to the next article will be added once it’s ready. When a link is not given on a given type of data, simply means that it is still in the process and it will be given shortly.&lt;br&gt;
&lt;a href="https://dev.to/frihk_ian/maps-in-golang-4jif"&gt;Maps&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/frihk_ian/structs-in-go-1jff"&gt;structs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additional learning materials.&lt;br&gt;
&lt;a href="https://go.dev/tour/basics/11" rel="noopener noreferrer"&gt;Go Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/go/go_data_types.php" rel="noopener noreferrer"&gt;W3schools&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Maps in Golang</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Tue, 10 Mar 2026 07:23:29 +0000</pubDate>
      <link>https://dev.to/frihk_ian/maps-in-golang-4jif</link>
      <guid>https://dev.to/frihk_ian/maps-in-golang-4jif</guid>
      <description>&lt;p&gt;Maps are one of the most flexible data types in Go. It might even be the most flexible compared to maps from other programming languages. And just as flexible as they are , they are also as simple to initialize.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Map Structure&lt;/strong&gt;&lt;br&gt;
A map in Go is composed of two main parts: the key and the value. The key is defined as a unique label used to store and find a specific &lt;br&gt;
Maps are one of the main data structures in Go. Maps have two main parts : the key and the value. The key is like a word you are looking up in a dictionary. It is a unique label that stores and carries a specific type of data. Whilst the value is like the definition of the word you are looking up in a dictionary, it is the data that is stored in the key value. Unlike keys, which only accept comparable types, the value can be of any legal Go type.&lt;/p&gt;

&lt;p&gt;In Go, there are two main ways to initialize maps. The first uses the Go built-in make function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;m := make(map[string]int)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The second one uses a map literal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;m := map[string]int{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;They initialize an empty map that takes in a string as the key and an integer as the value. The make method is more flexible because it allows you to specify an initial capacity. When the expected size is known, this preallocation can reduce map growth and rehashing during execution.&lt;/p&gt;

&lt;p&gt;Accessing Values and Handling Missing Keys&lt;br&gt;
Reading from a map in Go is quite simple. You access a value by indexing the map with its key, as shown below:&lt;br&gt;
&lt;code&gt;age := m["John"]&lt;/code&gt;&lt;br&gt;
Sometimes you might call a key that is not in the map; this happens as a result of misspelling or during testing. Go does not display any error, nor does it crash. It instead displays the zero value of the map's value type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"missing_key"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;// value is 0&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program above displays:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The “Comma ok” Idiom&lt;/strong&gt;&lt;br&gt;
This can be misleading; if a key has a value of zero, it becomes difficult to know whether the key has a zero value or its value is a zero. Go handles this by introducing the “ok” to help solve this error. This helps distinguish the erroneous value and the correct value.&lt;/p&gt;

&lt;p&gt;value, ok := m["missing_key"]&lt;br&gt;
if !ok {&lt;br&gt;
    Return “Error” // The key does not exist in the map&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Here, the value is the data in the map value. Whilst the “ok” is a boolean that is true. If the value exist then the ok becomes true, and the program executes without any error. If it does not exist, then the program returns an “error.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inserting and Updating Data&lt;/strong&gt;&lt;br&gt;
In Go, the syntax for inserting, updating, or deleting data in a map is consistent and straightforward. When adding/ updating a key, you simply assign a value; the method is pretty much the same as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// Insert: Key "Alice" does not exist, so it is created.&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; 

&lt;span class="c"&gt;// Update: Key "Alice" already exists, so its value is overwritten.&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;26&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deleting Elements&lt;/strong&gt;&lt;br&gt;
When deleting from a map, we use the Go built-in function delete(). It removes the key and everything associated with it. Below is its syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Bob"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Removes "Alice" from the map&lt;/span&gt;
&lt;span class="nb"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The delete function has a safety feature that makes it very safe to work with. In case of deletion of something that is not in the map, it simply does nothing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Iterating Over Maps&lt;/strong&gt;&lt;br&gt;
Now that you know how to add and remove individual items, you’ll likely want to loop through the entire collection. In Go, we use the range keyword to iterate over a map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Alice&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Bob&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Charlie&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;35&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Something to keep in mind when it comes to maps is that they are  “commitment-phobic” when it comes to order. Unlike arrays or slices, they do not have a specified order during iteration. If you run the code above three times, chances are that you might get three different sequences. If you need a consistent order, you’ll have to sort the keys manually in a separate slice first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maps as Reference Types&lt;/strong&gt;&lt;br&gt;
The most significant points to keep in mind are that maps are a type of reference. Passing a map to a function does not give a copy of the data, but the pointer to the underlying data structure.&lt;br&gt;
This implies that when you make changes to a map within a function, the changes will be carried over to the original map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;updateAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Alice&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt; &lt;span class="c"&gt;// This changes the original map!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Map Performance and Concurrency&lt;/strong&gt;&lt;br&gt;
Maps are incredibly fast, offering O(1) average time complexity for searches, additions, and deletions. Maps are extremely quick with an average time complexity of O(1) for searches, add, and delete. There is, however, a kind of safety limit that they have: Maps are not thread-safe.&lt;br&gt;
If two different goroutines try to write to the same map at the same time, your program will crash with a fatal error: concurrent map writes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature 
Behavior in Golang Maps
Search Speed
Extremely fast (Constant time)
Iteration Order
Random/Unordered
Thread Safety
None (Requires sync.Mutex or sync.Map)
Zero Value
nil (A nil map can be read, but writing to it causes a panic)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Maps are the go-to tool in Go when you need a fast, associative data structure. By mastering the make function, the “comma ok” idiom, and understanding the nuances of iteration and reference types, you can handle complex data relationships with very little code.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Go templates</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Wed, 11 Feb 2026 12:23:08 +0000</pubDate>
      <link>https://dev.to/frihk_ian/go-templates-51nm</link>
      <guid>https://dev.to/frihk_ian/go-templates-51nm</guid>
      <description>&lt;p&gt;The first thing to handle is answering the question, "what are Go templates?" Go templates are a way to create dynamic content in Go by allowing the user to mix data with plain text or HTML files with data received from the Go code. This makes it possible to change the data in a template very easily, as it can be edited with ease. They are mainly used in web dev to create web pages that rely on user inputs or content from a database.&lt;br&gt;
A template is just a file that has placeholders for data. In Go, placeholders are written using double curly braces {{ }}. These curly braces can hold a user’s data, and this can be shown by {{ .name }}. The dot refers to the data object you refer to from Go. When the template is running, Go replaces the {{ .name }} with the actual user name from the user's input or the database.&lt;br&gt;
To use templates in Go, a data structure that can help you hold the data to show, usually a map or a struct, is required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type PageData struct {
    Title   string
    Message string
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you parse the template file in your Go code and execute it with your data. The template system will automatically replace all placeholders with the correct values. This keeps your HTML or text separate from your Go code, making your program cleaner and easier to maintain.&lt;br&gt;
A good example of template file is as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;{{ .Title }}&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;{{ .Title }}&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;{{ .Message }}&amp;lt;/p&amp;gt;

    {{ if .IsLogged }}
        &amp;lt;p&amp;gt;Welcome back, {{ .User }}! &amp;lt;/p&amp;gt;
    {{ else }}
        &amp;lt;p&amp;gt;Please login to continue. &amp;lt;/p&amp;gt;
    {{ end }}

    &amp;lt;h2&amp;gt;Your Tasks:&amp;lt;/h2&amp;gt;
    &amp;lt;ul&amp;gt;
        {{ range .Tasks }}
            &amp;lt;li&amp;gt;{{ . }}&amp;lt;/li&amp;gt;
        {{ else }}
            &amp;lt;li&amp;gt;No tasks available.&amp;lt;/li&amp;gt;
        {{ end }}
    &amp;lt;/ul&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go templates also support basic logic. This helps to manipulate the values to be displayed. You can display certain things if a certain condition is met. You can also range over maps, structs, among others. For example, you can display a specific message to only logged-in users, you can list all the content of a check list from a shopping list among many others. You can also use functions like len to make the content more flexible.&lt;br&gt;
Below is an example of Go code that displays its output to the HTML file above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "html/template"
    "net/http"
)

type PageData struct {
    Title    string
    Message  string
    IsLogged bool
    User     string
    Tasks    []string
}

func handler(w http.ResponseWriter, r *http.Request) {
    data := PageData{
        Title:    "Go Templates Advanced Example",
        Message:  "Using conditions and loops in templates",
        IsLogged: true,
        User:     "Ian",
        Tasks:    []string{"Learn Go templates", "Build a web app", "Practice Go daily"},
    }

    tmpl := template.Must(template.ParseFiles("index.html"))
    tmpl.Execute(w, data)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, to generate a dynamic text or HTML in Go, the best option is the Go template. They help separate the design of your output from your code logic, allow you to reuse templates with different data, and make it easy to build web pages, reports, or any text-based content.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
