<?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"><channel><title><![CDATA[Shashank Priyadarshi's Blog]]></title><description><![CDATA[Musings of a Senior Software Developer working on tech stacks from Go to R, and technologies from Web Server and Blockchain to Data Analytics to Compiler and Programming Languages.]]></description><link>https://hashnode.ssnk.in</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 05:22:23 GMT</lastBuildDate><atom:link href="https://hashnode.ssnk.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Traffic Light Simulator in Angular]]></title><description><![CDATA[Building a traffic light simulator in Angular
Context
While procrastinating on YouTube today, I came across this short video of a traffic light simulator in React.
It was then that I had an idea: why not embark on a series of articles where I constru...]]></description><link>https://hashnode.ssnk.in/traffic-light-simulator-in-angular-2023</link><guid isPermaLink="true">https://hashnode.ssnk.in/traffic-light-simulator-in-angular-2023</guid><category><![CDATA[Angular]]></category><category><![CDATA[CSS]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[npm]]></category><category><![CDATA[project]]></category><dc:creator><![CDATA[Shashank Priyadarshi]]></dc:creator><pubDate>Sat, 16 Sep 2023 21:57:42 GMT</pubDate><content:encoded><![CDATA[<h1 id="heading-building-a-traffic-light-simulator-in-angular">Building a traffic light simulator in Angular</h1>
<h2 id="heading-context">Context</h2>
<p>While procrastinating on YouTube today, I came across <a target="_blank" href="https://www.youtube.com/watch?v=5ZefcXGTQE0">this short video</a> of a traffic light simulator in React.
It was then that I had an idea: why not embark on a series of articles where I construct these delightful, small-scale projects, purely for the joy of it? And so, let's dive right in.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before getting started, let's check if we have Angular CLI, Node and npm installed on our machine using the command: <code>ng v</code>.
It should show the version of NodeJS, npm and Angular CLI installed on our machine.
We will also need docker to containerize our application, which can be checked using the following command: <code>docker --version</code>.
Check the following links to install these if they are missing:</p>
<ul>
<li><a target="_blank" href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm">NodeJS and npm</a>: Node.js is an open-source, cross-platform JavaScript runtime environment. Acronym for <code>Node Package Manager</code>, npm is typically used to install libraries, frameworks, and tools that are required for developing JavaScript applications.</li>
<li><a target="_blank" href="https://angular.io/guide/setup-local">Angular</a>: Angular is an open-source front-end web application framework for building dynamic, single-page web applications (SPAs).</li>
<li><a target="_blank" href="https://docs.docker.com/engine/install/">Docker</a>: Software and tools for building and running application in containers. Containers are lightweight, standalone, and executable packages that contain everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.</li>
</ul>
<h2 id="heading-getting-started">Getting Started</h2>
<p>Now that we have the prerequisites, let's get started using <code>ng new</code> which interactively generates an Angular project scaffolding for us. <code>ng serve --open</code> builds and runs this project locally for us and serves the page in our browser client.
We can then remove the common code from <code>app.component.html</code> file as we do not it, and create the html frame for our traffic light:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Traffic Light Simulator<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"traffic-light"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Let's also add some basic CSS to our <code>app.component.sass</code> to improve the look and feel of our traffic light:</p>
<pre><code class="lang-sass">.parent
    display: flex
    flex-direction: column
    justify-content: center
    align-items: center
    height: 90vh
    width: 99vw
    font-size: 2em

.traffic-light
    height: 90%
    width: 15%
    align-items: center
    display: flex
    flex-direction: column
    background: black

.circle
    width: 95%
    height: 30%
    border-radius: 50%
    margin: 4%

.grey
    background: grey

.red
    background: red

.yellow
    background: yellow

.green
    background: green
</code></pre>
<p>Note that I am using <code>sass</code> preprocessor selected while setting up the project. <code>css</code>, <code>scss</code>, <code>less</code> and <code>stylus</code> are the other options available. Read more about css preprocessors and Angular css preprocessor options <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Glossary/CSS_preprocessor">here</a> and <a target="_blank" href="https://blog.angular.io/the-new-state-of-css-in-angular-bec011715ee6">here</a>.</p>
<h2 id="heading-using-typescript-and-ngclass-for-a-functional-traffic-light">Using TypeScript and ngClass for a functional traffic light</h2>
<p>Now that we have the basic structure ready, let's write some code. We will be using the <code>ngClass</code> Angular directive alongside some TypeScript in our <code>app.component.ts</code> file to get our traffic light to work.</p>
<ul>
<li><a target="_blank" href="https://angular.io/api/core/Directive">Directives in Angular</a>: Angular directives are a way to extend and enhance the functionality of HTML elements in Angular templates. Directives are responsible for adding behavior, manipulating the DOM (Document Object Model), and controlling how components and templates behave. Angular provides several built-in directives, and also provides options to create custom directives.We will be using one such builtin directive, the <code>ngClass</code> directive.</li>
<li><a target="_blank" href>ngClass</a>: <code>ngClass</code> is a built-in Angular directive that allows us to dynamically add or remove CSS classes to an HTML element based on certain conditions. This is what our <code>app.component.html</code> will look like after adding <code>ngClass</code> directives:</li>
</ul>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Traffic Light Simulator<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"traffic-light"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span> [<span class="hljs-attr">ngClass</span>]=<span class="hljs-string">"red &amp;&amp; !yellow &amp;&amp; !green ? 'red' : 'grey'"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span> [<span class="hljs-attr">ngClass</span>]=<span class="hljs-string">"!red &amp;&amp; yellow &amp;&amp; !green ? 'yellow' : 'grey'"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span> [<span class="hljs-attr">ngClass</span>]=<span class="hljs-string">"!red &amp;&amp; !yellow &amp;&amp; green ? 'green' : 'grey'"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Here, we are using ngClass directive to use red, yellow, green or grey css classes on circles based on the component properties red, yellow and green.
Let's add these properties to our <code>AppComponent</code> class in <code>app.component.ts</code>:</p>
<pre><code class="lang-typescript">red = <span class="hljs-literal">true</span>;
yellow = <span class="hljs-literal">false</span>;
green = <span class="hljs-literal">false</span>;
</code></pre>
<p>Next, let's write a <code>trafficSignal</code> function that changes the color of traffic lights at set intervals:</p>
<pre><code class="lang-typescript">  trafficSignal() {
    <span class="hljs-keyword">const</span> turnOnRed = <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">this</span>.red = <span class="hljs-literal">true</span>;
      <span class="hljs-built_in">this</span>.yellow = <span class="hljs-literal">false</span>;
      <span class="hljs-built_in">setTimeout</span>(turnOnYellow, <span class="hljs-number">6500</span>); <span class="hljs-comment">// Red for 6.5 seconds</span>
    }

    <span class="hljs-keyword">const</span> turnOnYellow = <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">this</span>.red = <span class="hljs-literal">false</span>;
      <span class="hljs-built_in">this</span>.yellow = <span class="hljs-literal">true</span>;
      <span class="hljs-built_in">setTimeout</span>(turnOnGreen, <span class="hljs-number">1000</span>); <span class="hljs-comment">// Yellow for 1 second</span>
    }

    <span class="hljs-keyword">const</span> turnOnGreen = <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">this</span>.yellow = <span class="hljs-literal">false</span>;
      <span class="hljs-built_in">this</span>.green = <span class="hljs-literal">true</span>;
      <span class="hljs-built_in">setTimeout</span>(turnOnYellowAgain, <span class="hljs-number">4000</span>); <span class="hljs-comment">// Green for 4 seconds</span>
    }

    <span class="hljs-keyword">const</span> turnOnYellowAgain = <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">this</span>.green = <span class="hljs-literal">false</span>;
      <span class="hljs-built_in">this</span>.yellow = <span class="hljs-literal">true</span>;
      <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> { <span class="hljs-built_in">this</span>.trafficSignal() }, <span class="hljs-number">1000</span>); <span class="hljs-comment">// Yellow again for 1 second, then restart</span>
    }

    <span class="hljs-comment">// Start the traffic signal</span>
    turnOnRed();
  }
</code></pre>
<p>This function uses JS concepts like callbacks and setTimeout to set red, yellow and green for 6.5s, 1s and 4s respectively. Here's a brief explanation of each of these concepts:</p>
<ul>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function">callbacks</a>: An asynchronous programming construct where functions take other functions as arguments. These argument functions can then be executed once the parent functions have finished execution, hence the name callback.</li>
<li><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/setTimeout">setTimeout</a>: The JavaScript function takes a callback function and a time delay as arguments. It executes the callback function after the specified delay.</li>
</ul>
<pre><code class="lang-JavaScript"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'This will be executed after 1000 milliseconds.'</span>);
}, <span class="hljs-number">1000</span>);
</code></pre>
<p><code>trafficSignal</code> function can then be invoked from within the ngOnInit lifecycle hook which triggers an infinite event cycle resulting in a traffic light like behavior.</p>
<pre><code class="lang-TypeScript">ngOnInit() {
    <span class="hljs-built_in">this</span>.trafficSignal();
  }
</code></pre>
<p>Here, Angular concepts like lifecycle hook and change detection are used:</p>
<ul>
<li><a target="_blank" href="https://angular.io/guide/lifecycle-hooks">Angular Lifecycle Hooks</a>: Methods that allow tapping into specific moments in the lifecycle of a component or directive, providing a way to perform actions at certain stages of the component's lifecycle, such as initialization, change detection, and destruction.</li>
<li><a target="_blank" href="https://angular.io/guide/zone">Angular Change Detection</a>: Change detection is the process by which Angular checks for changes in the application's data and updates the view accordingly, ensuring data changes in our application are reflected on  the UI automatically.</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Voila! Our traffic light simulator is now ready. 
<img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYmx0eWdndnp2b20yd2czdHgyczh2bzR5Mnc5eGV3N2FzcGF1emF3dSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/MI6qhbDkJlK0tgRbUI/giphy.gif" alt="Traffic Light Simulator" />
Code for this mini project is hosted at my <a target="_blank" href="https://github.com/shashank-priyadarshi/projects/tree/main/ui/angular/traffic-light-simulator">GitHub profile</a>.
You can also build this project and bundle it in a container image ready for deployment using the following Dockerfile:</p>
<pre><code class="lang-Dockerfile"><span class="hljs-comment">#stage 1</span>
<span class="hljs-keyword">FROM</span> node:latest as node
<span class="hljs-keyword">WORKDIR</span><span class="bash"> /app</span>
<span class="hljs-keyword">COPY</span><span class="bash"> . .</span>
<span class="hljs-keyword">RUN</span><span class="bash"> npm i</span>
<span class="hljs-keyword">RUN</span><span class="bash"> npm run build --prod</span>

<span class="hljs-comment">#stage 2</span>
<span class="hljs-keyword">FROM</span> nginx:alpine
<span class="hljs-keyword">COPY</span><span class="bash"> --from=node /app/dist/resume /usr/share/nginx/html</span>
<span class="hljs-keyword">EXPOSE</span> <span class="hljs-number">4200</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[OOP in Go: Interfaces]]></title><description><![CDATA[In the last article, we discussed Go structs, and how they can be used to mimic key OOP concepts like Encapsulation, Abstraction and Inheritance.
A Go interface is a collection of method signatures that define behaviors. An interface is defined using...]]></description><link>https://hashnode.ssnk.in/oop-in-go-interfaces</link><guid isPermaLink="true">https://hashnode.ssnk.in/oop-in-go-interfaces</guid><category><![CDATA[Go Language]]></category><category><![CDATA[oop]]></category><category><![CDATA[OOPS]]></category><category><![CDATA[Interfaces]]></category><category><![CDATA[Interfaces Go]]></category><dc:creator><![CDATA[Shashank Priyadarshi]]></dc:creator><pubDate>Sat, 04 Mar 2023 13:27:41 GMT</pubDate><content:encoded><![CDATA[<p>In the last article, we discussed Go structs, and how they can be used to mimic key OOP concepts like Encapsulation, Abstraction and Inheritance.</p>
<p>A Go interface is a collection of method signatures that define behaviors. An interface is defined using the <code>type</code> keyword followed by the name of the new type and the keyword <code>interface</code>.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Ethnicity <span class="hljs-keyword">interface</span> {
    IsAsian() <span class="hljs-keyword">bool</span>
    IsIndian() <span class="hljs-keyword">bool</span>
}
</code></pre>
<p>In the above example, we define an interface type <code>Ethnicity</code> with method signature IsAsian() bool &amp; IsIndian() bool. Any type that has a method with the same signatures is said to implement the interface.</p>
<p>Let us declare a Person struct, and implement IsAsian() and IsIndian() methods with the same arguments and return types.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    name      <span class="hljs-keyword">string</span>
    country   <span class="hljs-keyword">string</span>
    continent <span class="hljs-keyword">string</span>
}

<span class="hljs-keyword">type</span> Employee <span class="hljs-keyword">struct</span> {
    Person
    salary <span class="hljs-keyword">int</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">IsAsian</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> {
    <span class="hljs-keyword">return</span> p.continent == <span class="hljs-string">"Asia"</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">IsIndian</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> {
    <span class="hljs-keyword">return</span> p.country == <span class="hljs-string">"India"</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{
    p := Person{name: <span class="hljs-string">"John"</span>, country: <span class="hljs-string">"India"</span>, continent: <span class="hljs-string">"Asia"</span>}
    c.IsAsian()
    c.IsIndian()
}
</code></pre>
<p>Since type <code>Person</code> has methods(receiver functions) with the same name and similar return types as defined in the <code>Ethnicity</code> interface, it implements the <code>Ethnicity</code> interface.</p>
<p>We could also implement the interface by simply declaring a <code>Person</code> variable and assigning it to an <code>Ethnicity</code> variable, like so:</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
    p := Person{name: <span class="hljs-string">"John"</span>, country: <span class="hljs-string">"India"</span>, continent: <span class="hljs-string">"Asia"</span>}
    <span class="hljs-keyword">var</span> e Ethnicity = p
    e.IsAsian()
    e.IsIndian()
}
</code></pre>
<p>Go also allows the embedding of interfaces in structs, which can be helpful in inheriting behaviors and defining new structs with additional behaviors.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    name      <span class="hljs-keyword">string</span>
    age       <span class="hljs-keyword">int</span>
    netIncome <span class="hljs-keyword">int</span>
    country   <span class="hljs-keyword">string</span>
    continent <span class="hljs-keyword">string</span>
    Ethnicity
}

<span class="hljs-keyword">type</span> WealthManager <span class="hljs-keyword">interface</span>{
    NetWorth() <span class="hljs-keyword">int</span>
    IsWealthy() <span class="hljs-keyword">bool</span>
}

<span class="hljs-keyword">type</span> SuperRich <span class="hljs-keyword">struct</span>{
    Person
    WealthManager
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">NetWorth</span><span class="hljs-params">()</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">return</span> p.netIncome - p.debt
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">IsWealthy</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> {
    <span class="hljs-keyword">return</span> p.NetWorth() &gt; <span class="hljs-number">1</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(s SuperRich)</span> <span class="hljs-title">IsSuperRich</span><span class="hljs-params">()</span></span> {
    fmt.Printf(<span class="hljs-string">"%v is from %v and, is super rich with a networth of %v"</span>, s.Person.name, s.Person.Country, s.Person.NetWorth())
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
    fmt.Println(<span class="hljs-string">"Hello World!"</span>)
    p := Person{name: <span class="hljs-string">"John"</span>, age: <span class="hljs-number">30</span>, netIncome: <span class="hljs-number">50000</span>, debt: <span class="hljs-number">10000</span>, country: <span class="hljs-string">"India"</span>, continent: <span class="hljs-string">"Asia"</span>}
    <span class="hljs-keyword">var</span> isSuperRich SuperRich = SuperRich{Person: p, WealthManager: p}
    isSuperRich.IsSuperRich()
}
</code></pre>
<p>Here, we have <code>Person</code> implementing the <code>WealthManger</code> interface by having two receiver functions IsWealthy() and NetWorth().</p>
<p>The <code>SuperRich</code> struct combines the properties and behaviors of <code>Person</code> and the <code>WealthManager</code>, and has additional behavior in the form of <code>IsSuperRich()</code> method.</p>
<p>In the main function, we have the isSuperRich variable, where we assign new <code>Person</code> p twice. We can assign p to <code>WealthManager</code> as the <code>Person</code> struct implements the <code>WealthManager</code> interface.</p>
<p>Interfaces with no method signatures are not bound to any behavior and hence can hold values of any type. This property allows us to achieve Polymorphism.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> x <span class="hljs-keyword">interface</span>{}
x = <span class="hljs-string">"hello"</span>
fmt.Println(x)

x = <span class="hljs-number">42</span>
fmt.Println(x)

x = <span class="hljs-literal">true</span>
fmt.Println(x)
</code></pre>
<p>Hence, we can see that interfaces in Go can allow us to mimic OOP properties like Inheritance using nesting and Polymorphism using empty interfaces. Interfaces are used to define behaviors that a type must implement. This allows functions to accept any type that implements the interface, making the code more flexible and easier to maintain.</p>
<p>In the upcoming article, we shall discuss receiver functions in Go.</p>
]]></content:encoded></item><item><title><![CDATA[OOP in Go: Structs]]></title><description><![CDATA[Go is a statically typed language that supports object-oriented programming(OOP) concepts, but is not an OOP programming language.
One of the key features of Go is its support for struct types and interfaces. A struct is similar to a class in object-...]]></description><link>https://hashnode.ssnk.in/oop-in-go-structs</link><guid isPermaLink="true">https://hashnode.ssnk.in/oop-in-go-structs</guid><category><![CDATA[Go Language]]></category><category><![CDATA[OOPS]]></category><category><![CDATA[oop]]></category><category><![CDATA[struct]]></category><dc:creator><![CDATA[Shashank Priyadarshi]]></dc:creator><pubDate>Fri, 24 Feb 2023 11:26:29 GMT</pubDate><content:encoded><![CDATA[<p>Go is a statically typed language that supports object-oriented programming(OOP) concepts, but is not an OOP programming language.</p>
<p>One of the key features of Go is its support for struct types and interfaces. A struct is similar to a class in object-oriented languages. Go structs allow mimicking OOP features such as Encapsulation, Abstraction and Inheritance.</p>
<ul>
<li><p>Encapsulation: Go structs can be used to encapsulate data and methods together.</p>
</li>
<li><p>Inheritance: A struct can contain fields of other structs, which can be used to achieve composition and code reuse.</p>
</li>
<li><p>Polymorphism: Go doesn't have traditional polymorphism like inheritance-based languages, but it provides interface-based polymorphism.*</p>
</li>
<li><p>Abstraction: By grouping related data and methods in a struct, the rest of the program can interact with it through a simple interface without knowing the underlying implementation details.</p>
</li>
</ul>
<p>In this blog post, we will discuss Go structs and their usage in the language.</p>
<p>Structs are user-defined types that group together zero or more values under a single name. In Go, a struct is defined using the <code>type</code> keyword followed by the name of the struct and the keyword <code>struct</code>.</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Person <span class="hljs-keyword">struct</span> {
    name <span class="hljs-keyword">string</span> 
    age <span class="hljs-keyword">int</span> 
    netIncome <span class="hljs-keyword">int</span>
    debt <span class="hljs-keyword">int</span>
}

<span class="hljs-keyword">type</span> Employee <span class="hljs-keyword">struct</span> {
    Person
    salary <span class="hljs-keyword">int</span>
}
</code></pre>
<p>In the above example, we created <code>Person</code> and <code>Employee</code> structs. Note that the <code>Employee</code> struct implements the <code>Person</code> struct but also has an additional field, salary. We can create a new <code>Person</code> object by initializing its fields with values.</p>
<pre><code class="lang-go">p := Person{name: <span class="hljs-string">"John"</span>, age: <span class="hljs-number">30</span>, netIncome: <span class="hljs-number">50000</span>, debt: <span class="hljs-number">10000</span>}
</code></pre>
<p>Here, we created a <code>Person</code> p and populated the fields with values. We can now have p as an <code>Employee</code> with a salary of ₹60,000.</p>
<pre><code class="lang-go">e := Employee{Person: p, salary: <span class="hljs-number">60000</span>}
</code></pre>
<p>Structs can also have methods associated with them, which can be defined as regular functions, with additional receiver arguments.</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(p Person)</span> <span class="hljs-title">NetWorth</span><span class="hljs-params">()</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">return</span> p.netIncome - p.debt
}
</code></pre>
<p>In the above example, we defined a method NetWorth() that calculates the net worth of p. By defining the method with p as receiver argument, we can access the fields of p.</p>
<p>In this blog post, we explored the basics of defining and using Go structs by creating a Person and Employee struct with fields and methods, and initializing their values. We also briefly touched on receiver arguments, which are used in defining methods associated with structs.</p>
<p>* Interfaces and Receivers will be covered in detail in future blog posts.</p>
]]></content:encoded></item><item><title><![CDATA[About me!]]></title><description><![CDATA[As an elementary school kid, and even later, I remember having participated in multiple public speaking events, and eventually forgetting what I was supposed to speak, on stage, in front of an audience. And when you have friends who clap hard when yo...]]></description><link>https://hashnode.ssnk.in/article-1</link><guid isPermaLink="true">https://hashnode.ssnk.in/article-1</guid><category><![CDATA[first post]]></category><category><![CDATA[First Blog]]></category><category><![CDATA[#first-article]]></category><category><![CDATA[First]]></category><dc:creator><![CDATA[Shashank Priyadarshi]]></dc:creator><pubDate>Tue, 14 Feb 2023 16:22:35 GMT</pubDate><content:encoded><![CDATA[<p>As an elementary school kid, and even later, I remember having participated in multiple public speaking events, and eventually forgetting what I was supposed to speak, on stage, in front of an audience. And when you have friends who clap hard when you get on stage, and even harder when you forget everything and run off stage, I'd say I've had my moments.</p>
<p>So after pushing trying to do this for more than a month now, I finally have some courage and I hope I will get the same tough applause from you, that my friends gave me, if I decide to run off.</p>
<p>Hi, I am Shashank, I am a web dev by profession, and I like to tinker. I am trying to learn a few things about TypeScript, Angular, Go, Docker, Docker Compose and Kubernetes, so you can expect articles on my blog to be about these technologies.</p>
]]></content:encoded></item></channel></rss>