<?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: Andi</title>
    <description>The latest articles on DEV Community by Andi (@andidev30).</description>
    <link>https://dev.to/andidev30</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%2F523455%2F47425798-358f-4b33-8e76-825120030d26.jpeg</url>
      <title>DEV Community: Andi</title>
      <link>https://dev.to/andidev30</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andidev30"/>
    <language>en</language>
    <item>
      <title>Go (Golang) Basic (Bonus) Three Advanced Function Techniques</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:54:16 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-bonus-three-advanced-function-techniques-57l7</link>
      <guid>https://dev.to/andidev30/go-golang-basic-bonus-three-advanced-function-techniques-57l7</guid>
      <description>&lt;h2&gt;
  
  
  Leveling Up Your Function Game
&lt;/h2&gt;

&lt;p&gt;Welcome to a bonus installment of our Go series! You've already mastered the fundamentals of creating functions, but Go has a few more powerful tricks up its sleeve.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore three advanced techniques that can make your code more flexible and elegant:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Variadic Functions&lt;/strong&gt;: Functions that can accept any number of arguments.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Anonymous Functions&lt;/strong&gt;: Functions without a name.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Recursive Functions&lt;/strong&gt;: Functions that call themselves.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Variadic Functions (Accepting Many Arguments)
&lt;/h2&gt;

&lt;p&gt;Have you ever used &lt;code&gt;fmt.Println()&lt;/code&gt; and noticed you can pass as many arguments as you want?&lt;br&gt;
&lt;code&gt;fmt.Println("a", "b", "c")&lt;/code&gt; works, and so does &lt;code&gt;fmt.Println("just one")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is possible because of &lt;strong&gt;variadic functions&lt;/strong&gt;. A variadic function can accept a variable number of arguments for its last parameter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A shopping cart. You can put one item, ten items, or a hundred items into it. The cart (&lt;code&gt;function&lt;/code&gt;) can handle it. &lt;/p&gt;
&lt;h3&gt;
  
  
  How to Create a Variadic Function
&lt;/h3&gt;

&lt;p&gt;You create one by adding &lt;code&gt;...&lt;/code&gt; before the type of the final parameter. Inside the function, that parameter is treated as a &lt;strong&gt;slice&lt;/strong&gt; of its type.&lt;/p&gt;
&lt;h4&gt;
  
  
  Code Example: Sum All Numbers
&lt;/h4&gt;

&lt;p&gt;Let's create a function that can sum any amount of 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;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 '...int' tells Go that 'numbers' can be zero or more ints.&lt;/span&gt;
&lt;span class="c"&gt;// Inside the function, 'numbers' is a slice: []int&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sumAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&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="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="c"&gt;// We can loop over it like any other slice&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;number&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;numbers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total&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;// We can call it with multiple arguments&lt;/span&gt;
    &lt;span class="n"&gt;total1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sumAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&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;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;"Total 1:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: 30&lt;/span&gt;

    &lt;span class="c"&gt;// Or with a different number of arguments&lt;/span&gt;
    &lt;span class="n"&gt;total2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sumAll&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="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="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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Total 2:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: 25&lt;/span&gt;

    &lt;span class="c"&gt;// We can also pass a slice, but we must "spread" it with '...'&lt;/span&gt;
    &lt;span class="n"&gt;mySlice&lt;/span&gt; &lt;span class="o"&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="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&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;total3&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sumAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mySlice&lt;/span&gt;&lt;span class="o"&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;"Total 3:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: 60&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Anonymous Functions (Functions Without a Name)
&lt;/h2&gt;

&lt;p&gt;Just as the name suggests, an &lt;strong&gt;anonymous function&lt;/strong&gt; is a function that is declared without a name. So why would you want this? They are extremely useful for short, one-off tasks where defining a full, named function would be overkill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A disposable, single-use tool. You use it for one specific job and then throw it away without needing to store it in your toolbox. &lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use Anonymous Functions
&lt;/h3&gt;

&lt;p&gt;You can assign an anonymous function to a variable, or pass it directly as an argument to another function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Code Example
&lt;/h4&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;// 1. Assigning an anonymous function to a variable&lt;/span&gt;
    &lt;span class="c"&gt;// This function has no name, but we can call it using the 'greet' variable&lt;/span&gt;
    &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;func&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;"Hello from an anonymous function!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// Call it like a regular function&lt;/span&gt;

    &lt;span class="c"&gt;// 2. A more practical use: a self-executing anonymous function&lt;/span&gt;
    &lt;span class="c"&gt;// Notice the () at the end, which executes the function immediately&lt;/span&gt;
    &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="kt"&gt;string&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="s"&gt;"This is a one-time message!"&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;h2&gt;
  
  
  3. Recursive Functions (Functions That Call Themselves)
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;recursive function&lt;/strong&gt; is a function that calls itself to solve a problem. This is a powerful technique for problems that can be broken down into smaller, self-similar sub-problems.&lt;/p&gt;

&lt;p&gt;Every recursive function must have two parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;A Base Case&lt;/strong&gt;: A condition that stops the recursion. Without this, you'll have an infinite loop!&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;A Recursive Step&lt;/strong&gt;: The part where the function calls itself, usually with a modified argument that brings it closer to the base case.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Russian nesting dolls. Each doll contains a smaller, identical version of itself, until you reach the final, solid doll (the base case).&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example: Factorial
&lt;/h3&gt;

&lt;p&gt;Calculating a factorial (like &lt;code&gt;5! = 5 * 4 * 3 * 2 * 1&lt;/code&gt;) is a classic recursion problem.&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;// Calculates n!&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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="c"&gt;// 1. The Base Case: If n is 0 or 1, we stop.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// 2. The Recursive Step: n * factorial of (n-1)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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="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;// The call stack will look like:&lt;/span&gt;
    &lt;span class="c"&gt;// 5 * factorial(4)&lt;/span&gt;
    &lt;span class="c"&gt;// 5 * (4 * factorial(3))&lt;/span&gt;
    &lt;span class="c"&gt;// 5 * (4 * (3 * factorial(2)))&lt;/span&gt;
    &lt;span class="c"&gt;// 5 * (4 * (3 * (2 * factorial(1))))&lt;/span&gt;
    &lt;span class="c"&gt;// 5 * (4 * (3 * (2 * 1)))&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;factorial&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"5! is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: 120&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;And there you have it! Three advanced function techniques that add a whole new level of flexibility and power to your Go programming.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Variadic Functions&lt;/strong&gt; let you handle an unknown number of inputs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anonymous Functions&lt;/strong&gt; are perfect for quick, inline logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recursive Functions&lt;/strong&gt; provide an elegant solution for problems that can be broken down into smaller pieces.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While you might not use these every single day, knowing they exist in your toolbox will make you a more capable and effective Gopher.&lt;/p&gt;

&lt;p&gt;Thanks for reading this bonus article!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Structuring Project with Packages</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:49:13 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-structuring-project-with-packages-2e3c</link>
      <guid>https://dev.to/andidev30/go-golang-basic-structuring-project-with-packages-2e3c</guid>
      <description>&lt;h2&gt;
  
  
  From a Single File to a Real Project
&lt;/h2&gt;

&lt;p&gt;Welcome to the final part of our "Complete Guide to Go Basics" series! So far, all of our code has lived in a single file: &lt;code&gt;main.go&lt;/code&gt;. This is fine for small programs, but as your projects grow, you'll need a way to organize your code into logical, reusable pieces.&lt;/p&gt;

&lt;p&gt;In Go, the solution is &lt;strong&gt;Packages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Think of packages like folders on your computer. You don't put all your documents, photos, and music into one giant folder. You organize them into separate, dedicated folders. Packages do the same for your code. &lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is a Package?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;package&lt;/strong&gt; is simply a directory or folder containing one or more Go files that all belong together. Every Go file must declare which package it belongs to at the very top of the file (e.g., &lt;code&gt;package main&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Let's create our own package. Imagine we want to build a simple calculator utility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create the Package Directory
&lt;/h3&gt;

&lt;p&gt;Inside your project folder (e.g., &lt;code&gt;my-go-project&lt;/code&gt;), create a new sub-folder named &lt;code&gt;calculator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your project structure should now look 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;my-go-project/
├── go.mod
├── main.go
└── calculator/  \&amp;lt;-- Our new package folder

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Create a File in the Package
&lt;/h3&gt;

&lt;p&gt;Inside the &lt;code&gt;calculator&lt;/code&gt; folder, create a new file named &lt;code&gt;add.go&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Write the Package Code
&lt;/h3&gt;

&lt;p&gt;Open &lt;code&gt;calculator/add.go&lt;/code&gt; and add the following code. Notice the first line!&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="c"&gt;// This file belongs to the 'calculator' package&lt;/span&gt;
&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;

&lt;span class="c"&gt;// Add is a function that takes two integers and returns their sum.&lt;/span&gt;
&lt;span class="c"&gt;// We'll discuss why 'A' is capitalized in a moment.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;b&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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;Key Rule&lt;/strong&gt;: All &lt;code&gt;.go&lt;/code&gt; files inside the &lt;code&gt;calculator&lt;/code&gt; directory must start with &lt;code&gt;package calculator&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Importing and Using Your Package
&lt;/h2&gt;

&lt;p&gt;Now that we have our &lt;code&gt;calculator&lt;/code&gt; package, how do we use the &lt;code&gt;Add&lt;/code&gt; function from our &lt;code&gt;main.go&lt;/code&gt; file? We use the &lt;code&gt;import&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Open your &lt;code&gt;main.go&lt;/code&gt; file and modify it to look 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;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="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"my-first-go-project/calculator"&lt;/span&gt; &lt;span class="c"&gt;// Import our custom package&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;// Call the Add function from the calculator package&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&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;3&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;"The sum is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: The sum is: 8&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;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We import our package using its full path: &lt;code&gt;&amp;lt;module_name&amp;gt;/&amp;lt;package_name&amp;gt;&lt;/code&gt;. The module name is the one you defined in your &lt;code&gt;go.mod&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;To use the function, we prefix it with the package name: &lt;code&gt;calculator.Add()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Go's Simple Visibility Rule: The Capital Letter
&lt;/h2&gt;

&lt;p&gt;You might have noticed that we named our function &lt;code&gt;Add&lt;/code&gt; with a capital &lt;code&gt;A&lt;/code&gt;. This was intentional and is one of the most elegant features of Go.&lt;/p&gt;

&lt;p&gt;In Go, there is no &lt;code&gt;public&lt;/code&gt; or &lt;code&gt;private&lt;/code&gt; keyword. Visibility is determined by a simple rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;If a name (function, struct, variable, etc.) starts with a capital letter, it is Public (Exported).&lt;/strong&gt; This means it can be accessed from other packages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If a name starts with a lowercase letter, it is Private (unexported).&lt;/strong&gt; This means it can only be accessed by code within the same package.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A restaurant.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public (Capital Letter)&lt;/strong&gt;: The &lt;strong&gt;Menu&lt;/strong&gt; given to customers. Anyone can see it and order from it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private (lowercase letter)&lt;/strong&gt;: The &lt;strong&gt;secret recipe&lt;/strong&gt; in the kitchen. Only the chefs (code in the same package) can access it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's prove this. Add a new &lt;code&gt;subtract&lt;/code&gt; function with a lowercase 's' to your &lt;code&gt;calculator/add.go&lt;/code&gt; file:&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="c"&gt;// in calculator/add.go&lt;/span&gt;

&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;b&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// 's' is lowercase, so this function is private&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;b&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, try to call &lt;code&gt;subtract&lt;/code&gt; from &lt;code&gt;main.go&lt;/code&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="c"&gt;// in main.go&lt;/span&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="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"my-first-go-project/calculator"&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;sum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&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;3&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;"The sum is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// This line will cause a compilation error!&lt;/span&gt;
    &lt;span class="c"&gt;// diff := calculator.subtract(5, 3) &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to run this, Go will give you an error saying &lt;code&gt;cannot refer to unexported name calculator.subtract&lt;/code&gt;. This is Go's way of protecting your private code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: You've Built a Strong Foundation!
&lt;/h2&gt;

&lt;p&gt;Congratulations! You have officially reached the end of our "Complete Guide to Go Basics" series. You've journeyed from a simple "Hello, World!" to understanding some of Go's most powerful and unique features.&lt;/p&gt;

&lt;p&gt;Let's recap what you've mastered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Basics&lt;/strong&gt;: Variables, data types, and collections like slices and maps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control Flow&lt;/strong&gt;: Making decisions with &lt;code&gt;if/switch&lt;/code&gt; and repeating tasks with &lt;code&gt;for&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Structure&lt;/strong&gt;: Organizing logic into &lt;code&gt;functions&lt;/code&gt;, creating custom types with &lt;code&gt;structs&lt;/code&gt;, and attaching behavior with &lt;code&gt;methods&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advanced Concepts&lt;/strong&gt;: Using &lt;code&gt;pointers&lt;/code&gt; for efficiency, handling &lt;code&gt;errors&lt;/code&gt; gracefully, and now, structuring your project with &lt;code&gt;packages&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You now have a very solid foundation. The Go world is vast, and your next steps could be exploring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Goroutines and Channels&lt;/strong&gt;: Go's famous approach to concurrency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Standard Library&lt;/strong&gt;: Discovering Go's rich set of built-in packages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Building a Project&lt;/strong&gt;: Creating your first web API or command-line tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for following along with this series. I hope it has been a helpful and enjoyable journey. Happy coding!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Error</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:43:54 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-error-3dkl</link>
      <guid>https://dev.to/andidev30/go-golang-basic-error-3dkl</guid>
      <description>&lt;h2&gt;
  
  
  What Happens When Things Go Wrong?
&lt;/h2&gt;

&lt;p&gt;We've learned to write functions, create structs, and use pointers. Our code works perfectly... in a perfect world. But in the real world, things go wrong: a file might not exist, a network connection might fail, or a user might provide invalid input.&lt;/p&gt;

&lt;p&gt;Many languages handle these situations with "exceptions" using &lt;code&gt;try-catch&lt;/code&gt; blocks. Go takes a fundamentally different and more explicit approach.&lt;/p&gt;

&lt;p&gt;In Go, &lt;strong&gt;errors are values&lt;/strong&gt;. An error is not a special event that stops your program; it's just another value that a function can return, like a &lt;code&gt;string&lt;/code&gt; or an &lt;code&gt;int&lt;/code&gt;. This philosophy forces you to consciously handle potential failures, leading to more robust and predictable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The &lt;code&gt;error&lt;/code&gt; Type: Go's Built-in Contract
&lt;/h2&gt;

&lt;p&gt;At its core, Go's &lt;code&gt;error&lt;/code&gt; is a built-in &lt;strong&gt;interface&lt;/strong&gt;. It's a very simple contract: any type that wants to be considered an &lt;code&gt;error&lt;/code&gt; must have a single method called &lt;code&gt;Error()&lt;/code&gt; that returns a string.&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="kt"&gt;error&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;Error&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't usually need to create your own error types from scratch, but understanding this helps you see why the system is so flexible.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Idiomatic Go Pattern: &lt;code&gt;if err != nil&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Because errors are just values, the standard way to handle them is to check if an error was returned from a function. This leads to the most common pattern you will see in any Go codebase: &lt;code&gt;if err != nil&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A function that can fail will typically return two values: &lt;strong&gt;&lt;code&gt;(the result, an error)&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A service technician's report. When the job is done, you get two things: the fixed appliance (the result) and a report slip (the error).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the job was successful, the report slip is &lt;strong&gt;blank&lt;/strong&gt; (&lt;code&gt;nil&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;If something went wrong, the report slip has a &lt;strong&gt;description of the problem&lt;/strong&gt; (&lt;code&gt;not nil&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Pattern in Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// A conceptual example&lt;/span&gt;
&lt;span class="n"&gt;result&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;someFunctionThatCanFail&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="c"&gt;// An error occurred! Handle it here.&lt;/span&gt;
    &lt;span class="c"&gt;// For now, we can just print it and stop.&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;"An error happened:"&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="k"&gt;return&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// If the code reaches this point, it means 'err' was nil.&lt;/span&gt;
&lt;span class="c"&gt;// It is now safe to use the 'result'.&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;"Success! The result is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Creating and Returning Errors
&lt;/h2&gt;

&lt;p&gt;Now that we know how to check for errors, how do we create our own? Go's built-in &lt;code&gt;errors&lt;/code&gt; package makes this very simple.&lt;/p&gt;

&lt;p&gt;Let's build a practical example: a safe division function that returns an error if you try to divide by zero.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&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="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"errors"&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// This function returns two values: a float64 AND an error&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float64&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;span class="c"&gt;// If the divisor is zero, it's a predictable failure&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&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;// We return a zero value for the number and a new error message&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cannot divide by zero"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// If everything is okay, we return the result and 'nil' for the error&lt;/span&gt;
    &lt;span class="c"&gt;// 'nil' is Go's way of saying "nothing" or "no error"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&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;// --- Successful case ---&lt;/span&gt;
    &lt;span class="n"&gt;result&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2.0&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="c"&gt;// This block is skipped because 'err' is nil&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;"Error:"&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="k"&gt;else&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;"Result 1:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// --- Failure case ---&lt;/span&gt;
    &lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10.0&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err2&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="c"&gt;// This block runs because an error was returned&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;"Error:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;"Result 2:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result2&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;This pattern of returning &lt;code&gt;(result, nil)&lt;/code&gt; on success and &lt;code&gt;(zero-value, error)&lt;/code&gt; on failure is fundamental to writing clean and robust Go code.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;code&gt;error&lt;/code&gt; vs. &lt;code&gt;panic&lt;/code&gt;: A Crucial Distinction
&lt;/h2&gt;

&lt;p&gt;Beginners often wonder when to return an &lt;code&gt;error&lt;/code&gt; and when to use &lt;code&gt;panic&lt;/code&gt;. The difference is a matter of expectation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Think of your program as a car journey.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;error&lt;/code&gt;&lt;/strong&gt;: This is like a &lt;strong&gt;"Check Engine"&lt;/strong&gt; light.  It's an expected problem. You can see the warning, decide to pull over, and handle it. The journey can potentially continue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;panic&lt;/code&gt;&lt;/strong&gt;: This is like the &lt;strong&gt;engine suddenly exploding&lt;/strong&gt;.  This is a catastrophic, unexpected failure that indicates something is deeply wrong with the car (a bug in your code). The journey stops immediately.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use this as your guide:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;error&lt;/code&gt;&lt;/strong&gt; for expected failures that are a normal part of your program's operation (e.g., invalid user input, file not found, network timeout).&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;panic&lt;/code&gt;&lt;/strong&gt; very rarely, and only for truly exceptional situations that indicate a programmer error or a state that should be impossible to reach.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Go's approach to error handling is one of its most defining features. By treating errors as regular values, it encourages you to build resilient and predictable applications.&lt;/p&gt;

&lt;p&gt;You've now learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The philosophy that "errors are values".&lt;/li&gt;
&lt;li&gt;The standard &lt;code&gt;if err != nil&lt;/code&gt; pattern for checking errors.&lt;/li&gt;
&lt;li&gt;How to create and return your own errors.&lt;/li&gt;
&lt;li&gt;The critical difference between a manageable &lt;code&gt;error&lt;/code&gt; and a fatal &lt;code&gt;panic&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the final part of our foundational series, we'll learn how to structure larger projects by organizing our code into &lt;strong&gt;Packages&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Interfaces</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:41:00 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-interfaces-55d</link>
      <guid>https://dev.to/andidev30/go-golang-basic-interfaces-55d</guid>
      <description>&lt;h2&gt;
  
  
  What Comes After Custom Types?
&lt;/h2&gt;

&lt;p&gt;In the last few parts, we've learned how to create our own data types with &lt;code&gt;structs&lt;/code&gt; and attach behavior to them with &lt;code&gt;methods&lt;/code&gt;. This is great for creating organized, self-contained components.&lt;/p&gt;

&lt;p&gt;But what if we want to write a function that can work with &lt;em&gt;different&lt;/em&gt; types, as long as they share a common behavior? For example, a function that can process anything that can be "saved" to a file, whether it's a &lt;code&gt;User&lt;/code&gt; struct, a &lt;code&gt;Product&lt;/code&gt; struct, or an &lt;code&gt;Invoice&lt;/code&gt; struct.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;Interfaces&lt;/strong&gt; come in. An interface is one of Go's most powerful features for writing flexible and abstract code.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is an Interface? (The Contract)
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;interface&lt;/strong&gt; in Go is a type that defines a set of method signatures. It's not a piece of data; it's a &lt;strong&gt;contract&lt;/strong&gt;. It says, "Any type that wants to be considered a member of my group &lt;em&gt;must&lt;/em&gt; have these specific methods."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: An electrical wall socket (stopkontak).  The socket is the interface. It has a contract: "Anything that wants to get power from me must have a two-pronged plug that fits my shape." The socket doesn't care if you plug in a phone charger, a fan, or a TV. As long as the device's plug matches the contract, it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining an Interface
&lt;/h3&gt;

&lt;p&gt;Let's define a simple interface for anything that can make a sound.&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;// This is our contract.&lt;/span&gt;
&lt;span class="c"&gt;// Any type that wants to be a "SoundMaker" MUST have a method&lt;/span&gt;
&lt;span class="c"&gt;// called MakeSound() that returns a string.&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;SoundMaker&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;MakeSound&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Implementing an Interface (Implicitly)
&lt;/h2&gt;

&lt;p&gt;Here's a key difference between Go and other languages like Java or C#: &lt;strong&gt;you don't explicitly say that your type implements an interface.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A type in Go &lt;em&gt;implicitly&lt;/em&gt; satisfies an interface if it defines all the methods specified in that interface. No &lt;code&gt;implements&lt;/code&gt; keyword needed.&lt;/p&gt;

&lt;p&gt;Let's create two different types that will satisfy our &lt;code&gt;SoundMaker&lt;/code&gt; contract.&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="c"&gt;// (Continuing from the code above)&lt;/span&gt;

&lt;span class="c"&gt;// --- First Type: Dog ---&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Dog&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="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Dog now satisfies the SoundMaker interface because it has the MakeSound() method.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MakeSound&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Woof!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// --- Second Type: Cat ---&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Cat&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="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Cat also satisfies the SoundMaker interface.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MakeSound&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Meow!"&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;// We'll see how to use these in the next step.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; are completely different structs, Go considers both of them to be &lt;code&gt;SoundMaker&lt;/code&gt;s because they both fulfill the contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Using the Interface (The Payoff)
&lt;/h2&gt;

&lt;p&gt;Now for the magic. We can create a function that accepts our &lt;code&gt;SoundMaker&lt;/code&gt; interface as a parameter. This function doesn't know or care whether it's receiving a &lt;code&gt;Dog&lt;/code&gt; or a &lt;code&gt;Cat&lt;/code&gt;. It only knows that whatever it receives, it's guaranteed to have a &lt;code&gt;MakeSound()&lt;/code&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Flexible Function
&lt;/h3&gt;

&lt;p&gt;Let's create a function that makes any &lt;code&gt;SoundMaker&lt;/code&gt; speak.&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="c"&gt;// (Continuing from the code above)&lt;/span&gt;

&lt;span class="c"&gt;// This function can accept ANY type that satisfies the SoundMaker interface.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;letThemSpeak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;SoundMaker&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;"The sound is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MakeSound&lt;/span&gt;&lt;span class="p"&gt;())&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;// Create an instance of Dog and Cat&lt;/span&gt;
    &lt;span class="n"&gt;myDog&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Dog&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;"Buddy"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;myCat&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Cat&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;"Whiskers"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// We can pass both myDog and myCat to the same function!&lt;/span&gt;
    &lt;span class="n"&gt;letThemSpeak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myDog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: The sound is: Woof!&lt;/span&gt;
    &lt;span class="n"&gt;letThemSpeak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myCat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: The sound is: Meow!&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 incredibly powerful. It allows us to write generic functions that are decoupled from specific data types, making our code much more reusable and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting It All Together: A Full Example
&lt;/h2&gt;

&lt;p&gt;Here is the complete, runnable program that demonstrates everything we've discussed.&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;// 1. The contract (the interface)&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;SoundMaker&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;MakeSound&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="c"&gt;// 2. The first type that satisfies the contract&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Dog&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MakeSound&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Woof!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// 3. The second type that also satisfies the contract&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Cat&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MakeSound&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Meow!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// 4. The flexible function that works with any SoundMaker&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;letThemSpeak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;SoundMaker&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;"The sound is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MakeSound&lt;/span&gt;&lt;span class="p"&gt;())&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;// Create instances of our concrete types&lt;/span&gt;
    &lt;span class="n"&gt;myDog&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Dog&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;"Buddy"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;myCat&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Cat&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;"Whiskers"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Call the same function with different types&lt;/span&gt;
    &lt;span class="n"&gt;letThemSpeak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myDog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;letThemSpeak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myCat&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Interfaces are a core concept in Go for writing clean, abstract, and testable code. By defining contracts (interfaces) instead of depending on concrete types (structs), you can build systems that are much more flexible and scalable.&lt;/p&gt;

&lt;p&gt;You've now learned how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define an interface as a set of method signatures.&lt;/li&gt;
&lt;li&gt;Implicitly satisfy an interface by implementing its methods on a struct.&lt;/li&gt;
&lt;li&gt;Write generic functions that operate on interfaces, not specific types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next part, we'll cover another of Go's most defining features: its elegant approach to &lt;strong&gt;Error Handling&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Pointers</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:37:39 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-pointers-24j7</link>
      <guid>https://dev.to/andidev30/go-golang-basic-pointers-24j7</guid>
      <description>&lt;h2&gt;
  
  
  One of Go's Most Feared (and Powerful) Concepts
&lt;/h2&gt;

&lt;p&gt;Welcome back! In our journey so far, we've learned how to create our own data types with structs and attach behavior with methods. Now, we're going to tackle a topic that can seem intimidating at first but is absolutely essential for writing effective Go code: &lt;strong&gt;Pointers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Don't worry. We'll break it down with a simple analogy that makes it easy to understand. The core idea is to understand the difference between giving someone a &lt;em&gt;copy&lt;/em&gt; of your data versus giving them &lt;em&gt;access&lt;/em&gt; to the original.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Go's Default Behavior: Pass-by-Value (The Photocopy)
&lt;/h2&gt;

&lt;p&gt;By default, whenever you pass data to a function in Go (including structs), Go makes a &lt;strong&gt;copy&lt;/strong&gt; of that data. The function then works on the copy, leaving the original untouched.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: You have an important document. If you give a &lt;strong&gt;photocopy&lt;/strong&gt; to a colleague and they scribble all over it, your original document remains clean and unchanged. &lt;/p&gt;

&lt;h3&gt;
  
  
  A Code Example That Doesn't Work as Expected
&lt;/h3&gt;

&lt;p&gt;Let's try to write a method that updates a user's email.&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;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;Email&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;// This method receives a COPY of the User&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&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;updateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newEmail&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newEmail&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;"Inside method, email is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;)&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;// Create a user&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="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"budi.lama@email.com"&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;"Before method call, email is:"&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;Email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// We call the method to update the email&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;updateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"budi.baru@email.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Let's check the original user's email again&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 method call, email is:"&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;Email&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;When you run this, you'll see this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Before method call, email is: budi.lama@email.com
Inside method, email is: budi.baru@email.com
After method call, email is: budi.lama@email.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the original &lt;code&gt;user&lt;/code&gt;'s email &lt;strong&gt;did not change!&lt;/strong&gt; This is because the &lt;code&gt;updateEmail&lt;/code&gt; method worked on a photocopy, not the original.&lt;/p&gt;

&lt;p&gt;So, how do we modify the original? With pointers.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Solution: Pass-by-Reference with Pointers
&lt;/h2&gt;

&lt;p&gt;To solve our problem, we need to stop sending a photocopy and instead send the &lt;strong&gt;address&lt;/strong&gt; of the original document. In Go, this "address" is called a &lt;strong&gt;pointer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A pointer doesn't hold the data itself, but it holds the memory location &lt;em&gt;where the data lives&lt;/em&gt;. When a function receives a pointer, it knows where to find the original data and can modify it directly.&lt;/p&gt;

&lt;p&gt;We use two special symbols for this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;*&lt;/code&gt; (The Star/Asterisk)&lt;/strong&gt;: When used in a type declaration like &lt;code&gt;*User&lt;/code&gt;, it means "a pointer to a User".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;amp;&lt;/code&gt; (The Ampersand)&lt;/strong&gt;: When used in front of a variable like &lt;code&gt;&amp;amp;user&lt;/code&gt;, it means "get the memory address of this user".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. The Most Important Use Case: Pointer Receivers
&lt;/h2&gt;

&lt;p&gt;The most common and important place you'll use pointers is on &lt;strong&gt;method receivers&lt;/strong&gt;. By adding a &lt;code&gt;*&lt;/code&gt; to the receiver, you tell Go that this method should work on the &lt;em&gt;original&lt;/em&gt; struct instance, not a copy.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Corrected Code Example
&lt;/h3&gt;

&lt;p&gt;Let's fix our previous example by adding one character: a &lt;code&gt;*&lt;/code&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;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;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;Email&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;// This method now receives a POINTER to the User (*User)&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&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;updateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newEmail&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newEmail&lt;/span&gt; &lt;span class="c"&gt;// This now modifies the original user's email&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;// Create a user&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="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"budi.lama@email.com"&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;"Before method call, email is:"&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;Email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// When you call a method with a pointer receiver on a variable,&lt;/span&gt;
    &lt;span class="c"&gt;// Go automatically handles sending the address for you (it's like implicitly doing `(&amp;amp;user).updateEmail(...)`)&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;updateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"budi.baru@email.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Let's check the original user's email again&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 method call, email is:"&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;Email&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, the output is exactly what we want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Before method call, email is: budi.lama@email.com
After method call, email is: budi.baru@email.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success! The original &lt;code&gt;user&lt;/code&gt; struct was modified because the method received a pointer to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use pointer receivers?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;To Modify Data&lt;/strong&gt;: As we just saw, it's the only way for a method to change the original struct's values.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;For Performance&lt;/strong&gt;: It avoids copying large structs every time a method is called, making your program faster and more memory-efficient.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Pointers might seem complex, but their main purpose is simple: to give functions and methods direct access to modify original data.&lt;/p&gt;

&lt;p&gt;Here's a golden rule for you as a beginner:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;When you write methods for a struct, always use a pointer receiver (&lt;code&gt;func (s *MyStruct) ...&lt;/code&gt;).&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the standard practice in Go and will save you from many common bugs and performance issues.&lt;/p&gt;

&lt;p&gt;In the next part, we'll explore another powerful concept for writing flexible code: &lt;strong&gt;Interfaces&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programmers</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Structs and Methods</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:29:23 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-structs-and-methods-1894</link>
      <guid>https://dev.to/andidev30/go-golang-basic-structs-and-methods-1894</guid>
      <description>&lt;h2&gt;
  
  
  Beyond Strings and Integers
&lt;/h2&gt;

&lt;p&gt;So far, we've worked with Go's built-in data types like &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, and &lt;code&gt;bool&lt;/code&gt;. These are great, but what if you want to represent something more complex, like a user, a product, or a car? A user isn't just a string; they have a name, an email, an age, and so on.&lt;/p&gt;

&lt;p&gt;To handle this, Go allows us to define our own custom data types using &lt;strong&gt;Structs&lt;/strong&gt;. We can then attach functions to these structs, which are called &lt;strong&gt;Methods&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is a Struct? (The Blueprint)
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;struct&lt;/strong&gt; (short for structure) is a collection of fields that defines a new data type. It's like creating a blueprint for your data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A blank biodata form. The form has predefined fields like "Name," "Address," and "Age," but it doesn't contain any actual information yet. It's just the template. &lt;/p&gt;

&lt;h3&gt;
  
  
  Defining a Struct
&lt;/h3&gt;

&lt;p&gt;We use the &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;struct&lt;/code&gt; keywords to define a new struct. Let's create one for a &lt;code&gt;User&lt;/code&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;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;// This is our blueprint for a User.&lt;/span&gt;
&lt;span class="c"&gt;// The fields (Name, Email, Age) start with a capital letter&lt;/span&gt;
&lt;span class="c"&gt;// to make them public (we'll cover this later).&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;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Age&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;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;// We'll create a user here in the next step&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Creating Data from a Struct (The Instance)
&lt;/h2&gt;

&lt;p&gt;Once we have the blueprint, we can create actual data from it. An individual piece of data created from a struct is called an &lt;strong&gt;instance&lt;/strong&gt; or an &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We create an instance and access its fields using the dot &lt;code&gt;.&lt;/code&gt; notation.&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="c"&gt;// (Continuing from the code above)&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 our User struct&lt;/span&gt;
    &lt;span class="n"&gt;user1&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;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"budi@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Age&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Creating another instance&lt;/span&gt;
    &lt;span class="n"&gt;user2&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;"Siti"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"siti@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Age&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Accessing the fields&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;"User 1's Name:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user1&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="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;"User 2's Email:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&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;This is far more organized than managing three separate variables (&lt;code&gt;userName&lt;/code&gt;, &lt;code&gt;userEmail&lt;/code&gt;, &lt;code&gt;userAge&lt;/code&gt;) for each user!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Methods: Giving Your Structs Behavior
&lt;/h2&gt;

&lt;p&gt;Now that we have a blueprint for our data (&lt;code&gt;struct&lt;/code&gt;), we can define actions or behaviors that this data can perform. In Go, a function that is attached to a specific struct is called a &lt;strong&gt;Method&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: If a &lt;code&gt;Car&lt;/code&gt; struct holds data like &lt;code&gt;Color&lt;/code&gt; and &lt;code&gt;Speed&lt;/code&gt;, its methods would be actions like &lt;code&gt;StartEngine()&lt;/code&gt; or &lt;code&gt;Accelerate()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining and Using a Method
&lt;/h3&gt;

&lt;p&gt;A method is defined almost exactly like a function, but with one special addition: the &lt;strong&gt;receiver&lt;/strong&gt;. The receiver is what links the function to the 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="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;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Age&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;// This is a METHOD because it has a receiver: '(u User)'&lt;/span&gt;
&lt;span class="c"&gt;// The receiver links this function to the User struct.&lt;/span&gt;
&lt;span class="c"&gt;// Inside this method, 'u' refers to the specific instance of the user.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&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;greet&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, my name is %s and I am %d years old.&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;u&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="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;)&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 our User struct&lt;/span&gt;
    &lt;span class="n"&gt;user1&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;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"budi@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Age&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// We call the method using the dot notation from the instance&lt;/span&gt;
    &lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// Prints: Hello, my name is Budi and I am 30 years old.&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;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We defined the &lt;code&gt;greet()&lt;/code&gt; method with a receiver &lt;code&gt;(u User)&lt;/code&gt;. This tells Go that &lt;code&gt;greet()&lt;/code&gt; belongs to the &lt;code&gt;User&lt;/code&gt; struct.&lt;/li&gt;
&lt;li&gt;Inside the method, we can access the fields of that specific user instance through the receiver variable (&lt;code&gt;u&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;We call the method from the instance variable (&lt;code&gt;user1.greet()&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Putting It All Together: A Full Example
&lt;/h2&gt;

&lt;p&gt;Let's combine everything into one complete, runnable program.&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 blueprint for our data&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;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Age&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;// The behavior attached to the User struct&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&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;greet&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, my name is %s and I am %d years old.&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;u&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="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;)&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;// Create two instances of User&lt;/span&gt;
    &lt;span class="n"&gt;user1&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;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"budi@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Age&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="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;user2&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;"Siti"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"siti@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Age&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Call the method from each instance&lt;/span&gt;
    &lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;greet&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You've just taken a massive leap in writing organized Go code! You now know how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Group related data together into a clean blueprint using &lt;strong&gt;Structs&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Attach specific behaviors to that data using &lt;strong&gt;Methods&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This pattern is fundamental to building larger, more complex applications. It allows you to create self-contained components that are easy to understand and reuse.&lt;/p&gt;

&lt;p&gt;In the next part, we'll dive into one of Go's most powerful (and sometimes tricky) concepts: &lt;strong&gt;Pointers&lt;/strong&gt;. We'll learn how they allow us to modify our structs and write more efficient code. See you there!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>go</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Functions</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:26:11 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-functions-88h</link>
      <guid>https://dev.to/andidev30/go-golang-basic-functions-88h</guid>
      <description>&lt;h2&gt;
  
  
  From a Long Script to Organized Blocks
&lt;/h2&gt;

&lt;p&gt;As our programs grow, putting all our logic inside the &lt;code&gt;main&lt;/code&gt; function can become messy and hard to manage. If we need to perform the same task multiple times, we'd have to copy and paste our code, which is a recipe for disaster.&lt;/p&gt;

&lt;p&gt;The solution is to organize our code into reusable, named blocks. In Go, as in most programming languages, these blocks are called &lt;strong&gt;Functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A coffee machine. Instead of manually grinding beans, tamping, and pulling a shot every time, you just press the "Espresso" button. That button is a &lt;strong&gt;function&lt;/strong&gt;. It bundles a series of steps into one simple action. &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Defining and Calling a Simple Function
&lt;/h2&gt;

&lt;p&gt;A function is a block of code that performs a specific task. Let's create our first 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="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;// 1. We define our function here&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sayHello&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;"Hello from a function!"&lt;/span&gt;&lt;span class="p"&gt;)&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;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;"Starting the program..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// 2. We call the function by its name&lt;/span&gt;
    &lt;span class="n"&gt;sayHello&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;"Program finished."&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;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; We define &lt;code&gt;func sayHello()&lt;/code&gt; to encapsulate the printing logic.&lt;/li&gt;
&lt;li&gt; Inside &lt;code&gt;main&lt;/code&gt;, we call &lt;code&gt;sayHello()&lt;/code&gt; by its name, followed by parentheses &lt;code&gt;()&lt;/code&gt;. The program jumps to the function, runs the code inside it, and then returns to &lt;code&gt;main&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. Functions with Parameters (Providing Input)
&lt;/h2&gt;

&lt;p&gt;What if we want our function to be more flexible? For example, what if we want to greet a specific person? We can pass data into a function through &lt;strong&gt;parameters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Parameters are variables that a function accepts as input.&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;// This function accepts one parameter called 'name' of type 'string'&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;greet&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="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;"Hello,"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Budi"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// "Budi" is the argument we pass to the 'name' parameter&lt;/span&gt;
    &lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Siti"&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 our &lt;code&gt;greet&lt;/code&gt; function is much more reusable!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Functions with Return Values (Getting Output)
&lt;/h2&gt;

&lt;p&gt;Functions are not just for &lt;em&gt;doing&lt;/em&gt; things; they can also &lt;em&gt;give back&lt;/em&gt; a result after they've finished their work. This is called a &lt;strong&gt;return value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A calculator. You provide input (numbers and an operation), and it &lt;strong&gt;returns&lt;/strong&gt; the answer to you. &lt;/p&gt;

&lt;h3&gt;
  
  
  How to Return a Single Value
&lt;/h3&gt;

&lt;p&gt;You specify the type of data the function will return right after its parameters.&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;// This function accepts a 'string' and will return a 'string'&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;createGreeting&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="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;greeting&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello from a function, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="c"&gt;// The 'return' keyword sends the value back&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;// We capture the returned value in a variable&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;createGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Charlie"&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Prints: Hello from a function, Charlie&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. The Go Superpower: Multiple Returns
&lt;/h2&gt;

&lt;p&gt;Here's a feature that makes Go stand out: a function can return &lt;strong&gt;more than one value&lt;/strong&gt;. This is extremely common in Go, especially for functions that might fail.&lt;/p&gt;

&lt;p&gt;The standard pattern is to return &lt;strong&gt;(the result, an error)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Return Multiple Values
&lt;/h3&gt;

&lt;p&gt;Let's create a safe division function that returns the result and a potential error if we try to divide by zero.&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="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"errors"&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// This function returns two values: a float64 AND an error&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float64&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;span class="c"&gt;// If the divisor is zero, it's an error&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&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;// We return a zero value for the number and a new error message&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cannot divide by zero"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// If everything is okay, we return the result and 'nil' for the error&lt;/span&gt;
    &lt;span class="c"&gt;// 'nil' is Go's way of saying "nothing" or "no error"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&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;// --- Successful case ---&lt;/span&gt;
    &lt;span class="n"&gt;result&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2.0&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="c"&gt;// This block is skipped because 'err' is nil&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;"Error:"&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="k"&gt;else&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;"Result 1:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// --- Failure case ---&lt;/span&gt;
    &lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10.0&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err2&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="c"&gt;// This block runs because an error was returned&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;"Error:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;"Result 2:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result2&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Functions are the cornerstone of writing well-structured and maintainable code. You've now learned how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define and call simple functions.&lt;/li&gt;
&lt;li&gt;Pass data into them using parameters.&lt;/li&gt;
&lt;li&gt;Get results back using single and multiple return values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By breaking your logic into functions, you're making your code cleaner, easier to debug, and much more powerful.&lt;/p&gt;

&lt;p&gt;In the next part, we'll learn how to create our own custom data types using &lt;strong&gt;Structs&lt;/strong&gt; and attach functions directly to them using &lt;strong&gt;Methods&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>newbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Go (Golang) Basic - 'for' Loop</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:23:25 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-basic-for-loop-3b83</link>
      <guid>https://dev.to/andidev30/go-golang-basic-for-loop-3b83</guid>
      <description>&lt;h2&gt;
  
  
  Doing Things Over and Over Again
&lt;/h2&gt;

&lt;p&gt;In our last part, we taught our code how to make decisions using &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;switch&lt;/code&gt;. Now, let's teach it how to perform tasks repeatedly without us having to copy and paste our code. This process is called &lt;strong&gt;looping&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Many programming languages have several types of loops (&lt;code&gt;while&lt;/code&gt;, &lt;code&gt;do-while&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;forEach&lt;/code&gt;). Go simplifies this dramatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Go, there is only one loop: the &lt;code&gt;for&lt;/code&gt; loop.&lt;/strong&gt; But it's incredibly versatile and can handle every looping scenario you need. Let's explore its three main forms.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Classic &lt;code&gt;for&lt;/code&gt; Loop
&lt;/h2&gt;

&lt;p&gt;This is the most traditional form of a &lt;code&gt;for&lt;/code&gt; loop, and you'll recognize it if you've used languages like C++, Java, or JavaScript. It's perfect when you know exactly how many times you want to repeat an action.&lt;/p&gt;

&lt;p&gt;It consists of three parts, separated by semicolons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;The init statement&lt;/strong&gt;: &lt;code&gt;i := 0&lt;/code&gt; (Initializes a counter. Runs once at the very beginning).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The condition expression&lt;/strong&gt;: &lt;code&gt;i &amp;lt; 5&lt;/code&gt; (Checked before every loop. If &lt;code&gt;true&lt;/code&gt;, the loop continues).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The post statement&lt;/strong&gt;: &lt;code&gt;i++&lt;/code&gt; (Runs at the end of every loop iteration, usually to increment the counter).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: An assembly line worker who has been told to assemble exactly 5 boxes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&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;// This loop will run 5 times (for i = 0, 1, 2, 3, 4)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&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;&amp;lt;&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;i&lt;/span&gt;&lt;span class="o"&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;"Assembling box number:"&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. The &lt;code&gt;for&lt;/code&gt; Loop as a &lt;code&gt;while&lt;/code&gt; Loop
&lt;/h2&gt;

&lt;p&gt;What if you don't know how many times to loop, but you know the condition to stop? In other languages, you'd use a &lt;code&gt;while&lt;/code&gt; loop. Go handles this with a simplified &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;You just provide the condition, and the loop will run as long as that condition is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Running on a treadmill. You don't know how many steps you'll take, but you'll keep running until you've burned 100 calories. The condition is &lt;code&gt;caloriesBurned &amp;lt; 100&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&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;// This loop acts like a 'while' loop&lt;/span&gt;
    &lt;span class="c"&gt;// It will run as long as 'number' is less than or equal to 5&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The number is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="c"&gt;// Important: Update the variable to avoid an infinite loop!&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;h2&gt;
  
  
  3. The &lt;code&gt;for range&lt;/code&gt; Loop: Iterating Over Collections
&lt;/h2&gt;

&lt;p&gt;This is the most idiomatic and powerful way to loop over collections like &lt;strong&gt;slices&lt;/strong&gt; and &lt;strong&gt;maps&lt;/strong&gt;. It's Go's version of a &lt;code&gt;forEach&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: Going through your grocery bag and looking at each item one by one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example with a Slice
&lt;/h3&gt;

&lt;p&gt;When used with a slice, &lt;code&gt;for range&lt;/code&gt; gives you the &lt;code&gt;index&lt;/code&gt; and the &lt;code&gt;value&lt;/code&gt; of each element.&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;fruits&lt;/span&gt; &lt;span class="o"&gt;:=&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="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cherry"&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;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fruit&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;fruits&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;"Fruit at index %d is %s&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;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// If you only need the value, you can ignore the index with an underscore (_)&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;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Just the fruit names:"&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;fruit&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;fruits&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;fruit&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;h3&gt;
  
  
  Code Example with a Map
&lt;/h3&gt;

&lt;p&gt;When used with a map, it gives you the &lt;code&gt;key&lt;/code&gt; and the &lt;code&gt;value&lt;/code&gt; of each pair.&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;userProfile&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"city"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Jakarta"&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;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;userProfile&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;"%s: %s&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;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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Even with just one loop keyword, Go provides all the power you need to handle any repetitive task. You've now learned the three essential patterns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The classic &lt;code&gt;for&lt;/code&gt; loop for a fixed number of iterations.&lt;/li&gt;
&lt;li&gt;The conditional &lt;code&gt;for&lt;/code&gt; loop that acts like a &lt;code&gt;while&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;for range&lt;/code&gt; loop for iterating over collections.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mastering loops is a huge step in becoming a proficient programmer. In our next part, we'll learn how to organize our code into clean, reusable blocks called &lt;strong&gt;Functions&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - 'if' and 'switch'</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:17:09 +0000</pubDate>
      <link>https://dev.to/andidev30/series-go-golang-basic-if-and-switch-10p7</link>
      <guid>https://dev.to/andidev30/series-go-golang-basic-if-and-switch-10p7</guid>
      <description>&lt;h2&gt;
  
  
  Making Your Code "Think"
&lt;/h2&gt;

&lt;p&gt;So far, our code has been executing line by line, from top to bottom. But what if we want it to make decisions? What if we want it to do one thing if a user is logged in, and something else if they aren't?&lt;/p&gt;

&lt;p&gt;This is where conditional logic comes in. It's the "brain" of our program, allowing it to react differently to different situations. In Go, we have two primary tools for this: the &lt;code&gt;if&lt;/code&gt; statement and the &lt;code&gt;switch&lt;/code&gt; statement.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The &lt;code&gt;if&lt;/code&gt; Statement: The Classic Decision Maker
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; statement is the most fundamental way to make a decision. Its logic is simple: &lt;strong&gt;"IF a certain condition is true, then do this."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can extend it with &lt;code&gt;else if&lt;/code&gt; for more conditions, and &lt;code&gt;else&lt;/code&gt; for a default action if no conditions are met.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A fork in the road. You check the sign (&lt;code&gt;if condition&lt;/code&gt;), and based on that, you choose a path. &lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use &lt;code&gt;if-else&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Let's build a simple program that gives a grade based on a score.&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;score&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;85&lt;/span&gt;

    &lt;span class="c"&gt;// The program checks these conditions from top to bottom&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;90&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;"Grade: A (Excellent!)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;80&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;"Grade: B (Good)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;70&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;"Grade: C (Average)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;"Grade: D (Needs Improvement)"&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;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since &lt;code&gt;score&lt;/code&gt; is 85, the first condition (&lt;code&gt;&amp;gt; 90&lt;/code&gt;) is false.&lt;/li&gt;
&lt;li&gt;It moves to the next one. &lt;code&gt;score &amp;gt; 80&lt;/code&gt; is true!&lt;/li&gt;
&lt;li&gt;The code inside that block runs, printing "Grade: B (Good)".&lt;/li&gt;
&lt;li&gt;The rest of the &lt;code&gt;if-else&lt;/code&gt; chain is then skipped.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The &lt;code&gt;switch&lt;/code&gt; Statement: The Tidy Alternative
&lt;/h2&gt;

&lt;p&gt;Sometimes, you have a long chain of &lt;code&gt;if-else if&lt;/code&gt; statements that all check the &lt;em&gt;same variable&lt;/em&gt;. This can look a bit messy. For these situations, Go gives us the &lt;code&gt;switch&lt;/code&gt; statement, which is often cleaner and easier to read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A vending machine menu. You check one thing (the button the user pressed) and match it against a list of possible options. &lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use &lt;code&gt;switch&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Let's rewrite a decision-making process for the days of the week.&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;day&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Saturday"&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Monday"&lt;/span&gt;&lt;span class="o"&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;"Time to start the week!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="o"&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;"The weekend is almost here!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Saturday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Sunday"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="c"&gt;// You can group multiple cases&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;"It's the weekend!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="c"&gt;// This runs if no other case matches&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;"It's a regular day."&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;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go takes the &lt;code&gt;day&lt;/code&gt; variable ("Saturday").&lt;/li&gt;
&lt;li&gt;It checks each &lt;code&gt;case&lt;/code&gt; one by one.&lt;/li&gt;
&lt;li&gt;It finds a match in &lt;code&gt;case "Saturday", "Sunday":&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The code inside that block runs, and the &lt;code&gt;switch&lt;/code&gt; statement ends.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;default&lt;/code&gt; block is the equivalent of the final &lt;code&gt;else&lt;/code&gt; in an &lt;code&gt;if&lt;/code&gt; chain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;if&lt;/code&gt; vs. &lt;code&gt;switch&lt;/code&gt;: When to Use Which?
&lt;/h2&gt;

&lt;p&gt;Here's a simple rule of thumb:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;if-else&lt;/code&gt;&lt;/strong&gt; when you have &lt;strong&gt;complex conditions&lt;/strong&gt;, check &lt;strong&gt;ranges&lt;/strong&gt; (like &lt;code&gt;score &amp;gt; 90&lt;/code&gt;), or need to evaluate &lt;strong&gt;multiple different variables&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;switch&lt;/code&gt;&lt;/strong&gt; when you are checking a &lt;strong&gt;single variable&lt;/strong&gt; against a list of &lt;strong&gt;specific, exact values&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You've now learned how to give your Go programs a "brain"! Using &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;switch&lt;/code&gt;, you can control the flow of your code, allowing it to make decisions and react dynamically. This is a massive step up from simply running code from top to bottom.&lt;/p&gt;

&lt;p&gt;In the next part of our series, we'll learn how to make our programs perform tasks repeatedly without rewriting code, by mastering Go's only loop: the powerful &lt;strong&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>newbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Data Collections: Array, Slice, and Map</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:09:59 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-data-collections-array-slice-and-map-15ei</link>
      <guid>https://dev.to/andidev30/go-golang-data-collections-array-slice-and-map-15ei</guid>
      <description>&lt;h2&gt;
  
  
  Storing More Than One Piece of Data
&lt;/h2&gt;

&lt;p&gt;So far, we've learned how to store single pieces of information in variables. But what happens when you need to manage a list of items, like a grocery list, or a collection of user settings? Storing each one in a separate variable would be a nightmare!&lt;/p&gt;

&lt;p&gt;This is where data collections come in. Go provides powerful, built-in types for managing groups of data. In this part, we'll focus on the two you'll use 99% of the time: &lt;strong&gt;Slices&lt;/strong&gt; and &lt;strong&gt;Maps&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Slice: Your Go-To Dynamic List
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;slice&lt;/strong&gt; is the most common collection type you'll use in Go. Think of it as a dynamic, flexible list that can grow or shrink as needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A shopping list. You can start with a few items and add more as you think of them. The order of the items matters. &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating and Using a Slice
&lt;/h3&gt;

&lt;p&gt;Let's see it in action.&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;// Creating a slice of strings with some initial data&lt;/span&gt;
    &lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;:=&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="s"&gt;"Apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Cherry"&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;"Initial fruits:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Adding a new item to the slice using append()&lt;/span&gt;
    &lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Orange"&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;"After adding Orange:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Accessing an element by its index (starts from 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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The first fruit is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fruits&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key points about Slices:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The syntax &lt;code&gt;[]string&lt;/code&gt; means "a slice of strings".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;append()&lt;/code&gt; is the built-in function to add elements to a slice.&lt;/li&gt;
&lt;li&gt;You access elements using an index, starting from &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The Map: A Key-Value Collection
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;map&lt;/strong&gt; is a collection of key-value pairs. It's incredibly useful when you want to look up a value based on a unique identifier (the key).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A phone's contact list. You use a unique key (the person's name) to look up their value (their phone number). The order of contacts doesn't really matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating and Using a Map
&lt;/h3&gt;

&lt;p&gt;Maps are perfect for things like configuration or user profiles.&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;// Creating a map where the key is a string and the value is a string&lt;/span&gt;
    &lt;span class="n"&gt;userProfile&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="s"&gt;"budi@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"country"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Indonesia"&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;"User profile:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Accessing a value by its key&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;"User's name is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="c"&gt;// Adding or updating a value&lt;/span&gt;
    &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jakarta"&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 adding city:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Deleting a key-value pair&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;userProfile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"country"&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;"After deleting country:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&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;&lt;strong&gt;Key points about Maps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The syntax &lt;code&gt;map[string]string&lt;/code&gt; means "a map with string keys and string values".&lt;/li&gt;
&lt;li&gt;The order of elements in a map is &lt;strong&gt;not guaranteed&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You use the key to add, retrieve, or delete data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tentu, ini bagian kedua dan terakhir dari draf artikel &lt;strong&gt;Bagian 5&lt;/strong&gt; yang sudah direvisi.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Draf Artikel Bagian 5 (Revisi, 2/2)&lt;/strong&gt;
&lt;/h3&gt;

&lt;h2&gt;
  
  
  3. The Map: A Key-Value Collection
&lt;/h2&gt;

&lt;p&gt;While slices are great for ordered lists, sometimes you need to look up data based on a unique identifier, not its position. For this, we use a &lt;strong&gt;map&lt;/strong&gt;. A map is a collection of key-value pairs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy&lt;/strong&gt;: A phone's contact list. You use a unique key (the person's name) to look up their value (their phone number). The order of contacts doesn't really matter. &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating and Using a Map
&lt;/h3&gt;

&lt;p&gt;Maps are perfect for things like configuration, user profiles, or any data where you need fast lookups.&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;// Creating a map where the key is a string and the value is a string&lt;/span&gt;
    &lt;span class="n"&gt;userProfile&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Budi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="s"&gt;"budi@email.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"country"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Indonesia"&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;"User profile:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Accessing a value by its key&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;"User's name is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="c"&gt;// Adding or updating a value is done the same way&lt;/span&gt;
    &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jakarta"&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 adding city:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// Deleting a key-value pair using delete()&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;userProfile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"country"&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;"After deleting country:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userProfile&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;&lt;strong&gt;Key points about Maps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The syntax &lt;code&gt;map[string]string&lt;/code&gt; means "a map with string keys and string values".&lt;/li&gt;
&lt;li&gt;The order of elements in a map is &lt;strong&gt;not guaranteed&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You use the key (&lt;code&gt;[]&lt;/code&gt;) to add, retrieve, or &lt;code&gt;delete()&lt;/code&gt; data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Array vs. Slice vs. Map: Which One to Use?
&lt;/h2&gt;

&lt;p&gt;Here's a quick guide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use an &lt;strong&gt;Array&lt;/strong&gt; when you know &lt;em&gt;exactly&lt;/em&gt; how many items you'll have, and that number will never change (very rare).&lt;/li&gt;
&lt;li&gt;Use a &lt;strong&gt;Slice&lt;/strong&gt; for almost everything else that is a list of items. It's the default choice for lists in Go.&lt;/li&gt;
&lt;li&gt;Use a &lt;strong&gt;Map&lt;/strong&gt; when you need to store data that should be accessed by a unique identifier, not by its position in a list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You've now learned how to manage groups of data in Go! You understand the difference between a rigid &lt;code&gt;Array&lt;/code&gt;, a flexible &lt;code&gt;Slice&lt;/code&gt;, and a lookup-based &lt;code&gt;Map&lt;/code&gt;. These three data structures are the foundation for building any complex application.&lt;/p&gt;

&lt;p&gt;In the next part, we'll learn how to make our code "think" and make decisions using &lt;strong&gt;conditional logic with &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;switch&lt;/code&gt;&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Data Types and Variables</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 06:57:32 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-data-types-and-variables-29gk</link>
      <guid>https://dev.to/andidev30/go-golang-data-types-and-variables-29gk</guid>
      <description>&lt;h2&gt;
  
  
  The Building Blocks of a Program
&lt;/h2&gt;

&lt;p&gt;To build any useful program, we need a way to store and manage information—a user's name, a score in a game, or the price of a product. In programming, we do this using &lt;strong&gt;variables&lt;/strong&gt; and &lt;strong&gt;data types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They are the most fundamental building blocks for handling data. Let's dive into how Go approaches this.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Data Types? (Why Go is "Strict")
&lt;/h2&gt;

&lt;p&gt;Before we can store a piece of data, we must tell Go what &lt;em&gt;kind&lt;/em&gt; of data it is. Go is a &lt;strong&gt;statically-typed language&lt;/strong&gt;, which means it's very strict about this rule.&lt;/p&gt;

&lt;p&gt;Think of it like having labeled storage boxes: one for "Books", one for "Clothes", and one for "Toys". You can't put a toy in the book box. This strictness helps prevent many common bugs right from the start.&lt;/p&gt;

&lt;p&gt;Here are the four most essential data types in Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;string&lt;/code&gt;&lt;/strong&gt;: Used for text. Any sequence of characters enclosed in double quotes (&lt;code&gt;"&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;"Hello, Budi"&lt;/code&gt;, &lt;code&gt;"123 Main St."&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;int&lt;/code&gt;&lt;/strong&gt;: Used for integers (whole numbers), both positive and negative.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;-50&lt;/code&gt;, &lt;code&gt;2025&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;float64&lt;/code&gt;&lt;/strong&gt;: Used for floating-point numbers (decimals).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;3.14&lt;/code&gt;, &lt;code&gt;99.5&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;bool&lt;/code&gt;&lt;/strong&gt;: Used for boolean logic (&lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Variables: The Containers for Your Data
&lt;/h2&gt;

&lt;p&gt;A variable is simply a named container for our data. Once you create (declare) a variable, you can change its value later. There are two common ways to declare a variable in Go:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Formal Way (&lt;code&gt;var&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;This way is very explicit about the variable's 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="c"&gt;// var &amp;lt;variableName&amp;gt; &amp;lt;dataType&amp;gt; = &amp;lt;value&amp;gt;&lt;/span&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;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go can also infer the type if you leave it out:&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;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jakarta"&lt;/span&gt; &lt;span class="c"&gt;// Go knows this is a string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The Short Way (&lt;code&gt;:=&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;This is the most common and preferred way to declare and initialize a variable &lt;strong&gt;inside a function&lt;/strong&gt;. It's clean and concise. This is the most common and preferred way to declare and initialize a variable inside a 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="c"&gt;// &amp;lt;variableName&amp;gt; := &amp;lt;value&amp;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;"Siti"&lt;/span&gt;
&lt;span class="n"&gt;isActive&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once a variable is declared, you can update its value using a simple equals sign (&lt;code&gt;=&lt;/code&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="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Siti Nurbaya"&lt;/span&gt; &lt;span class="c"&gt;// Updating the value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constants: Values That Never Change
&lt;/h2&gt;

&lt;p&gt;Sometimes, you have a value that you know will never, ever change throughout your program's life. Think of mathematical constants like Pi, or a specific configuration setting. For these, Go gives us &lt;strong&gt;constants&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A constant is declared using the &lt;code&gt;const&lt;/code&gt; keyword. Once set, its value is locked and cannot be changed.&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;const&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3.14&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;defaultPort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8080&lt;/span&gt;

&lt;span class="c"&gt;// This would cause an error! You cannot change a constant.&lt;/span&gt;
&lt;span class="c"&gt;// pi = 3.15 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Putting It All Together: A Code Example
&lt;/h2&gt;

&lt;p&gt;Let's see all these concepts in action in a single program. Create a &lt;code&gt;main.go&lt;/code&gt; file and try out this code:&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;// --- STRINGS ---&lt;/span&gt;
    &lt;span class="c"&gt;// Using the short declaration (:=)&lt;/span&gt;
    &lt;span class="n"&gt;productName&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Go Programming E-book"&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;productName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// --- INTEGERS ---&lt;/span&gt;
    &lt;span class="c"&gt;// Using the formal declaration (var)&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;quantity&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;10&lt;/span&gt;
    &lt;span class="k"&gt;var&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;50000&lt;/span&gt; &lt;span class="c"&gt;// Go infers this is an int&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;quantity&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;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// --- FLOATS ---&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;85.5&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;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// --- BOOLEANS ---&lt;/span&gt;
    &lt;span class="n"&gt;isPublished&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;true&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;"Is the product published?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isPublished&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// --- CONSTANTS ---&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Go Community"&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;"Author:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c"&gt;// --- UPDATING A VARIABLE ---&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;"Current quantity:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt; &lt;span class="c"&gt;// Update the value&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;"Updated quantity:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You've now mastered the absolute fundamentals of handling data in Go. You know how to store text, numbers, and logical values using &lt;strong&gt;variables&lt;/strong&gt;, and how to protect important values using &lt;strong&gt;constants&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These are the essential building blocks we'll use in every program from now on. In the next part of the series, we'll take a big step forward and learn how to store &lt;strong&gt;collections of data&lt;/strong&gt; using two of Go's most powerful features: &lt;strong&gt;Slices and Maps&lt;/strong&gt;. See you there!&lt;/p&gt;

</description>
      <category>go</category>
      <category>newbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Go (Golang) Basic - Your First 'Hello, World!'</title>
      <dc:creator>Andi</dc:creator>
      <pubDate>Sun, 28 Sep 2025 04:01:16 +0000</pubDate>
      <link>https://dev.to/andidev30/go-golang-hello-world-3im2</link>
      <guid>https://dev.to/andidev30/go-golang-hello-world-3im2</guid>
      <description>&lt;h2&gt;
  
  
  Let's Write Some Code!
&lt;/h2&gt;

&lt;p&gt;With your Go development environment now set up, it's time for the moment we've been waiting for: writing our first lines of code.&lt;/p&gt;

&lt;p&gt;In the world of programming, the "Hello, World!" program is a time-honored tradition. It's a simple program that prints a message to the screen, serving two main purposes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Confirming that our setup works correctly.&lt;/li&gt;
&lt;li&gt; Introducing us to the most basic syntax of a language.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create Your Go File
&lt;/h2&gt;

&lt;p&gt;Inside the project folder you created (e.g., &lt;code&gt;my-go-project&lt;/code&gt;), create a new file and name it &lt;strong&gt;&lt;code&gt;main.go&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;All your Go code will live inside files with the &lt;code&gt;.go&lt;/code&gt; extension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Write the Code
&lt;/h2&gt;

&lt;p&gt;Open your new &lt;code&gt;main.go&lt;/code&gt; file in your editor (VS Code or GoLand) and type the following code exactly as it is shown:&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;// This is the main function, the entry point of our program&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;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;"Hello, World from Go!"&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;h2&gt;
  
  
  Step 3: Breaking Down the Code
&lt;/h2&gt;

&lt;p&gt;It's a short program, but every line has an important role. Let's break it down so you understand what's happening. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;package main&lt;/code&gt;&lt;/strong&gt;: This line declares that our file belongs to the &lt;code&gt;main&lt;/code&gt; package. In Go, an executable program (one you can run directly) must have a &lt;code&gt;main&lt;/code&gt; package.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;import "fmt"&lt;/code&gt;&lt;/strong&gt;: We are "importing" a built-in Go package named &lt;code&gt;fmt&lt;/code&gt; (short for format). This package provides us with functions for formatting and printing text, much like &lt;code&gt;console.log&lt;/code&gt; in JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;func main()&lt;/code&gt;&lt;/strong&gt;: This defines the &lt;code&gt;main&lt;/code&gt; function. This function is special. It's the special entry point of our application. When we run the program, Go looks for func main() and executes the code inside it first. When we run the program, the code inside the curly braces &lt;code&gt;{}&lt;/code&gt; of &lt;code&gt;func main()&lt;/code&gt; is the first code that gets executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;fmt.Println("Hello, World from Go!")&lt;/code&gt;&lt;/strong&gt;: Here we are calling the &lt;code&gt;Println&lt;/code&gt; (&lt;em&gt;Print Line&lt;/em&gt;) function from the &lt;code&gt;fmt&lt;/code&gt; package we imported. This function prints the text &lt;code&gt;"Hello, World from Go!"&lt;/code&gt; to our terminal, and then adds a new line at the end.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: Running Your Program
&lt;/h2&gt;

&lt;p&gt;Now for the magic. Return to your terminal, make sure you are still inside your project directory, and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything was typed correctly, you will see this output on your screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World from Go!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You have just written, understood, and run your very first Go program. This is a fundamental and exciting milestone in learning any new language.&lt;/p&gt;

</description>
      <category>go</category>
      <category>newbie</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
