<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akash Jain on Medium]]></title>
        <description><![CDATA[Stories by Akash Jain on Medium]]></description>
        <link>https://medium.com/@perennialsky?source=rss-259a9539de52------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gAlpWY9BjsVwErrxql4fOw.jpeg</url>
            <title>Stories by Akash Jain on Medium</title>
            <link>https://medium.com/@perennialsky?source=rss-259a9539de52------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 17:30:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@perennialsky/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implements proto file — Protocol Buffer]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://perennialsky.medium.com/implements-proto-file-protocol-buffer-b296e9ce219b?source=rss-259a9539de52------2"><img src="https://cdn-images-1.medium.com/max/1134/1*b7siaQBHwkuz2gDmv8ygHg.png" width="1134"></a></p><p class="medium-feed-snippet">Protocol Buffers provide a method for encoding data in a format that is both easy for machines to read, transfer, and process, as well as&#x2026;</p><p class="medium-feed-link"><a href="https://perennialsky.medium.com/implements-proto-file-protocol-buffer-b296e9ce219b?source=rss-259a9539de52------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://perennialsky.medium.com/implements-proto-file-protocol-buffer-b296e9ce219b?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/b296e9ce219b</guid>
            <category><![CDATA[grpc]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Wed, 10 May 2023 18:31:37 GMT</pubDate>
            <atom:updated>2023-05-10T18:31:37.084Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Kubernetes Architecture and Run Locally using Minikube]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/kubernetes-architecture-and-run-locally-using-minikube-1cfb7bd5f10e?source=rss-259a9539de52------2"><img src="https://cdn-images-1.medium.com/max/2600/1*LvbLjdoMpLntUFM6ldrlXg.jpeg" width="5104"></a></p><p class="medium-feed-snippet">Prerequisite</p><p class="medium-feed-link"><a href="https://medium.com/swlh/kubernetes-architecture-and-run-locally-using-minikube-1cfb7bd5f10e?source=rss-259a9539de52------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/kubernetes-architecture-and-run-locally-using-minikube-1cfb7bd5f10e?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cfb7bd5f10e</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[minikube]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Mon, 01 May 2023 15:32:12 GMT</pubDate>
            <atom:updated>2023-05-01T15:32:12.698Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Progressive Web Apps (PWAs )]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://perennialsky.medium.com/progressive-web-apps-pwas-504cb08923d5?source=rss-259a9539de52------2"><img src="https://cdn-images-1.medium.com/max/2560/1*D3D7BQC2k8IgvR_WEQK4IQ.jpeg" width="2560"></a></p><p class="medium-feed-snippet">In the last 4 - 5 years, mobiles became the primary source to surf the internet. Many companies have shut down their website and launched&#x2026;</p><p class="medium-feed-link"><a href="https://perennialsky.medium.com/progressive-web-apps-pwas-504cb08923d5?source=rss-259a9539de52------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://perennialsky.medium.com/progressive-web-apps-pwas-504cb08923d5?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/504cb08923d5</guid>
            <category><![CDATA[application]]></category>
            <category><![CDATA[pwa]]></category>
            <category><![CDATA[progressive-web-app]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[apps]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Fri, 15 Jan 2021 10:44:28 GMT</pubDate>
            <atom:updated>2021-01-15T10:44:28.157Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Handle CORS in Golang]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://perennialsky.medium.com/handle-cors-in-golang-7c5c3902dc08?source=rss-259a9539de52------2"><img src="https://cdn-images-1.medium.com/max/1200/1*caY-bPuDZVyoz_Nb9kpUVg.png" width="1200"></a></p><p class="medium-feed-snippet">CORS stands for Cross-Origin Resource Sharing, it&#x2019;s a mechanism that allow browser to access the content of other websites on their&#x2026;</p><p class="medium-feed-link"><a href="https://perennialsky.medium.com/handle-cors-in-golang-7c5c3902dc08?source=rss-259a9539de52------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://perennialsky.medium.com/handle-cors-in-golang-7c5c3902dc08?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c5c3902dc08</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[cors]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Sat, 10 Oct 2020 10:14:38 GMT</pubDate>
            <atom:updated>2021-05-13T08:06:41.126Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Type Assertion and Type Conversion in Golang]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/type-assertion-and-type-conversion-in-golang-11fd98f69a3f?source=rss-259a9539de52------2"><img src="https://cdn-images-1.medium.com/max/1200/1*2i3vchmX0k_A7Kzj3n7Bmw.jpeg" width="1200"></a></p><p class="medium-feed-snippet">Type assertion is to get value in interface and Type conversion is a way to convert a variable from one data type to another data type.</p><p class="medium-feed-link"><a href="https://medium.com/swlh/type-assertion-and-type-conversion-in-golang-11fd98f69a3f?source=rss-259a9539de52------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/type-assertion-and-type-conversion-in-golang-11fd98f69a3f?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/11fd98f69a3f</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[type-assertion]]></category>
            <category><![CDATA[type-conversion]]></category>
            <category><![CDATA[type-casting]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Mon, 10 Aug 2020 18:40:10 GMT</pubDate>
            <atom:updated>2020-08-11T08:36:05.956Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Proxy Server in Golang]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/proxy-server-in-golang-43e2365d9cbc?source=rss-259a9539de52------2"><img src="https://cdn-images-1.medium.com/max/2600/1*VVCEVkLeeyFa9ThmDpyoag.png" width="4096"></a></p><p class="medium-feed-snippet">Proxy means someone has the power or authority to do something for someone else or pretends to be someone else.</p><p class="medium-feed-link"><a href="https://medium.com/swlh/proxy-server-in-golang-43e2365d9cbc?source=rss-259a9539de52------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/proxy-server-in-golang-43e2365d9cbc?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/43e2365d9cbc</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[proxy-server]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Tue, 04 Aug 2020 10:01:28 GMT</pubDate>
            <atom:updated>2021-04-18T17:49:05.646Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Build your chrome extension]]></title>
            <link>https://perennialsky.medium.com/build-your-chrome-extension-a358b9d68b5d?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/a358b9d68b5d</guid>
            <category><![CDATA[chrome-extension]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 04:21:41 GMT</pubDate>
            <atom:updated>2021-01-15T10:47:09.592Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bVw-FZMqsq3VVCSUld1wtA.png" /></figure><p>You must have used chrome extension many times in your life. Have you ever wonder how these extensions can be created, No? Well, it’s not like you have to learn a new programing language or a tool.</p><p>Having a basic understanding of HTML, CSS, JAVASCRIPT is like a cherry on the cake to create this extension.</p><p>Let’s see file structure first</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*rHofWNpFsC34sXE92P89ag.png" /></figure><p><em>manifest.json </em>is the main file for a chrome extension, the browser starts with this file and read line by line. See how this file content looks</p><pre>{<br>  &quot;manifest_version&quot;: 2,<br>  &quot;name&quot;: &quot;Debarred Tab&quot;,<br>  &quot;version&quot;: &quot;0.1&quot;,<br>  &quot;content_scripts&quot;: [<br>    {<br>      &quot;matches&quot;: [<br>        &quot;&lt;all_urls&gt;&quot;<br>      ],<br>      &quot;js&quot;: [&quot;content.js&quot;]<br>    }<br>  ],<br>  &quot;background&quot;: {<br>    &quot;scripts&quot;: [&quot;background.js&quot;]<br>  },<br>  &quot;web_accessible_resources&quot;: [<br>    &quot;debarredPage.html&quot;<br>  ],<br>  &quot;permissions&quot;:[<br>    &quot;storage&quot;,<br>    &quot;tabs&quot;<br>  ],<br>  &quot;browser_action&quot;: {<br>    &quot;default_icon&quot;: &quot;DebarredTab.png&quot;,<br>    &quot;default_popup&quot;: &quot;popup.html&quot;,<br>    &quot;default_title&quot;: &quot;Debarred Tab&quot;<br>  },<br>  &quot;options_page&quot;: &quot;options.html&quot;<br>}</pre><blockquote><a href="https://developer.chrome.com/extensions/manifest">Official Documentation</a> of chrome extension’s manifest file</blockquote><p>The name, manifest_version, and version keys are required keys used to define basic information about chrome extension.</p><p>Now two important keys are there which need little explanation.</p><p>Divide the browser into two parts, the first one is where web pages are rendered and the second one is all tabs, toolbar, etc</p><ul><li><em>content_scripts</em>: All javascript files inside this key are added in all the web pages that render and after that JS files are the part of those web pages. If we want we can change the content of any web page using javascript. It has one more key `<em>matches</em>` which is used to define the URL for which javascript files are to be added.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g96kLguLkMKx90zLN9zVeQ.png" /></figure><ul><li><em>background: </em>All javascript files inside this key is used to manipulate URL, tab events such as navigating to a new page, removing a bookmark, or closing a tab.</li></ul><p><em>browser_action</em> key is used to define extension icon, its title, and popup page that will display on click.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*iZZVkYh4bUFQwdWBIpvSGA.png" /></figure><p><em>options_page </em>key is used to define the configuration page for the extension. If the Configuration page is not required then no need to mention this key. The configuration page is a simple HTML page which takes the form data and saves it in chrome itself using its API</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TxTc9bJp_BZx64HHcRODWw.png" /></figure><p>Here I am taking this dropdown value and on clicking the save button calling js code to save this data using chrome API</p><pre>chrome.storage.sync.set({&lt;name&gt;: &lt;value&gt;}, function(items) {});</pre><p><em>permissions </em>key is used to taking permissions from chrome to access the particular resources, here we are using `storage`, `tab`.</p><p><em>web_accessible_resources: W</em>hen we use any file of the extension directly we need to mention that file here.</p><p>Sample:</p><pre>chrome-extension://&lt;chrome-extension-ID&gt;/&lt;Any page of extension&gt;.html</pre><p>You can see all files for an extension here: <a href="https://github.com/akashjain132/debarredTab">Debarred Tab</a></p><h3>Run the extension</h3><p>Open the extension page of the chrome browser and load the extension.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q_GrAd5rBYgbJCM26db2_Q.png" /></figure><p>On top right section you will see a checkbox with the developer mode text, enabling this will show three more options from which click <em>Load unpacked </em>button and select the whole folder of chrome extension.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EcGysqxay3AeOm4c4RNkoA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2K0089S-t9h1BINp1oVNTA.png" /></figure><blockquote>Every chrome extension have a unique ID which is used to identify extension.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a358b9d68b5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How String works in Golang]]></title>
            <link>https://perennialsky.medium.com/how-string-works-in-golang-7ac4d797164b?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ac4d797164b</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Sun, 17 May 2020 14:26:16 GMT</pubDate>
            <atom:updated>2023-09-15T12:20:52.717Z</atom:updated>
            <content:encoded><![CDATA[<h3>@ How String works in Golang?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*uNRjsyRmQTTAzBidzG7HRQ.jpeg" /></figure><p>Computer understands only two things 0 and 1 and all programming languages know how to show these 0 and 1’s in a monitor sometimes as numeric, alphabets or some special symbols because of their type. Defining variable explicitly tell compiler its type whether we will store integer, string, float in it and when we get a value from a particular variable then the program take it from memory in the form of 0 and 1’s and then based on its type, it converts that 0 and 1s in that type and gives output.</p><pre>var age int;</pre><pre>age = 26</pre><p>In RAM (memory) it is stored like this `11010` but when compilers fetch it, first it looks at its type and converts accordingly, in this case it converts into 26.</p><p>Modern computers support multilingual language. It can show all symbols that exist in the world whether it’s a Chinese, Spanish or any other language symbol and to do so the computer has a list of all symbols with his decimal number. You can see <a href="https://unicode-table.com/">here</a>.</p><p>For `$`symbol it stores like <em>100100 (</em>36<em>) or </em>㿝 stores like <em>11111111011101 (</em>16349<em>)</em></p><p><strong>What is UTF-8 encoding</strong></p><blockquote>UTF stands for Unicode Transformation Format. The ‘8’ means it uses 8-bit blocks to represent a character.</blockquote><p>UTF-8 is just a method to represent a binary number to send any symbol with a minimum number of chunks. In UTF-8, 1 chunk means 8 bit of data. Send $ symbol, software will convert this number to binary <em>100100 and send this binary in one chunk that’s 8 bits `00100100`.</em></p><p>Like 㿝 symbol, its binary is <em>11111111011101 so to convert this binary in UTF-8 we need 3 chunks that’s it 3*8 = 24 bits`</em>11100011` `10<em>111111</em>` `10<em>011101</em>`</p><p>You will see some more extra bits of data here, it is because when anyone reads these series of bits they will know the number of chunks for a symbol.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4wehjiu0pgxwyo-Dvo89Q.png" /><figcaption>1.1 UTF-8 Bytes Allocation</figcaption></figure><p>In above picture you will understand if we want to encode any symbol in UTF-8 then we can simple do following steps</p><ul><li>Just find out decimal number of that symbol</li><li>Convert that decimal number to binary number</li><li>Find out how many bytes (chunks) they need to do encoding</li><li>Replace x with appropriate binary digit</li></ul><p><strong>String in Golang</strong></p><p>string is its own data type internally it’s a sequence of bytes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AmD3PIV5vUr5GOf-NpNA3g.png" /><figcaption><a href="https://play.golang.org/p/eEQeskF6sxL">https://play.golang.org/p/eEQeskF6sxL</a></figcaption></figure><p>Shocking?? you must be thinking the output should come like this `H e l l o` as I earlier said string is sequence of bytes and bytes is alias of uint8, it means each byte stores only integer value of a character.</p><p>H =&gt; 72, E =&gt; 101 L =&gt; 108 L =&gt; 108 O =&gt; 111</p><p>In Go, string uses UTF-8 encoding, lets see how RAM (memory) stored`Hello` string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4SpkXQbH32L5IJ8rtXYSGA.png" /></figure><p>If you see the first bit of every character is 0 this represents every character uses one bytes (one chunk).</p><p>Lets see some other string which have some special character like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NzL_CxYfy1YRLV6byatJMA.png" /><figcaption><a href="https://play.golang.org/p/a5Py9bLApdm">https://play.golang.org/p/a5Py9bLApdm</a></figcaption></figure><p>If you see, “HelᏝo” has 5 characters word in which one is special character which has decimal number 5085 but when we do iteration we got 7 decimal numbers, confuse? First Let’s see memory allocation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*McIAqOopsdUnnh-yuf1w4g.png" /></figure><p>Ꮭ special character uses 3 bytes in UTF-8 encoding, please refer 1.1 image.</p><p>when we do iteration over string it reads byte by byte and on a special character loop read it 3 bytes as 3 different characters not single one that’s why they printed 3 different characters (2 characters are non-visible). To avoid this issue we have a concept of `rune`. Actually I will call it a trick instead of concept.</p><p>Rune is an alias of int32, as I told you UTF-8 is 1 - 4 bytes range so Go uses int32 for max characters and when we do a range loop over this string then it gives the whole int32 instead of bytes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zwwba9QEPWASmCoefXmLCw.png" /><figcaption><a href="https://play.golang.org/p/3MwMdPzEkrA">https://play.golang.org/p/3MwMdPzEkrA</a></figcaption></figure><p>Interestingly if we see the index it jumps from 3 to 6 because range read byte by byte and understand UTF-8 encoding so its use rune type and get the exact string.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ac4d797164b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory Allocation for a Struct in golang]]></title>
            <link>https://perennialsky.medium.com/memory-allocation-for-a-struct-in-golang-b5057b8ccf23?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5057b8ccf23</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[struct]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Sun, 19 Apr 2020 08:41:23 GMT</pubDate>
            <atom:updated>2020-08-04T19:36:52.591Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*mAkQjlvFDd147QDrk21Z-w.jpeg" /></figure><p>The user-defined data type is the most important quality of any programming language through which we can define our data type. Go is not being novel here. Lots of languages do that, so there are some important things I want to share. I am not gonna tell you what is a user-defined data type but I will explain how it manages in Go.</p><p>Let’s see the syntax of user-defined data type in Go.</p><pre>type Employee struct {<br>  ID int  <br>  Name string<br>  Age int16<br>  Gender string<br>  Active bool<br>}</pre><p>System defined type occupied some space in memory like below (Assume we are running on a 64-bit machine)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*79tgAEw5b1AiDc8CuHfD6w.png" /></figure><p>Now, what if I ask you the size of the Employee struct and then you simply add up of all data type used in that struct.</p><pre>Employee struct size = 8(ID) + 16(Name) + 2(Age) + 16(Gender) + 1(Active) = 43 bytes</pre><p>I will not say it’s a bad guess but as the old saying “Everything you see is not always the real”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1adc6aeb78cfb0e590e2f2a92e755a7/href">https://medium.com/media/a1adc6aeb78cfb0e590e2f2a92e755a7/href</a></iframe><p>Run this program and you will find out</p><pre>Size of main.Employee struct: 56 bytes</pre><p>It’s shocking, right? But will explain to you why this happened and before going into this we need to understand what 64-bit machine is?</p><p><strong>64-bit</strong> is a type of CPU architecture that is capable of transferring 64 <a href="https://www.computerhope.com/jargon/b/bit.htm">bits</a> of data per clock cycle. It is the amount of information that your CPU can process each time it operates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DFREcbztMhJeK2X-_1lXvA.png" /></figure><p>Just to explain above architecture I wanna tell you one thing imagine you work in a supermarket and one time a truck comes to the store with lots of stuff and now you are the only one person who needs to unload that truck now you have to go to that truck and takes two bags from it with your bare hand and go to supermarket put that bag there and come back to the truck and do the same process again.</p><p>Similarly in 64-bit machine CPU takes 64 bit of data from memory and does the operation and sends back processed data back to memory and if the machine is 32-bit then it takes 32 bit of data from memory.</p><p>When we make data type it occupies memory in RAM sequentially as above Employee struct do as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0GJ2SMHF4B7Zr6BpwiBbow.png" /><figcaption>Memory Representation</figcaption></figure><p>CPU takes this data in chunks of 64 bits so, in the first cycle of CPU, it takes int (8)[ID] and half of the string (16)[Name], in the second cycle it takes half of the string (16)[Name] and int16 (2)[Age], in the third cycle, it takes half of the string (16)[Gender] and in the fourth cycle, it takes half of string (16)[Gender] in the fifth cycle, it takes bool (1)[Active]. It means reading string (16) will take two cycles of CPU so to save CPU cycle hardware uses alignment concept in which variable takes spaces in multiple data types. see below to understand</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*U-kFH-LUShcEbw9UgtgEYQ.png" /></figure><p>Here in the 4th cycle, it takes only 2 bytes but CPU could take 8 bytes (64 bit) but because of the alignment concept in 4th cycle 6 bytes memory will be wasted and similarly in the 7th cycle, it takes only 1 byte and 7 bytes will be wasted. It means a total of 13 bytes memory is of no use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eoJOoDv5Csqg-zPq8WXXnA.png" /></figure><p>So if we add all memory (7 cycles * 64 = 448 bit = 56 bytes) you will find out why we got 56 bytes memory of employee struct.</p><p>Now there are very important things that you can notice here what if we combined the 4th and 7th cycle and make it one cycle then it will reduce the memory but how is it possible? Simply just rearrange your struct fields.</p><pre>type Employee struct {<br>  Name string<br>  Gender string<br>  ID int<br>  Age int16<br>  Active bool<br>}</pre><p>Here we put fields in descending order of element’s memory size and now let’s execute the below script and see the size of employee struct, it’s shocking</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf2e5cd1a460546a9068bb444dcaa38a/href">https://medium.com/media/bf2e5cd1a460546a9068bb444dcaa38a/href</a></iframe><pre>Size of main.Employee struct: 48 bytes</pre><p>No need to explain anything here just see below table and you will understand it by yourself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n5H3WJY0aSm7rsZdOQ3brA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*REBfvwAQc-h8BCc26194dg.png" /></figure><p>Here we wasted only 5 bytes of memory so you just saw here ordering elements in struct makes memory allocation differently.</p><p><strong>Conclusion:</strong> The best way to create a struct is to put all fields in descending order of data type size so that it reduces the cycle of CPU utilization and memory in RAM.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5057b8ccf23" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang Package Manager GO Module]]></title>
            <link>https://perennialsky.medium.com/basic-understanding-of-golang-package-manager-go-module-8f909e8c6de7?source=rss-259a9539de52------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f909e8c6de7</guid>
            <category><![CDATA[package-management]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Akash Jain]]></dc:creator>
            <pubDate>Sat, 21 Mar 2020 14:43:04 GMT</pubDate>
            <atom:updated>2020-08-04T19:38:26.631Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang Package Manager: GO Module</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dF3yRJd9wJVFjOutvlx7KQ.jpeg" /></figure><p>Before GO 1.11 version, there were no official package manager of golang but there were a lot of CLI introduced by the open source community to manage go packages like dep, glide, gopkg.in etc</p><p>In 1.11 version, Go introduced their official package manager <strong>Go Module.</strong></p><p>Previously Google was thinking of some other name like version go (vgo) and then later they all agreed with go module. Firstly understand what is module?</p><p>Suppose we have go project which has path `<em>github.com/akashjain132/taxpayer</em>` and we are importing many packages like</p><pre>import (<br>  fmt<br>  net/http<br>  github.com/gorilla/mux<br>  github.com/sirupsen/logrus<br>  github.com/akashjain132/taxpayer/invoice<br>)</pre><p>Packages are divided into three categories</p><ul><li><strong>Standard Packages</strong>: fmt, net/http etc all are standard packages which are maintained by golang itself.</li><li><strong>Application Packages</strong>: github.com/akashjain132/taxpayer/invoice is application packages that are managed by the developers.</li><li><strong>External Packages</strong>: github.com/gorilla/mux, github.com/sirupsen/logrus are external packages that we have to download from the internet.</li></ul><p>Go module considers only external packages. Suppose you have downloaded one external package “<a href="https://github.com/pmezard/go-difflib"><em>github.com/pmezard/go-difflib</em></a><em>”. </em>This package has no dependency on any other external package then we can call it a module.</p><p>Another example: “<a href="https://github.com/pkg/errors"><em>github.com/pkg/errors</em></a><em>”</em> This package depends on two other external packages “<a href="https://golang.org/x/crypto"><em>golang.org/x/crypto</em></a><em>”</em> and “<em>golang.org/x/syn”</em>, it means all combined packages make a module</p><p>So we can say in simple words that</p><blockquote><em>A collection of packages with potential dependencies is called a module.</em></blockquote><figure><img alt="Go module" src="https://cdn-images-1.medium.com/max/684/1*5ntjV83Kz3qL8DZpXqhGow.png" /></figure><h3>How to start Go Module</h3><pre>$ go mod init</pre><p>Run this command in your project root directory and it will create two files <em>go.mod</em> and <em>go.sum. </em>We will see <em>go.sum</em> file later, the only thing that is crucial for now is go.mod</p><p>Go.mod looks like</p><pre>module github.com/akashjain132/taxpayer</pre><pre>go 1.14</pre><p>Go treats your project as a module. The <strong>module</strong> key is the first word in go.mod file which tells the compiler your module name.</p><p>Total 4 directives that we can use in go.mod file:</p><ul><li><strong>Module</strong></li><li><strong>Require</strong></li><li><strong>Replace</strong></li><li><strong>Exclude</strong></li></ul><p>Before jumping into go.mod file content, first get familiar with new ENV variables<em>. </em>You must have heard the GOPATH env variable, it has the path where all Go related code lives.</p><pre>$ echo $GOPATH</pre><pre>/Users/akashjain/web/go</pre><p>If we want to create a new project in golang we must have all code inside the GOPATH folder otherwise it won’t compile.</p><p>Go introduces a new env variable in 1.11 version which is GO111MODULE. The default value of this env variable is “auto”. The possible value of this env variable is “on”, “auto” and “off”.</p><p>If its “on” (go module is enabled) then go compiler ignores GOPATH and now you can put your go code anywhere and your project root folder must have go.mod and go.sum files. With “off” value (go module is disabled) you are restricted to put your code inside GOPATH and the compiler will not take care of the go.mod file and “AUTO” will automatically check your code path and behave accordingly and if they found go.mod and go.sum file, then the compiler will behave as go module is enabled.</p><p>Now let’s write a code in main.go file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b85db6cb0adb93cd784f3aebcc87180/href">https://medium.com/media/4b85db6cb0adb93cd784f3aebcc87180/href</a></iframe><p>We are using lorgus external package in our code, now just do</p><pre>$ go run main.go</pre><pre>Hello world</pre><p>And now see go.mod file</p><pre>module github.com/akashjain132/taxpayer</pre><pre>go 1.14</pre><pre>Require (<br>  <a href="https://github.com/sirupsen/logrus">github.com/sirupsen/logrus</a> v1.4.2<br>)</pre><p>Go run command adds all required external packages that have been used in a project with the latest tag. If you want to add a specific tag then use go get command.</p><pre>Syntax: go get module@&lt;tag&gt;</pre><pre>go get <a href="https://github.com/sirupsen/logrus">github.com/sirupsen/logrus</a>@v1.4.2</pre><p>Suppose that package hasn’t released any tag, then it will get the latest commit from the master branch. If you want to get an update from any other branch, mention that branch name</p><pre>go get <a href="https://github.com/sirupsen/logrus">github.com/sirupsen/logrus</a>@&lt;branch name&gt; // default: master branch</pre><p>And if you want up to particular commit</p><pre>go get <a href="https://github.com/sirupsen/logrus">github.com/sirupsen/logrus</a>@&lt;Commit ID&gt;</pre><p>Till now we have learnt two directives of go.mod file i.e., <em>module</em> and <em>require.</em> Let&#39;s go further to see other directives.</p><p>Replace directive is used to replace a particular package with other package, let’s have a look at go.mod file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba954b8c5b4756dac95da2da8e9e5301/href">https://medium.com/media/ba954b8c5b4756dac95da2da8e9e5301/href</a></iframe><p>This mod file will download logrus module of version v1.4.1 instead of v1.4.2. Now suppose that we have changed the whole package with some other package</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3f77e66b8300d407e7a330428a033bf/href">https://medium.com/media/e3f77e66b8300d407e7a330428a033bf/href</a></iframe><p>This mod file will download the seelog package instead of logrus package.</p><p>Exclude directive is used to exclude a particular package to download, let’s have a look in go.mod file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d76e5f04f6093884419081e85eb590f5/href">https://medium.com/media/d76e5f04f6093884419081e85eb590f5/href</a></iframe><p>Now go compiler tries to download logrus v1.4.1 but due to exclude statement it will not download that version so complier will try to look higher version and download logrus v1.4.2</p><p>We have learnt about the go module file, now let see how it works</p><p>When we run go command for a test, build or run, it reads the go.mod file and search the package locally, if it’s not present there then it will download the package from the internet.</p><p>Now one question arises here that, if compiler already found package locally then how will it make sure that anyone has touched that package or not?</p><p>So, to check that, go module have one more file go.sum, it looks like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bad84e725a2052a6add0aaf2bbc9fd51/href">https://medium.com/media/bad84e725a2052a6add0aaf2bbc9fd51/href</a></iframe><p>Here in front of each package name, we have some hash value which was created by the compiler. This hash was created by using every line of that package. So, when the compiler reads go.mod file and found it locally then it creates a hash and matches with go.sum file and that’s how they verify that package is not modified by anyone.</p><p>I hope you are now familiar with the basic go module. You can visit <a href="https://github.com/golang/go/wiki/Modules">Go Modules</a> for more information.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f909e8c6de7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>