<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohammad Aziz</title>
    <description>The latest articles on Forem by Mohammad Aziz (@iaziz786).</description>
    <link>https://forem.com/iaziz786</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%2F101426%2F362b6087-3cbd-419e-b0de-045fc320788c.png</url>
      <title>Forem: Mohammad Aziz</title>
      <link>https://forem.com/iaziz786</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/iaziz786"/>
    <language>en</language>
    <item>
      <title>How to Use Unpublished Go Modules in Your Local Projects?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Thu, 13 May 2021 02:07:50 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-use-unpublished-go-modules-in-your-local-projects-3379</link>
      <guid>https://forem.com/iaziz786/how-to-use-unpublished-go-modules-in-your-local-projects-3379</guid>
      <description>&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;While developing any go applications you might come into a situation where you need to use a module that is not yet ready to publish. The module might be living on your local machine and you wanted to get the taste of it using with other projects before shipping it. In this post, we will discuss what approach you can take to import a module that is still in development and before publishing it to the package registry.&lt;/p&gt;

&lt;p&gt;We will write a simple project which takes a string and returns a boolean whether the string is valid &lt;a href="https://en.wikipedia.org/wiki/IP_address" rel="noopener noreferrer"&gt;IP address&lt;/a&gt; or not. The functionality is not that important but the important part is how we integrate it into another project while &lt;em&gt;still making changes&lt;/em&gt; to the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Projects Layout
&lt;/h2&gt;

&lt;p&gt;To keep things simple, I have created two projects which live side by side. The &lt;code&gt;wip-module&lt;/code&gt; project is the unpublished one and the &lt;code&gt;tester-module&lt;/code&gt; is the one in which will import the unpublished &lt;code&gt;wip-module&lt;/code&gt; project. You can find the folder structure on &lt;a href="https://github.com/iAziz786/unpublished-go-module" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2zf2xjxqos0779wxtlou.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2zf2xjxqos0779wxtlou.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  WIP Project
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;wip-module&lt;/code&gt; is a simple go module that holds the implementation of checking whether the IP is valid or not. Let's look into the file:&lt;/p&gt;

&lt;p&gt;wip-module/ipchecker.go&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;ipchecker&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;"net"&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;IsValidIP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&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;bool&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;ip&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParseIP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;ip&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="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This module does not have the main package which means it was intended to be used as a library in other projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tester Project
&lt;/h2&gt;

&lt;p&gt;This is the project which will import the &lt;code&gt;ipchecker&lt;/code&gt; package. Here is how it looks:&lt;/p&gt;

&lt;p&gt;tester-module/main.go&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="n"&gt;ipchecker&lt;/span&gt; &lt;span class="s"&gt;"github.com/iAziz786/wipmodule"&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;"valid IP = "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ipchecker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsValidIP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"127.0.0.1"&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;"valid IP = "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ipchecker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsValidIP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"invalid-ip"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Output:&lt;/span&gt;
&lt;span class="c"&gt;// valid IP =  true&lt;/span&gt;
&lt;span class="c"&gt;// valid IP =  false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see it's using the &lt;code&gt;ipchecker&lt;/code&gt; package from the &lt;code&gt;github.com/iAziz786/wipmodule&lt;/code&gt; module. At this point, if you run the &lt;code&gt;tester-module&lt;/code&gt; program you will see an error related to the package. Let's glue them together!&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Unpublished Modules
&lt;/h2&gt;

&lt;p&gt;Before we do anything let's look at the &lt;code&gt;go.mod&lt;/code&gt; file of the &lt;code&gt;tester-module&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;tester-module/go.mod&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module github.com/iAziz786/testermodule

go 1.16

require github.com/iAziz786/wipmodule v0.0.0-unpublished
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the module is not published we gave it a version (&lt;code&gt;v0.0.0-unpublished&lt;/code&gt;) that does not exist yet but to make things work correctly.&lt;/p&gt;

&lt;p&gt;The next step is to replace the import behaviour for the wipmodule. We can do this by telling the go compiler to replace the import for &lt;code&gt;github.com/iAziz786/wipmodule&lt;/code&gt; with the local path of the module. Since they are siblings, I can use relative path 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 shell"&gt;&lt;code&gt;go mod edit &lt;span class="nt"&gt;-replace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;github.com/iAziz786/wipmodule@v0.0.0-published&lt;span class="o"&gt;=&lt;/span&gt;../wip-module
go get &lt;span class="nt"&gt;-d&lt;/span&gt; github.com/iAziz786/wipmodule@v0.0.0-published
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will add a replacement in the &lt;code&gt;go.mod&lt;/code&gt; of the &lt;code&gt;tester-module&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;tester-module/go.mod&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module github.com/iAziz786/testermodule

go 1.16

require github.com/iAziz786/wipmodule v0.0.0-unpublished

replace github.com/iAziz786/wipmodule v0.0.0-unpublished =&amp;gt; ../wip-module
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you notice a replace directive at the end of the file? It adds the logic to tell &lt;code&gt;go get&lt;/code&gt; to use the implementation which is one level up in the &lt;code&gt;wip-module&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now if you will run the program in the &lt;code&gt;tester-module&lt;/code&gt; you will see this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;tester-module
&lt;span class="nv"&gt;$ &lt;/span&gt;go run &lt;span class="nb"&gt;.&lt;/span&gt;

&lt;span class="c"&gt;# Output&lt;/span&gt;
valid IP &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nb"&gt;true
&lt;/span&gt;valid IP &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Make sure to remove the replace directive from go.mod of the tester module before you commit your changes, otherwise &lt;code&gt;go get&lt;/code&gt; will break in non-local environment.&lt;/p&gt;

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

&lt;p&gt;We have used the golang's module &lt;a href="https://golang.org/ref/mod#go-mod-file-replace" rel="noopener noreferrer"&gt;replace directive&lt;/a&gt; to use the local go module which is not a published module to import it into the other projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://golang.org/doc/modules/managing-dependencies#unpublished" rel="noopener noreferrer"&gt;https://golang.org/doc/modules/managing-dependencies#unpublished&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://golang.org/ref/mod#go-mod-file-replace" rel="noopener noreferrer"&gt;https://golang.org/ref/mod#go-mod-file-replace&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>modules</category>
      <category>unpublished</category>
    </item>
    <item>
      <title>How to Work with Arbitrary JSON in Go?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Wed, 30 Dec 2020 15:24:22 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-work-with-arbitrary-json-in-go-1f71</link>
      <guid>https://forem.com/iaziz786/how-to-work-with-arbitrary-json-in-go-1f71</guid>
      <description>&lt;h2&gt;
  
  
  Abstract
&lt;/h2&gt;

&lt;p&gt;Mostly, when you are working with data you know the schema in which you will receive that data, but there come times when you don't know exactly what will be the structure you will end up receiving. We talk about how you can handle JSON data which are arbitrary in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON with a Fixed Structure
&lt;/h2&gt;

&lt;p&gt;When you receive JSON for which you already know the structure, it is pretty easy to work with. You create a &lt;code&gt;struct&lt;/code&gt; and unmarshal the bytes to that struct and the JSON package will take care of the rest. Suppose you receive information about today's dinner in JSON and everyday dinner has the following structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Dinner&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;MainCourse&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;  &lt;span class="s"&gt;`json:"main_course"`&lt;/span&gt;
    &lt;span class="n"&gt;Beverage&lt;/span&gt;     &lt;span class="kt"&gt;string&lt;/span&gt;  &lt;span class="s"&gt;`json:"beverage"`&lt;/span&gt;
    &lt;span class="n"&gt;HasDessert&lt;/span&gt;   &lt;span class="kt"&gt;bool&lt;/span&gt;    &lt;span class="s"&gt;`json:"has_dessert"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you receive the following JSON value:&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;food&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`
{
  "main_course": "Biryani",
  "beverage": "Coca Cola",
  "has_dessert": true
}
`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You just need to create a variable which can hold the value from JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;dinner&lt;/span&gt; &lt;span class="n"&gt;Dinner&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eventually, decode the values to the &lt;code&gt;dinner&lt;/code&gt; variable 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="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;dinner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can access those values through the fields on the struct. Lucky you, you have dessert for today 😋.&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;dinner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MainCourse&lt;/span&gt;   &lt;span class="c"&gt;// Biryani&lt;/span&gt;
&lt;span class="n"&gt;dinner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HasDessert&lt;/span&gt;   &lt;span class="c"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  JSON That You Are Not Aware
&lt;/h2&gt;

&lt;p&gt;Let's diverge our mind from food to shopping list :). Now assume every weekend you go to purchase some grocery and the items in the list keep changing each week. If you have to represent that into a struct, you got no luck!&lt;/p&gt;

&lt;p&gt;Like almost everything in Go where so much randomness is evolved, you have to use interfaces. More particularly an empty interface. You can represent your shopping list 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;var&lt;/span&gt; &lt;span class="n"&gt;shoppingList&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because an empty interface can satisfy any type you can use that to do Unmarshaling.&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;listJSON&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`
{
  "food": {
    "ketchup": "1pc",
    "noodles": "1pc",
    "rice": "2Kg"
  },
  "utensils": {
    "knives": 2,
    "forks": 3
  }
}
`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listJSON&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;shoppingList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point &lt;code&gt;shoppingList&lt;/code&gt; looks 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="n"&gt;shoppingList&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="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"food"&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="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"ketchup"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"1pc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"noodles"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"1pc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"rice"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"2Kg"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="s"&gt;"utensils"&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="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"knives"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"forks"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&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;To access the data, you can make a type assertion to &lt;code&gt;map[string]interface{}&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;list&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;shoppingList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, you can access the values like a regular map. But for the values that are nested, you &lt;strong&gt;need to do a type assertion again&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;utensils&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"utensils"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;

&lt;span class="n"&gt;utensils&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"forks"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;You can handle arbitrary JSON with the help of interfaces. You just need to pass an empty interface to the Unmarshal function and the JSON data. After a type assertion, you are good to use the arbitrary fields and their values.&lt;/p&gt;

</description>
      <category>go</category>
      <category>json</category>
    </item>
    <item>
      <title>How to Use Embedding to Write Cleaner Code in Go?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Thu, 17 Dec 2020 19:13:09 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-use-embedding-to-write-cleaner-code-in-go-4caf</link>
      <guid>https://forem.com/iaziz786/how-to-use-embedding-to-write-cleaner-code-in-go-4caf</guid>
      <description>&lt;h2&gt;
  
  
  Abstract
&lt;/h2&gt;

&lt;p&gt;Go is cleaner because it doesn't support many of the features that tradition OO languages support. &lt;em&gt;Less is more&lt;/em&gt;. Inheritance is one of those features but Go has a different style which can fulfil the necessity of inheritance. It's called Embedding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is embedding&lt;/li&gt;
&lt;li&gt;How to create embedding&lt;/li&gt;
&lt;li&gt;Embedding In Interface&lt;/li&gt;
&lt;li&gt;Embedding In Struct&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Reference&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is embedding
&lt;/h2&gt;

&lt;p&gt;You can "borrow" the functionality of a struct or interface into an another struct and interface respectively is called embedding. You can not embed an interface into a struct or vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create embedding
&lt;/h2&gt;

&lt;p&gt;To create an embedding you need to put a different struct or interface into a different one without naming the field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Morning&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;breakfast&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Routine&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;Morning&lt;/span&gt; &lt;span class="c"&gt;// ⬅️ no field name makes it embedded &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Director&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;Direct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Producer&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;Produce&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Creator&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;Director&lt;/span&gt;
  &lt;span class="n"&gt;Producer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Embedding In Interface
&lt;/h2&gt;

&lt;p&gt;Interface when embedded is the union of all the methods present in all of the interfaces combined. If method &lt;code&gt;Eat()&lt;/code&gt; is present on an interface and &lt;code&gt;Sleep()&lt;/code&gt; on another then embedding them into an interface with &lt;code&gt;Repeat()&lt;/code&gt; method will have all, &lt;code&gt;Eat()&lt;/code&gt;, &lt;code&gt;Sleep()&lt;/code&gt;, and &lt;code&gt;Repeat()&lt;/code&gt; methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embedding In Struct
&lt;/h2&gt;

&lt;p&gt;Embedding in a struct is where you can see the real power of Go. Not only the fields that &lt;a href="https://golang.org/ref/spec#Type_definitions"&gt;defined type&lt;/a&gt; has but also the methods you can access them directly. How cool is that! Let's continue with our Routine 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;type&lt;/span&gt; &lt;span class="n"&gt;Morning&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;breakfast&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;Morning&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;hasBreakfast&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;breakfast&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Routine&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;Morning&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Routine&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasBreakfast&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You also the embedded type will be a field name on the struct. For example, you can have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;DailySports&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;Jog&lt;/span&gt;            &lt;span class="c"&gt;// field name Jog&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Swim&lt;/span&gt;          &lt;span class="c"&gt;// field name Swim&lt;/span&gt;
  &lt;span class="n"&gt;sports&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Cycling&lt;/span&gt; &lt;span class="c"&gt;// field name Cycling&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sports&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tennis&lt;/span&gt; &lt;span class="c"&gt;// field name Tennis&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;DailySports&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;enrolledBySwimming&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Swim&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Speed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But you cannot have conflict while embedding. This makes sense otherwise you won't know which one to pick.&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;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Swim&lt;/span&gt;         &lt;span class="c"&gt;// ⛔ Now allowed&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sports&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Swim&lt;/span&gt;  &lt;span class="c"&gt;// ⛔ Not allowed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can do all the things you can with the promoted fields but you can not use the field name present in the defined type while constructing 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;type&lt;/span&gt; &lt;span class="n"&gt;Morning&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;breakfast&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Routine&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;Morning&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Routine&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;breakfast&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c"&gt;// ⛔ Not allowed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;You can use embedding to use any defined type to used inside a struct and borrow all the functionality it has. You need to check that the names don't clash otherwise you are good to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://golang.org/doc/effective_go.html#embedding"&gt;https://golang.org/doc/effective_go.html#embedding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://golang.org/ref/spec#Struct_types"&gt;https://golang.org/ref/spec#Struct_types&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>How to Handle Missing Fields From A Struct in Go?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Thu, 10 Dec 2020 10:20:14 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-handle-missing-fields-from-a-struct-in-go-40ag</link>
      <guid>https://forem.com/iaziz786/how-to-handle-missing-fields-from-a-struct-in-go-40ag</guid>
      <description>&lt;p&gt;Suppose you have a variable of the type of the struct mentioned below which you wanted to &lt;code&gt;Unmarshal&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;type&lt;/span&gt; &lt;span class="n"&gt;Building&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;WindowCount&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="s"&gt;`json:"window_count"`&lt;/span&gt;
    &lt;span class="n"&gt;Doors&lt;/span&gt;       &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="s"&gt;`json:"doors"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case the data is coming from an unreliable source, you can never be sure that the value you will receive will always have the correct format. You have to be cautious when working around data like this. If you are new in Go you might be wondering what will happen you &lt;code&gt;Unmarshal&lt;/code&gt; and incorrect value?&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiler Loves Zero Values
&lt;/h2&gt;

&lt;p&gt;If you unmarshal the code into a variable you will get the &lt;a href="https://dave.cheney.net/2013/01/19/what-is-the-zero-value-and-why-is-it-useful"&gt;zero value&lt;/a&gt; for the fields &lt;code&gt;WindowCount&lt;/code&gt;, and &lt;code&gt;Doors&lt;/code&gt;. Which in some case can lead to bug because you might have different logic for missing case.&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;b&lt;/span&gt; &lt;span class="n"&gt;Building&lt;/span&gt;
&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%+v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&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;// Output:&lt;/span&gt;
&lt;span class="c"&gt;// {WindowCount:0 Doors:0}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pointer To Rescue 🦸
&lt;/h2&gt;

&lt;p&gt;If you have worked with pointers in languages like C, C++ you might have some opinion. You either like it or you hate it. Most people fall into the latter group. Working with pointers in Golang is fun because things are declarative comparatively.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you set the field type to a pointer of that type it will be &lt;code&gt;nil&lt;/code&gt; in case the field is missing&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Building&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;WindowCount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="s"&gt;`json:"window_count"`&lt;/span&gt;
    &lt;span class="n"&gt;Doors&lt;/span&gt;       &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="s"&gt;`json:"doors"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unmarshalling it has the following effect:&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;b&lt;/span&gt; &lt;span class="n"&gt;Building&lt;/span&gt;
&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`{"window_count": 2}`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%+v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&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;// Output:&lt;/span&gt;
&lt;span class="c"&gt;// {WindowCount:0xc000016170 Doors:&amp;lt;nil&amp;gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;WindowCount&lt;/code&gt; holds the pointer to an integer which holds the value 2. Whereas since &lt;code&gt;doors&lt;/code&gt; is missing from the JSON string it will set to &lt;code&gt;nil&lt;/code&gt; thanks to the zero value of pointers. You can now write your logic in case of the absence of that value.&lt;/p&gt;

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

&lt;p&gt;Since the field of a struct is part of the contract of the struct it can't go missing entirely instead you need to handle it little differently. The most preferable way is to convert the type to a pointer of the same type and when in some case the value is missing it will be set to the zero value, which is &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>How to Auto Reload Golang Applications?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Mon, 30 Nov 2020 17:02:02 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-auto-reload-golang-applications-58j0</link>
      <guid>https://forem.com/iaziz786/how-to-auto-reload-golang-applications-58j0</guid>
      <description>&lt;p&gt;You wanted to develop quick, you wanted to develop fast and there is&lt;br&gt;
nothing more tedious for a developer than doing the same thing again&lt;br&gt;
and again.&lt;/p&gt;

&lt;p&gt;I really &lt;em&gt;HATE&lt;/em&gt; doing repetitive work.&lt;/p&gt;
&lt;h2&gt;
  
  
  Foundation (a server)
&lt;/h2&gt;

&lt;p&gt;I have been exploring Go, you might be aware since my previous post was&lt;br&gt;
written for itself. You can spin up a simple Go server with the following&lt;br&gt;
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;"net/http"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&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;Fprintln&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&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="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;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/echo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":3000"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code creates a simple server which listens on port 3000 and if&lt;br&gt;
you hit &lt;code&gt;http://localhost:3000/echo&lt;/code&gt; you will receive "hello" as a message.&lt;/p&gt;

&lt;p&gt;To run the go server we should run the command below&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 &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I call this the "tedious code". I don't care about it but I care what it&lt;br&gt;
does for me, it runs my application.&lt;/p&gt;
&lt;h2&gt;
  
  
  I want changes, right now!
&lt;/h2&gt;

&lt;p&gt;Now, if you do any changes to the code you have run the "tedious code" again, and again, and again...&lt;/p&gt;

&lt;p&gt;You get it 🙄&lt;/p&gt;

&lt;p&gt;If you are a lazy developer you must be thinking what is the&lt;br&gt;
the better way to do it?&lt;/p&gt;

&lt;p&gt;Your computer can help you will that for sure.&lt;/p&gt;

&lt;p&gt;Firstly, you need to install &lt;a href="https://nodejs.org/en/download/"&gt;Node.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I know, you must be thinking about how Node.js can help you to reload your Go code?&lt;br&gt;
Just bear with me and you will know how.&lt;/p&gt;

&lt;p&gt;Once Node.js is installed on your system you will be able to run the&lt;br&gt;
command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will install a package called nodemon in your system and you will be&lt;br&gt;
able to run it from the command line.&lt;/p&gt;
&lt;h2&gt;
  
  
  Command to Auto Reload
&lt;/h2&gt;

&lt;p&gt;Now you can start you Go application with the command mentioned below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nodemon &lt;span class="nt"&gt;--exec&lt;/span&gt; go run &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;--ext&lt;/span&gt; go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;It will watch for any files with &lt;code&gt;.go&lt;/code&gt; extension in the current directory and if&lt;br&gt;
any change in the go files it will execute the &lt;code&gt;go run .&lt;/code&gt; command again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;By using nodemon you can reload your any application, not just Go, by providing the command&lt;br&gt;
to run in the &lt;code&gt;--exec&lt;/code&gt; command and specifying which extensions to watch and&lt;br&gt;
if there is any change in the provided extensions, nodemon will execute the command specified in the &lt;code&gt;--exec&lt;/code&gt; again.&lt;/p&gt;

</description>
      <category>go</category>
      <category>node</category>
      <category>npm</category>
      <category>autoreload</category>
    </item>
    <item>
      <title>How to get most starred repositories on GitHub?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Mon, 16 Nov 2020 18:16:13 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-get-most-starred-repositories-on-github-5162</link>
      <guid>https://forem.com/iaziz786/how-to-get-most-starred-repositories-on-github-5162</guid>
      <description>&lt;p&gt;It's not that easy to find most starred repositories on GitHub by your own. So here I created this video which will show you the trick on how to do it.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XVx47rgJq5s"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Give this post ❤️ if the video was helpful to you :)&lt;/p&gt;

</description>
      <category>github</category>
    </item>
    <item>
      <title>How many pods you're running in your production environment?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Thu, 28 May 2020 10:03:06 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-many-pods-you-re-running-in-your-production-environment-ml5</link>
      <guid>https://forem.com/iaziz786/how-many-pods-you-re-running-in-your-production-environment-ml5</guid>
      <description>&lt;p&gt;Assuming you are using some kind of containerization (like Docker) to deploy your production code on Kubernetes, I'm curious to know how many pods are you currently running in your production environment and how do you manage them?&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>docker</category>
    </item>
    <item>
      <title>Merge two Sorted Linked List in Go</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Wed, 27 May 2020 18:12:45 +0000</pubDate>
      <link>https://forem.com/iaziz786/merge-two-sorted-linked-list-in-go-39hc</link>
      <guid>https://forem.com/iaziz786/merge-two-sorted-linked-list-in-go-39hc</guid>
      <description>&lt;p&gt;A linked list is a linear way to structure the data so that any arbitrary number of data we can store. That means it's flexible on the memory limitation as compare to arrays.&lt;/p&gt;

&lt;h4&gt;
  
  
  What do I mean by linear?
&lt;/h4&gt;

&lt;p&gt;A linear data structure is where I can go to only one other element from the current one.&lt;/p&gt;

&lt;p&gt;One supposes you already have two linked lists and they are already sorted how do you merge it &lt;em&gt;effectively&lt;/em&gt;? What I mean by being effective is how can you swap the node in memory of a list without having to manage an extra dummy node?&lt;/p&gt;

&lt;h3&gt;
  
  
  Show me some code
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let go through each method one by one.&lt;/p&gt;

&lt;p&gt;The first two are simple. The &lt;code&gt;Print&lt;/code&gt; is to help print all the values in the linked list and &lt;code&gt;Push&lt;/code&gt; will add value at the tail of the linked list.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Output is 3 5 7 11
&lt;/h3&gt;

&lt;p&gt;As promised since both linked lists are sorted then the expected output is very simple to guess.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;var result *Node = nil&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;result&lt;/code&gt; value is what we will be returning to the caller. At the start, there isn't any result so that's why keeping it &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;moveNode(dest **Node, source **Node)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dest&lt;/code&gt; and &lt;code&gt;source&lt;/code&gt; are pointers to a struct pointer. The value of &lt;code&gt;dest&lt;/code&gt; will be a &lt;code&gt;nil&lt;/code&gt; value because of &lt;code&gt;lastPtrRef = &amp;amp;((*lastPtrRef).next)&lt;/code&gt;.  In this function what happening is, we're setting the last value to the next node of the sorted list while doing that we're also updating the &lt;code&gt;result&lt;/code&gt; variable. We're also clearing the value which should be removed from the top of the sorted list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reference
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/merge-two-sorted-linked-lists/"&gt;https://www.geeksforgeeks.org/merge-two-sorted-linked-lists/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>Finding total lines of code in your project</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Sat, 11 Apr 2020 17:05:29 +0000</pubDate>
      <link>https://forem.com/iaziz786/finding-total-lines-of-code-in-your-project-1hhd</link>
      <guid>https://forem.com/iaziz786/finding-total-lines-of-code-in-your-project-1hhd</guid>
      <description>&lt;p&gt;You may be a new joiner to your organization or someone who is already working in a firm for many years. One day you're sitting on your chair and suddenly one thought came into your mind.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many lines of code are in my project? 🤔
&lt;/h3&gt;

&lt;p&gt;One challenge could be that you might be relying on external packages, preferably open-sourced, or some code coverage folder might be already present from the last time you have run your test. You can delete them, fair point but then you have to create the coverage or install all of the dependencies again. Only if there is a way adhere my &lt;code&gt;.gitignore&lt;/code&gt; rules then the world could have been a little more perfect.&lt;/p&gt;

&lt;p&gt;Turns out, there is a way out 💡&lt;/p&gt;

&lt;h3&gt;
  
  
  Entering &lt;code&gt;git&lt;/code&gt;, like always!
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;git ls-files | xargs cat | wc -l&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Running this command will give you the total number of lines in your project.&lt;/p&gt;

&lt;p&gt;There are two parts in this process, well, as you can see there are three parts but you can think the last two commands as singular. The last two commands are there basically to count the number of lines only. The first command is a kind of interesting one which lists all the files that currently present in your repository and excludes the one who is ignored in your &lt;code&gt;.gitignore&lt;/code&gt; file. So there you have it.&lt;/p&gt;

&lt;p&gt;You can also tweak &lt;a href="https://git-scm.com/docs/git-ls-files"&gt;git ls-files&lt;/a&gt; command to check the lines for specific extensions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://unsplash.com/photos/F-Bq5wB5RY0"&gt;Image Courtesy&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Does an object passed from a function is by reference or by value in JavaScript?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Sun, 24 Nov 2019 19:32:24 +0000</pubDate>
      <link>https://forem.com/iaziz786/does-an-object-passed-from-a-function-is-by-reference-or-by-value-in-javascript-23je</link>
      <guid>https://forem.com/iaziz786/does-an-object-passed-from-a-function-is-by-reference-or-by-value-in-javascript-23je</guid>
      <description>&lt;p&gt;Lying on my bed, I started pondering one question in my mind. Whether the object passed by a function call is passed by reference or passed by value in the context of the function body?&lt;/p&gt;

&lt;h3&gt;
  
  
  How to evaluate this?
&lt;/h3&gt;

&lt;p&gt;It was confusing at first that how should I test this? Because of once a function returns something, its body is done executing, so there wasn't any mechanism to check what happens to the object if we change it outside of the function body; whether will it be changed into the function as well?&lt;/p&gt;

&lt;h3&gt;
  
  
  Enters Generators
&lt;/h3&gt;

&lt;p&gt;A generator function is a kind of function whose execution of the body can pause and can resume from the same spot from which It was paused before. So I created one simple generator function which I'll be using as the base to evaluate my theory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Aziz&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nx"&gt;rv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rv&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;Can you see the difference? It's the &lt;code&gt;*&lt;/code&gt; over there ☝️. Now let's run this function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;gen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling the above function doesn't mean that the body will start executing from the same. When we run a generator function, it returns a generator object which has a property called &lt;code&gt;next&lt;/code&gt;, when we call this method, that is when the body of the generator function starts executing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myself&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result of calling &lt;code&gt;next&lt;/code&gt; property is an object that is returned which has this type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Changing object's value
&lt;/h3&gt;

&lt;p&gt;Now, I've got the passed object &lt;code&gt;me&lt;/code&gt; to the variable &lt;code&gt;myself&lt;/code&gt;. Let's try to change its value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;myself&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mohammad Aziz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One fascinating thing about the generators is that you can also pass value from outside to the stopped position of the generator. This is done by calling the same &lt;code&gt;next&lt;/code&gt; method and by passing the parameter you want to transfer to the generator body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;gen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myself&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// logs -&amp;gt; {name: "Mohammad Aziz", age: 23} {name: "Mohammad Aziz", age: 23}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This concludes that when we pass any object from a function, then it is passed by reference.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Debugging (Also) The Standard Library In Python</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Sat, 27 Jul 2019 07:23:01 +0000</pubDate>
      <link>https://forem.com/iaziz786/debugging-also-the-standard-library-in-python-1mk4</link>
      <guid>https://forem.com/iaziz786/debugging-also-the-standard-library-in-python-1mk4</guid>
      <description>&lt;p&gt;We all have to debug our application very often in order to fix a bug, learn something new, or to understand the flow of our code, etc.&lt;/p&gt;

&lt;p&gt;Have you ever tried to debug your code in Python and more specifically in VS Code?&lt;/p&gt;

&lt;p&gt;If you have then you will find that the VS Code is not letting you get into the standard library packages. I wanted to know what is happening under the hood of those files so get a better understanding of why my code is not working as expected.&lt;/p&gt;

&lt;p&gt;I was trying to &lt;code&gt;create_connection&lt;/code&gt; with the &lt;code&gt;socket&lt;/code&gt; library. Every time when my file ran I was getting this error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flris08ut9m0knmpz8ry7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flris08ut9m0knmpz8ry7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not quite helpful for me. I wanted to put my breakpoint to where it was throwing the error. Now the question was how to do this?&lt;/p&gt;

&lt;p&gt;There are two steps for it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add a configuration file if you don't have any.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;justMyCode&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;. 🎉&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1. Add the configuration file
&lt;/h3&gt;

&lt;p&gt;It is very simple to do.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Just go to the &lt;code&gt;Debug&lt;/code&gt; tab&lt;/li&gt;
&lt;li&gt;Click on the &lt;code&gt;No Configurations&lt;/code&gt; option&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;Add Configuration...&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After that just to select any one of the prompted options, &lt;code&gt;Python File&lt;/code&gt; was in my case but VS Code also supports frameworks like &lt;code&gt;Django&lt;/code&gt;, &lt;code&gt;Flask&lt;/code&gt;, and &lt;code&gt;Pyramid&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkw9kttn4hoflo0w23d5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkw9kttn4hoflo0w23d5x.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this has been done you will see and &lt;code&gt;.vscode&lt;/code&gt; folder and in that folder, there will be a file named &lt;code&gt;launch.json&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Adding Magical Words 😄
&lt;/h3&gt;

&lt;p&gt;Now the time comes to add those magical words which will make you able to debug the files other than you code.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;launch.json&lt;/code&gt;, select the one configuration and add &lt;code&gt;justMyCode: false&lt;/code&gt;. THAT'S IT!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3z1y4tx77xdrefqdqw4m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3z1y4tx77xdrefqdqw4m.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The VS Code will now allow you to roam around the code of the standard library and you can find the exact reason why your code is not working as expected.&lt;/p&gt;

&lt;p&gt;In my case, the problem was that you don't prepend &lt;code&gt;https://&lt;/code&gt; when you are passing &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; to &lt;code&gt;socket.create_connection&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Footnotes
&lt;/h3&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/iAziz786" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cover Image: &lt;a href="https://unsplash.com/@cdr6934" rel="noopener noreferrer"&gt;Chris Ried&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/ieic5Tq8YMk" rel="noopener noreferrer"&gt;Unplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>python</category>
      <category>debugging</category>
    </item>
    <item>
      <title>How to render different components based on the selected option?</title>
      <dc:creator>Mohammad Aziz</dc:creator>
      <pubDate>Mon, 15 Apr 2019 10:13:26 +0000</pubDate>
      <link>https://forem.com/iaziz786/how-to-render-different-components-based-on-the-selected-option-51l9</link>
      <guid>https://forem.com/iaziz786/how-to-render-different-components-based-on-the-selected-option-51l9</guid>
      <description>&lt;p&gt;I want to create a page where I have more than 30 different select options. Whenever I select one of the options based on the currently selected option, a different set of React components renders below.&lt;/p&gt;

&lt;p&gt;As I have said, there are more than 30 different select options. Well, it is not necessary that for each option the combination of components will be different. There can be a one-to-many relationship between a component and the option under which the component is rendered.&lt;/p&gt;

&lt;p&gt;There are many ways to do this, but I wanted to gather some ideas before I implement one. What approach should I take to create this kind of a page?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
