<?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: Abhinav Prajapati</title>
    <description>The latest articles on DEV Community by Abhinav Prajapati (@abhinavdevs).</description>
    <link>https://dev.to/abhinavdevs</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4012955%2F89307f10-3ccf-4261-a2dc-2f997de00b6f.png</url>
      <title>DEV Community: Abhinav Prajapati</title>
      <link>https://dev.to/abhinavdevs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhinavdevs"/>
    <language>en</language>
    <item>
      <title>Variable Scope and Variable Shadowing in Golang</title>
      <dc:creator>Abhinav Prajapati</dc:creator>
      <pubDate>Fri, 03 Jul 2026 04:13:57 +0000</pubDate>
      <link>https://dev.to/abhinavdevs/variable-scope-and-variable-shadowing-in-golang-2hmo</link>
      <guid>https://dev.to/abhinavdevs/variable-scope-and-variable-shadowing-in-golang-2hmo</guid>
      <description>&lt;p&gt;Variable scope refers to the block or region of code in which a variable can be accessed. Go uses &lt;strong&gt;lexical scoping&lt;/strong&gt; (also known as &lt;strong&gt;static scoping&lt;/strong&gt;) based on blocks.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;block&lt;/strong&gt; is a sequence of declarations and statements enclosed within matching curly braces &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's consider the following Go 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="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&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;5&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;15&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;"Value of x at stage 1:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&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;"Value of x at stage 2:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&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;"Value of x at stage 3:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&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;What do you think the output of this code will be? Let's evaluate it.&lt;/p&gt;

&lt;p&gt;The line &lt;code&gt;x := 15&lt;/code&gt; declares a new variable within the scope of the &lt;code&gt;if&lt;/code&gt; block. So, whenever &lt;code&gt;x&lt;/code&gt; is modified inside that block, the changes apply only to the inner &lt;code&gt;x&lt;/code&gt;, not to the outer one.&lt;/p&gt;

&lt;p&gt;Therefore, the output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Value of x at stage 1: 15
Value of x at stage 2: 20
Value of x at stage 3: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This behavior is called &lt;strong&gt;variable shadowing&lt;/strong&gt;. It occurs when a variable declared in an inner scope has the same name as a variable declared in an outer scope.&lt;/p&gt;

&lt;p&gt;When you write &lt;code&gt;x := 15&lt;/code&gt;, you are &lt;strong&gt;not&lt;/strong&gt; assigning a new value to the &lt;code&gt;x&lt;/code&gt; whose value is &lt;code&gt;5&lt;/code&gt;. Instead, you are declaring a completely new and distinct variable that just happens to have the same name. Go allocates separate memory for this inner &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From that point until the closing brace &lt;code&gt;}&lt;/code&gt; of the &lt;code&gt;if&lt;/code&gt; block, the inner &lt;code&gt;x&lt;/code&gt; shadows (or hides) the outer &lt;code&gt;x&lt;/code&gt;. Whenever the Go compiler encounters &lt;code&gt;x&lt;/code&gt; inside that block, it resolves it to the variable in the innermost scope.&lt;/p&gt;

&lt;p&gt;Here's what happens at each stage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stage 1:&lt;/strong&gt; A new variable &lt;code&gt;x&lt;/code&gt; is initialized with the value &lt;code&gt;15&lt;/code&gt;. The outer &lt;code&gt;x&lt;/code&gt; (which is &lt;code&gt;5&lt;/code&gt;) still exists, but it is temporarily hidden.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stage 2:&lt;/strong&gt; The statement &lt;code&gt;x = 20&lt;/code&gt; updates the inner &lt;code&gt;x&lt;/code&gt;. The outer &lt;code&gt;x&lt;/code&gt; remains unchanged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stage 3:&lt;/strong&gt; The &lt;code&gt;if&lt;/code&gt; block ends, so the inner &lt;code&gt;x&lt;/code&gt; goes out of scope. The name &lt;code&gt;x&lt;/code&gt; now refers to the outer variable again, which still holds the value &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One simple way to avoid this issue is to choose variable names carefully so that shadowing doesn't happen unintentionally.&lt;/p&gt;

&lt;p&gt;Variable shadowing is a common source of subtle bugs in Go, especially when working with multiple return values. A frequent example is accidentally shadowing the &lt;code&gt;err&lt;/code&gt; variable inside loops or conditional blocks by using &lt;code&gt;:=&lt;/code&gt; instead of &lt;code&gt;=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Have you ever run into a bug caused by variable shadowing in Go? Share your experience in the comments!&lt;/p&gt;

</description>
      <category>go</category>
      <category>tutorial</category>
      <category>code</category>
    </item>
  </channel>
</rss>
