<?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>Forem: Hugo Oliveira</title>
    <description>The latest articles on Forem by Hugo Oliveira (@hugohenrick).</description>
    <link>https://forem.com/hugohenrick</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%2F3574521%2F8d433bcf-0342-488b-8216-be7755284009.png</url>
      <title>Forem: Hugo Oliveira</title>
      <link>https://forem.com/hugohenrick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hugohenrick"/>
    <language>en</language>
    <item>
      <title>Understanding Interfaces in Go — The Complete Beginner’s Guide</title>
      <dc:creator>Hugo Oliveira</dc:creator>
      <pubDate>Mon, 10 Nov 2025 04:11:04 +0000</pubDate>
      <link>https://forem.com/hugohenrick/understanding-interfaces-in-go-the-complete-beginners-guide-3alf</link>
      <guid>https://forem.com/hugohenrick/understanding-interfaces-in-go-the-complete-beginners-guide-3alf</guid>
      <description>&lt;p&gt;One of the most misunderstood features for newcomers to Go is the &lt;strong&gt;interface&lt;/strong&gt;.&lt;br&gt;
But once you understand how they really work, you’ll realize that they are one of Go’s most powerful design tools.&lt;/p&gt;

&lt;p&gt;Let’s explore what they are, how they work, and why Go’s approach is so unique.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is an Interface?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Go, an &lt;strong&gt;interface&lt;/strong&gt; defines &lt;strong&gt;behavior&lt;/strong&gt;, not data.&lt;/p&gt;

&lt;p&gt;It’s simply a collection of &lt;strong&gt;method signatures&lt;/strong&gt; — no implementation, no data fields.&lt;/p&gt;

&lt;p&gt;Think of it as a &lt;strong&gt;contract&lt;/strong&gt;:&lt;br&gt;
If a type has all the methods that an interface declares, it &lt;strong&gt;implicitly implements&lt;/strong&gt; that interface.&lt;/p&gt;

&lt;p&gt;“If it walks like a duck and quacks like a duck, it’s a duck.”&lt;br&gt;
That’s the Go philosophy.&lt;/p&gt;

&lt;p&gt;Example:&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;type&lt;/span&gt; &lt;span class="n"&gt;Mover&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Move&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="kt"&gt;int&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;Any type that has a &lt;code&gt;Move(int, int)&lt;/code&gt; method automatically &lt;strong&gt;implements&lt;/strong&gt; &lt;code&gt;Mover&lt;/code&gt;.&lt;br&gt;
No &lt;code&gt;implements&lt;/code&gt; keyword, no inheritance needed. Go keeps it simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s build something fun: a small game-like example using &lt;strong&gt;structs&lt;/strong&gt;, &lt;strong&gt;methods&lt;/strong&gt;, and &lt;strong&gt;interfaces&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structs: Our data types&lt;/strong&gt;&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;type&lt;/span&gt; &lt;span class="n"&gt;Item&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;X&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Y&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Player&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;Item&lt;/span&gt;          &lt;span class="c"&gt;// Embedded struct (composition)&lt;/span&gt;
    &lt;span class="n"&gt;Keys&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="c"&gt;// Keys collected by the player&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Item&lt;/code&gt; represents a position in a 2D space,&lt;br&gt;
and &lt;code&gt;Player&lt;/code&gt; embeds an &lt;code&gt;Item&lt;/code&gt;, gaining its fields and methods (composition instead of inheritance).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ll give &lt;code&gt;Item&lt;/code&gt; a &lt;code&gt;Move&lt;/code&gt; method:&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deltaX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;deltaY&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;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;deltaX&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Y&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;deltaY&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the receiver:&lt;br&gt;
&lt;code&gt;(i *Item)&lt;/code&gt; means this method belongs to &lt;code&gt;Item&lt;/code&gt; and can modify it (pointer receiver).&lt;/p&gt;

&lt;p&gt;Now both &lt;code&gt;Item&lt;/code&gt; and &lt;code&gt;Player&lt;/code&gt; can move, since &lt;code&gt;Player&lt;/code&gt; embeds &lt;code&gt;Item&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementing the Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remember our interface?&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;type&lt;/span&gt; &lt;span class="n"&gt;Mover&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Move&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="kt"&gt;int&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;Both &lt;code&gt;*Item&lt;/code&gt; and &lt;code&gt;*Player&lt;/code&gt; have a &lt;code&gt;Move&lt;/code&gt; method.&lt;br&gt;
Therefore, &lt;strong&gt;they automatically implement&lt;/strong&gt; &lt;code&gt;Mover&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s use that to our advantage:&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;moveAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;Mover&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dy&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="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;m&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;ms&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="n"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dy&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;moveAll&lt;/code&gt; can move &lt;strong&gt;anything&lt;/strong&gt; that can “move” — whether it’s an &lt;code&gt;Item&lt;/code&gt;, a &lt;code&gt;Player&lt;/code&gt;, or any other type you create later.&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;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&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;Player&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;"Hugo"&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;Item&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;

&lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;Mover&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;i&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;p&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;moveAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&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="s"&gt;"%+v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;%+v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;polymorphism&lt;/strong&gt; in Go — simple, explicit, and fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composition Over Inheritance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go doesn’t have inheritance like Java or C#.&lt;br&gt;
Instead, it promotes &lt;strong&gt;composition&lt;/strong&gt; — combining small, focused types together.&lt;/p&gt;

&lt;p&gt;When you embed one struct inside another, like this:&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;type&lt;/span&gt; &lt;span class="n"&gt;Player&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;Item&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get &lt;strong&gt;field and method&lt;/strong&gt; promotion:&lt;br&gt;
&lt;code&gt;Player&lt;/code&gt; now “has” all the fields and methods of &lt;code&gt;Item&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This approach is cleaner, more flexible, and avoids complex class hierarchies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices for Interfaces&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define interfaces where they’re used&lt;/strong&gt;, not where they’re implemented.&lt;br&gt;
Example: a &lt;code&gt;Mover&lt;/code&gt; interface should live in the game logic package, not inside the &lt;code&gt;Player&lt;/code&gt; struct package.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep interfaces small.&lt;/strong&gt;&lt;br&gt;
The Go standard library averages only 2 methods per interface.&lt;br&gt;
Small interfaces are easier to implement and compose.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Reader&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Read&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="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="kt"&gt;error&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;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don’t overdesign.&lt;/strong&gt;&lt;br&gt;
Avoid creating interfaces “just in case”. Start with concrete types, and extract interfaces later if patterns emerge.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Accept interfaces, return concrete types.&lt;/strong&gt;&lt;br&gt;
This makes your functions flexible but still predictable.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;Mover&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;     &lt;span class="c"&gt;// flexible input&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewPlayer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;Player&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c"&gt;// clear output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to Use Interfaces&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use them when you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Polymorphism&lt;/strong&gt; (different types, same behavior)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mocks for testing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Abstraction&lt;/strong&gt; between packages (reduce coupling)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple implementations&lt;/strong&gt; of a common behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t use them when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You only have &lt;strong&gt;one implementation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You don’t need to abstract the behavior&lt;/li&gt;
&lt;li&gt;You’re just defining data, not behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Recap&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Interface&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A set of method signatures that define behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Implicit implementation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;If a type has all required methods, it automatically implements the interface&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Polymorphism&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Different types can be used interchangeably if they satisfy the same interface&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Composition&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Embedding structs to reuse behavior (Go’s alternative to inheritance)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Best practice&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Keep interfaces small and define them where they’re used&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Interfaces in Go are a perfect reflection of the language’s philosophy:&lt;br&gt;
&lt;strong&gt;simple, explicit, and pragmatic&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They let you focus on what really matters — &lt;strong&gt;behavior&lt;/strong&gt; — without the complexity of inheritance chains or abstract hierarchies.&lt;/p&gt;

&lt;p&gt;Once you understand that interfaces are about &lt;strong&gt;what something can do&lt;/strong&gt;, not &lt;strong&gt;what it is&lt;/strong&gt;, you’ll start writing idiomatic Go code that feels natural and elegant.&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
      <category>programming</category>
      <category>interview</category>
    </item>
    <item>
      <title>Mastering JSON the Go Way</title>
      <dc:creator>Hugo Oliveira</dc:creator>
      <pubDate>Tue, 21 Oct 2025 21:54:46 +0000</pubDate>
      <link>https://forem.com/hugohenrick/mastering-json-the-go-way-1j5f</link>
      <guid>https://forem.com/hugohenrick/mastering-json-the-go-way-1j5f</guid>
      <description>&lt;p&gt;One of the things I love about Go is how practical it is when dealing with JSON.&lt;br&gt;
No complicated setup. No magic. Just clear, predictable code.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;encoding/json&lt;/code&gt; package provides &lt;strong&gt;two main ways&lt;/strong&gt; to work with JSON:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- In-memory (Marshal/Unmarshal)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;- Stream-based (Encoder/Decoder)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s break it down.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;Marshal&lt;/code&gt; and &lt;code&gt;Unmarshal&lt;/code&gt; — for small or simple data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are the most common functions when your JSON fits comfortably in memory.&lt;/p&gt;

&lt;p&gt;▶️ &lt;strong&gt;Go → JSON&lt;/strong&gt; (&lt;code&gt;Marshal&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;You convert a Go struct, map, or slice into JSON.&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;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="s"&gt;`json:"name"`&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"email"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;user&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="s"&gt;"Hugo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hugo@example.com"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Hugo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"hugo@example.com"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;Marshal&lt;/code&gt; when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You already have a Go object in memory.&lt;/li&gt;
&lt;li&gt;You want to send or save it as JSON (HTTP response, file, etc.).&lt;/li&gt;
&lt;li&gt;The data is not too large (fits easily in memory).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;◀️&lt;strong&gt;JSON → Go&lt;/strong&gt; (&lt;code&gt;Unmarshal&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;You convert JSON bytes into a Go struct or 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;jsonData&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;`{"name":"Hugo","email":"hugo@example.com"}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;
&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonData&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;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&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;user&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="c"&gt;// Hugo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;Unmarshal&lt;/code&gt; when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You receive JSON as a string or from a file and want to turn it into a Go type.&lt;/li&gt;
&lt;li&gt;The JSON is fully available in memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Tip&lt;/strong&gt;:&lt;br&gt;
Always pass a pointer to the destination (&lt;code&gt;&amp;amp;user&lt;/code&gt;), so Go can modify the variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; — for streams or large data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When working with &lt;strong&gt;large JSON files&lt;/strong&gt; or &lt;strong&gt;network streams&lt;/strong&gt;, loading everything into memory isn’t efficient.&lt;br&gt;
That’s where &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt; come in — they work with &lt;strong&gt;streams&lt;/strong&gt; (&lt;code&gt;io.Reader&lt;/code&gt; and &lt;code&gt;io.Writer&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;▶️&lt;strong&gt;Go → JSON stream (&lt;code&gt;Encoder&lt;/code&gt;)&lt;/strong&gt;&lt;br&gt;
You write JSON directly to an output, like a file or HTTP response.&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;user&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="s"&gt;"Hugo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hugo@example.com"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;encoder&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetIndent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"  "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// optional, for pretty-print&lt;/span&gt;
&lt;span class="n"&gt;encoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&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;Use &lt;code&gt;Encoder&lt;/code&gt; when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to write JSON directly to a file, socket, or HTTP response.&lt;/li&gt;
&lt;li&gt;You want to avoid keeping the full JSON in memory.&lt;/li&gt;
&lt;li&gt;You’re writing continuous data (e.g., logs, API responses).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;◀️ &lt;strong&gt;JSON stream → Go (&lt;code&gt;Decoder&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You read JSON progressively from an input, like a file or HTTP request body.&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;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user.json"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;
&lt;span class="n"&gt;decoder&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDecoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;decoder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decode&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;user&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;user&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;Use &lt;code&gt;Decoder&lt;/code&gt; when:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’re reading large JSON data.&lt;/li&gt;
&lt;li&gt;The data is coming from a network or file stream.&lt;/li&gt;
&lt;li&gt;You don’t want to load everything at once into memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Direction&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Data Type&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Function&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Best for&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;JSON → Go&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[]byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Unmarshal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Small data in memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Go → JSON&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[]byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Marshal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Small data in memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JSON → Go&lt;/td&gt;
&lt;td&gt;&lt;code&gt;io.Reader&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Decoder&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Large files or streams&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Go → JSON&lt;/td&gt;
&lt;td&gt;&lt;code&gt;io.Writer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Encoder&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Large files or streams&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Go’s &lt;code&gt;encoding/json&lt;/code&gt; package is a perfect example of the language’s philosophy:&lt;br&gt;
&lt;strong&gt;simple, explicit, and reliable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you need something quick and small, use &lt;code&gt;Marshal&lt;/code&gt; and &lt;code&gt;Unmarshal&lt;/code&gt;.&lt;br&gt;
When you’re dealing with streams, files, or large payloads, use &lt;code&gt;Encoder&lt;/code&gt; and &lt;code&gt;Decoder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You get both simplicity and performance, and you always know exactly what your code is doing.&lt;/p&gt;

&lt;p&gt;That’s Go.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>go</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Understanding Strings in Go: Bytes, Runes, and the Truth Behind len</title>
      <dc:creator>Hugo Oliveira</dc:creator>
      <pubDate>Mon, 20 Oct 2025 01:30:01 +0000</pubDate>
      <link>https://forem.com/hugohenrick/understanding-strings-in-go-bytes-runes-and-the-truth-behind-len-3818</link>
      <guid>https://forem.com/hugohenrick/understanding-strings-in-go-bytes-runes-and-the-truth-behind-len-3818</guid>
      <description>&lt;p&gt;When you first start working with Go, strings might seem simple — until you try to count characters, index them, or work with emojis. Then you realize that Go treats strings in a way that’s both elegant and slightly tricky.&lt;/p&gt;

&lt;p&gt;Let’s clear the confusion once and for all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💡 What a string really is&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Go, a string is &lt;strong&gt;an immutable sequence of bytes&lt;/strong&gt;, not a list of characters.&lt;/p&gt;

&lt;p&gt;Internally, it’s represented roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type stringStruct struct {
    Data *byte
    Len  int
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every byte is part of a UTF-8–encoded value. This means that characters like &lt;code&gt;á&lt;/code&gt; or &lt;code&gt;🚀&lt;/code&gt; may use multiple bytes.&lt;/p&gt;

&lt;p&gt;📏 &lt;code&gt;len()&lt;/code&gt; &lt;strong&gt;counts bytes, not characters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This one surprises a lot of newcomers. The &lt;code&gt;len()&lt;/code&gt; function returns the &lt;strong&gt;number of bytes&lt;/strong&gt;, not the number of visible characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s := "Olá"
fmt.Println(len(s)) // 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like three letters, right?&lt;br&gt;
But “á” uses &lt;strong&gt;two bytes&lt;/strong&gt; in UTF-8 (&lt;code&gt;0xC3 0xA1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;🧩 &lt;strong&gt;Accessing by index (&lt;code&gt;s[i]&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you index a string like &lt;code&gt;s[i]&lt;/code&gt;, you get a single byte (&lt;code&gt;uint8&lt;/code&gt;), not a character.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s := "Olá"
fmt.Println(s[2]) // 195 (0xC3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you’re only seeing part of the letter “á”.&lt;br&gt;
Strings in Go are sequences of bytes, not runes.&lt;/p&gt;

&lt;p&gt;🔁 &lt;strong&gt;Iterating with&lt;/strong&gt; &lt;code&gt;for&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are two main ways to loop over strings in Go — and they behave very differently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Byte by byte&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s := "Olá"
for i := 0; i &amp;lt; len(s); i++ {
    fmt.Printf("%d: %x\n", i, s[i])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0: 4f
1: 6c
2: c3
3: a1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each byte of “á” is printed separately (&lt;code&gt;c3&lt;/code&gt; and &lt;code&gt;a1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Rune by rune&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s := "Olá"
for i, r := range s {
    fmt.Printf("%d: %c (%[2]U)\n", i, r)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0: O (U+004F)
1: l (U+006C)
2: á (U+00E1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Go decodes UTF-8 correctly and gives you full Unicode characters.&lt;/p&gt;

&lt;p&gt;⚙️ &lt;code&gt;byte&lt;/code&gt; &lt;strong&gt;vs&lt;/strong&gt; &lt;code&gt;rune&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1 byte (&lt;code&gt;uint8&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Raw UTF-8 data&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'a' = 97&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rune&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4 bytes (&lt;code&gt;int32&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;A full Unicode character&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;'á' = 225&lt;/code&gt;, &lt;code&gt;'🚀' = 128640&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can convert between them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s := "🚀"
b := []byte(s)
r := []rune(s)

fmt.Println(len(b)) // 4 bytes
fmt.Println(len(r)) // 1 rune
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 &lt;strong&gt;Choosing the right type&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Use case&lt;/th&gt;
&lt;th&gt;Recommended type&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;File IO or network data&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[]byte&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Performance and control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Counting or printing characters&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;[]rune&lt;/code&gt; or &lt;code&gt;for range&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Handles Unicode properly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storing text&lt;/td&gt;
&lt;td&gt;&lt;code&gt;string&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Simple, safe, and immutable&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;🧭 &lt;strong&gt;Quick summary&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Returns&lt;/th&gt;
&lt;th&gt;Interprets as&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;len(string)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Number of bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;string[i]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;uint8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Byte value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;for range string&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unicode character&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;[]byte(string)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Slice of bytes&lt;/td&gt;
&lt;td&gt;UTF-8 encoded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;[]rune(string)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Slice of runes&lt;/td&gt;
&lt;td&gt;Unicode code points&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;💬 &lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go treats strings as &lt;strong&gt;byte sequences&lt;/strong&gt; for a reason. It keeps things fast, memory-safe, and predictable.&lt;br&gt;
But once you understand the difference between &lt;strong&gt;bytes&lt;/strong&gt; and &lt;strong&gt;runes&lt;/strong&gt;, you unlock the full power of Go’s simplicity.&lt;/p&gt;

&lt;p&gt;Next time you work with text in Go, remember:&lt;br&gt;
&lt;code&gt;len()&lt;/code&gt; doesn’t count letters. It counts bytes.&lt;br&gt;
And that tiny detail makes all the difference.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why Choose Go for Development?</title>
      <dc:creator>Hugo Oliveira</dc:creator>
      <pubDate>Mon, 20 Oct 2025 01:00:16 +0000</pubDate>
      <link>https://forem.com/hugohenrick/why-choose-go-for-development-3kj0</link>
      <guid>https://forem.com/hugohenrick/why-choose-go-for-development-3kj0</guid>
      <description>&lt;p&gt;When I first discovered Go, it instantly felt different. It wasn’t trying to be clever or overloaded with features. It was clean, fast, and built with a purpose.&lt;/p&gt;

&lt;p&gt;Go was created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. They wanted a language that combined Python’s simplicity with C’s performance and reliability. Something that made sense for building real-world, large-scale systems.&lt;/p&gt;

&lt;p&gt;Today, Go stands out as one of the most pragmatic and enjoyable languages to work with. It is minimal, opinionated, and focused on what actually matters: clarity and performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚙️ A language that helps you build better&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go makes you a better developer because it forces you to think clearly. It doesn’t hide complexity behind abstractions. Instead, it gives you the right tools to write code that is easy to understand, test, and maintain.&lt;/p&gt;

&lt;p&gt;Some of the things that make Go so good:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Static Types&lt;/strong&gt; that catch mistakes before they reach production&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Garbage Collector&lt;/strong&gt; that handles memory efficiently&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rich Standard Library&lt;/strong&gt; with everything you need out of the box&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast Compilation&lt;/strong&gt; that keeps your feedback loop short&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit Error Handling&lt;/strong&gt; that makes you deal with problems, not ignore them&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Native Concurrency&lt;/strong&gt; with goroutines and channels that just work&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-compilation&lt;/strong&gt; to build for any OS easily&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built-in Tooling&lt;/strong&gt; like go fmt, go test, and go mod that keep your workflow clean&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt; that encourages readable, maintainable, and long-lasting code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🚀 A community and ecosystem that inspire&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go powers some of the biggest tools in modern development. Docker, Kubernetes, Grafana, Prometheus, and Cloudflare all rely on it.&lt;/p&gt;

&lt;p&gt;The community is one of the most welcoming and practical I have seen. It is full of developers who care about writing clean, maintainable software. Every library or project you find reflects that same spirit of simplicity and reliability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💡 Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go is not just another programming language. It is a mindset. It is about doing more with less. It is about choosing simplicity over cleverness and clarity over chaos.&lt;/p&gt;

&lt;p&gt;For me, Go represents what modern software development should feel like: fast, predictable, and deeply satisfying.&lt;/p&gt;

&lt;p&gt;If you care about building systems that scale, perform, and stay maintainable over time, Go is a language that will make you fall in love with development again.&lt;/p&gt;

</description>
      <category>go</category>
      <category>performance</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
