<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shashank Shekhar</title>
    <description>The latest articles on DEV Community by Shashank Shekhar (@ssshekhu53).</description>
    <link>https://dev.to/ssshekhu53</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%2F672746%2Fa8faec8b-0d06-429a-8756-8bcce347597a.png</url>
      <title>DEV Community: Shashank Shekhar</title>
      <link>https://dev.to/ssshekhu53</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ssshekhu53"/>
    <language>en</language>
    <item>
      <title>Building a Playful File Locker with GoFr</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Fri, 19 Apr 2024 10:38:42 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/building-a-playful-file-locker-with-gofr-3f8a</link>
      <guid>https://dev.to/ssshekhu53/building-a-playful-file-locker-with-gofr-3f8a</guid>
      <description>&lt;p&gt;Forget complex permission schemes, sometimes you just want a quick and dirty way to lock a file. This guide takes you on a fun adventure to create a basic file locker command-line tool using GoFr, specifically focusing on understanding CLI tool development rather than building a production-ready application.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is GoFr?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://gofr.dev/"&gt;GoFr&lt;/a&gt; is an opinionated Go framework for accelerated microservice development. It takes an "opinionated" approach, meaning it has a specific way of doing things that streamlines development. This makes Gofr ideal for creating robust and scalable web applications without a lot of boilerplate code.&lt;/p&gt;

&lt;p&gt;Gofr is designed to be familiar and user-friendly for developers, even those new to Go. It provides a clear and consistent way to structure your web application, making it easier to understand and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building Our Playful File Locker
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Setting the Stage:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure you have Go installed &lt;a href="https://go.dev/"&gt;https://go.dev/&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create a project directory and initialize a Go module within it using go mod init &lt;strong&gt;&lt;/strong&gt;. Replace &lt;strong&gt;&lt;/strong&gt; with a fun name for your locker tool!&lt;/li&gt;
&lt;li&gt;Add &lt;a href="https://github.com/gofr-dev/gofr"&gt;gofr&lt;/a&gt; package to the project using the following command:
&lt;code&gt;go get gofr.dev&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Directory structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;├── file-locker
│   ├── handlers
│   │   ├── unix
│   │   │   ├── handler.go
│   ├── services
│   │   ├── unix
│   │   │   ├── service.go
│   │   ├── crypt
│   │   │   ├── crypt.go
│   ├── constants
│   │   ├── contants.go
│   ├── main.go
│   ├── go.mod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Service Layer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Let's add interface for the service. Add following code to &lt;code&gt;services/interfaces.go&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;FileLocker&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;Init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&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;error&lt;/span&gt;  
    &lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;  
    &lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&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;error&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;Crypt&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;Encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;creds&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="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;  
    &lt;span class="n"&gt;Decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cred&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's add implementation for interfaces. Add following code to &lt;code&gt;services/unix/service.go&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
     &lt;span class="n"&gt;fileName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;`private`&lt;/span&gt;  
     &lt;span class="n"&gt;hiddenFileName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;`.private`&lt;/span&gt;  
     &lt;span class="n"&gt;encryptedDataFileName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;`.encrypted-data`&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;unix&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;crypt&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Crypt&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;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Crypt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileLocker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;unix&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;crypt&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="p"&gt;(&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;unix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&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;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file locker already initialized"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Mkdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModePerm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;".nomedia"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;encryptedPassword&lt;/span&gt; &lt;span class="o"&gt;:=&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;crypt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encrypt&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="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  

     &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encryptedDataFileName&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;encryptedPassword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModePerm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&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;unix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ErrNotExist&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;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file locker not initialized or is still locked"&lt;/span&gt;&lt;span class="p"&gt;)&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;err&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;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hiddenFileName&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="p"&gt;(&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;unix&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&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;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hiddenFileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ErrNotExist&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;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file locker not initialized or is still unlocked"&lt;/span&gt;&lt;span class="p"&gt;)&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;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hiddenFileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encryptedDataFileName&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;decryptedData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decryptedData&lt;/span&gt;&lt;span class="p"&gt;)&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;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"unauthorized"&lt;/span&gt;&lt;span class="p"&gt;)&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;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hiddenFileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fileName&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Struct `unix` implements `FileLocker` interface.
- `New` is the factory function that returns a `FileLocker` instance
- `Init` method will create a directory named `private` in the current directory and will store the encrypted password in file named `.encrypted-data`. If `private` directory is already present or there occurs some error while creating it then `Init` method will return error otherwise will return nil.
- `Lock` method will hide the `private` directory by renaming it to `.private`. If the `private` directory is not present in the current directory then it means that either `file-locker` has not been initialised or it is already in locked state.
- `Unlock` method will read the password from `.private/.encrypted-data` file and compare with the password received in the parameter. If it matches, then it will unhide the `private` file otherwise return `unauthorized` error.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Add the following code to &lt;code&gt;services/crypt/crypt.go&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;`qwertyuiopasdfghjklzxcvb`&lt;/span&gt;  

  &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;crypt&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;block&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Block&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;New&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Crypt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;aes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewCipher&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="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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="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="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&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="n"&gt;aes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BlockSize&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;aes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BlockSize&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  

     &lt;span class="n"&gt;cfb&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewCFBEncrypter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;cipherText&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="n"&gt;cfb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;XORKeyStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cipherText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

     &lt;span class="n"&gt;dst&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&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="n"&gt;base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StdEncoding&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EncodedLen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cipherText&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;  

     &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StdEncoding&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cipherText&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;dst&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&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="n"&gt;aes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BlockSize&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  

     &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;aes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BlockSize&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  
     &lt;span class="n"&gt;cfb&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewCFBDecrypter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

     &lt;span class="n"&gt;cipherText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;base64&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StdEncoding&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DecodeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&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="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;plainText&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cipherText&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
     &lt;span class="n"&gt;cfb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;XORKeyStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plainText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cipherText&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;plainText&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- `Encrypt` method will encrypt the data given in the parameter and will return the encrypted string.
- `Decrypt` method will decrypt the string given in the parameter and will return the decrypted string.
- `secret` is a constant that has random string that will be used to encrypt and decrypt the data. You are free to update the value of secret but keeping a strong secret is advisable.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  4. Handler layer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add the following code in &lt;code&gt;handlers/handler.go&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&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;Handler&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;Init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;Help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;handler&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;service&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileLocker&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;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileLocker&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&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;service&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;service&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&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;Init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"password"&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;password&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;""&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;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"password is required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"file locker initialized"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&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;Lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"file locked"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&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;Unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"password"&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;password&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;""&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;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"password is required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"file unlocked"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  

  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&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;Help&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;`File Locker CLI Tool  

  Usage:  
   file-locker [command]  
  Available Commands:  
   init      Create a directory named private and initialize the file locker lock      Hide the private directory unlock    Unhides the private directory`&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- `handler` is the struct that implements `Handler` interface.
- `Init` is the handler for `init` command. It takes `password` as the flag. The value for this flag will be used in unlocking.
- `Lock` method is the handler for `lock` command.
- `Unlock` method is the handler for `unlock` command. It takes `password` flag for password to unlock.
- `Help` method is the handler for `help` command. It returns the help string for the app.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  5. Main
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add the following code in &lt;code&gt;main.go&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;  &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileLocker&lt;/span&gt;  

    &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gofr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewCMD&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  

     &lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;cryptPkg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error occurred: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GOOS&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
     &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;constants&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Darwin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;constants&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Linux&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;unix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;crypt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unsupported architecture: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GOOS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  

     &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;handlers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

     &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"init"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"unlock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"lock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SubCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"help"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Help&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

     &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Run&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- `NewCMD()` function creates a command line application
- Depending upon the architecture of the underlying OS, we are creating service. This service will be injected in the handler. Currently, we have only implemented service layer for Unix based OS hence in the switch statement we only have case for `Darwin` (for MacOS) and `Linux`.
- `SubCommand` method adds a sub-command to the CLI application. Here, we have added four sub-commands, viz. `init`, `unlock`, `lock`, `help`. The second argument in the `SubCommand` method is the handler that will be called for execution when that particular sub-command is given.
- `Run()` method will run the application.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add constants in &lt;code&gt;constants/constants.go&lt;/code&gt; and our application is ready for use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
   &lt;span class="n"&gt;Darwin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;`darwin`&lt;/span&gt;  
   &lt;span class="n"&gt;Linux&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;`linux`&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Compiling and Having Fun!
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Compile the program using &lt;code&gt;go build -o file-locker .&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the program with the desired action (sub-command):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./file-locker &amp;lt;sub-command&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;flags]
&lt;/code&gt;&lt;/pre&gt;


&lt;ul&gt;
&lt;li&gt;To create a private directory use:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;  ./file-locker init &lt;span class="nt"&gt;-password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Replace &lt;em&gt;&lt;/em&gt; with your desired password. This will create a directory named &lt;code&gt;private&lt;/code&gt; in the current directory. You can put all your files that you require to lock in this directory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To lock (hide) the private directory, use:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;  ./file-locker lock
&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;To unlock (unhide) the private directory, use:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;  ./file-locker unlock &lt;span class="nt"&gt;-password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Congratulations! You've built a basic file locker command-line tool in &lt;strong&gt;Go&lt;/strong&gt;. It may not be production-ready, but it demonstrates some key concepts of file operations and CLI development using &lt;strong&gt;GoFr&lt;/strong&gt;. While we focused on a playful implementation here, this knowledge can be applied to create more robust tools in the future.&lt;/p&gt;

&lt;p&gt;Want to see the complete code and experiment further? The entire codebase for this playful file locker is available at &lt;a href="https://github.com/ssshekhu53/file-locker"&gt;file-locker&lt;/a&gt;. Feel free to clone it, explore the implementation, and make it your own!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Currently, this implementation is designed for Unix-based systems. If you're adventurous, try extending it to support Windows by adding platform-specific logic for hiding and unhiding directories.&lt;/p&gt;

</description>
      <category>go</category>
      <category>gofr</category>
    </item>
    <item>
      <title>Go Generics: Unleashing Reusability and Type Safety</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Wed, 03 Apr 2024 13:30:34 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/go-generics-unleashing-reusability-and-type-safety-533l</link>
      <guid>https://dev.to/ssshekhu53/go-generics-unleashing-reusability-and-type-safety-533l</guid>
      <description>&lt;p&gt;Go 1.18 introduced generics, a game-changer for Go developers. Generics empower you to write code that transcends specific data types, promoting reusability and type safety – hallmarks of clean and maintainable code. So now, we don't have to write the same code over and over again.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Generics?
&lt;/h2&gt;

&lt;p&gt;Generics allow you to write flexible and reusable functions and data structures. Say goodbye to repetitive code and hello to generic solutions that adapt to various types. &lt;/p&gt;

&lt;p&gt;Generics introduce the concept of type parameters, acting as placeholders for various data types within your functions and data structures. This eliminates the need to create multiple copies of the same logic for different types, leading to several benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Write code once and apply it to various data types, reducing code duplication and maintenance overhead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type Safety:&lt;/strong&gt; Generics enforce type checks at compile time, preventing runtime errors caused by incorrect type usage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readability:&lt;/strong&gt; Code becomes more expressive by capturing generic behaviour in a single definition, improving code clarity.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Generics in Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;T&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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;y&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;x&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;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Leverage Min with multiple types&lt;/span&gt;
&lt;span class="n"&gt;minString&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"banana"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;minInt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Min function is generic with a type parameter T. It can determine the type of x and y from the function call using type inference. This allows Min to operate on both strings and integers, finding the minimum value in each case.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use Generics?
&lt;/h2&gt;

&lt;p&gt;There are some key things to remember when using generics in Go. First, ensure you're using &lt;strong&gt;Go version 1.18 or later&lt;/strong&gt; to take advantage of this feature. Generics extend beyond &lt;strong&gt;functions&lt;/strong&gt;; you can also create generic &lt;strong&gt;structs&lt;/strong&gt;, providing a flexible way to structure your data. It's important to note that generics cannot be used with unnamed types within methods. When defining constraints for your generics, you have two options: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Using type sets:&lt;/strong&gt; Type sets allow you to restrict the generic to a specific set of types&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using interface:&lt;/strong&gt; Interfaces offer a more flexible approach by requiring the type to implement a particular set of behaviors. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these concepts will help us leverage generics effectively in your Go projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Type Sets
&lt;/h2&gt;

&lt;p&gt;Go generics offer a powerful way to define functions and data structures that work with various types. However, you might want to restrict a generic to a specific set of compatible types. This is where type sets come into play.&lt;/p&gt;

&lt;p&gt;Type sets allow you to define a collection of types that can be used with a generic type parameter. This adds an extra layer of control and type safety to your 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;type&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&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;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Data&lt;/span&gt; &lt;span class="n"&gt;T&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;intObj&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Integer instance:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;stringObj&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;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;"String instance:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stringObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code demonstrates a generic struct named MyStruct in Go. Generics allow us to define a structure that can hold different data types. In this example, MyStruct uses a type parameter T which can be either int or string (restricted by the int|string syntax).&lt;/p&gt;

&lt;p&gt;The struct itself has a single field named Data with the type T. This allows us to create instances of MyStruct that hold either integer or string data.&lt;/p&gt;

&lt;p&gt;The main function showcases this concept:&lt;/p&gt;

&lt;p&gt;We create an instance of MyStruct with int as the type for &lt;strong&gt;T&lt;/strong&gt; &lt;strong&gt;(MyStruct[int])&lt;/strong&gt;. We assign the value &lt;strong&gt;10&lt;/strong&gt; to the Data field.&lt;/p&gt;

&lt;p&gt;Another instance is created with string as the type for &lt;strong&gt;T (MyStruct[string])&lt;/strong&gt;. This time, the Data field holds the string &lt;strong&gt;"Hello"&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Interface
&lt;/h2&gt;

&lt;p&gt;While type sets offer a way to restrict generics to a specific set of types, Go also allows you to use interfaces as constraints. This provides a more flexible approach when defining the required behaviuor for your generic 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;type&lt;/span&gt; &lt;span class="n"&gt;Stringer&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;PrintString&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;Stringer&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="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;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Go Developer"&lt;/span&gt;
    &lt;span class="n"&gt;PrintString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Works with strings&lt;/span&gt;

    &lt;span class="c"&gt;// Wouldn't work without implementing Stringer&lt;/span&gt;
    &lt;span class="c"&gt;// number := 10 &lt;/span&gt;
    &lt;span class="c"&gt;// PrintString(number)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define an interface &lt;strong&gt;Stringer&lt;/strong&gt; that requires a single method &lt;strong&gt;string()&lt;/strong&gt; string. This interface acts as the constraint for the generic type parameter T in the &lt;strong&gt;PrintString&lt;/strong&gt; function. The function simply prints the output of the &lt;strong&gt;string()&lt;/strong&gt; method on the provided value.&lt;/p&gt;

&lt;p&gt;Here's the key point: &lt;strong&gt;PrintString&lt;/strong&gt; can work with any type that implements the &lt;strong&gt;Stringer&lt;/strong&gt; interface. This could be strings, custom data structures, or even other generic types that implement Stringer. This flexibility allows for wider applicability of the generic function while still enforcing type safety through the interface constraint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type Inference
&lt;/h2&gt;

&lt;p&gt;Type inference feature allows the compiler to automatically deduce the types of arguments you pass to generic functions in many scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type Inference in Action
&lt;/h3&gt;

&lt;p&gt;Imagine a generic function Min that finds the minimum value of two arguments:&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;func&lt;/span&gt; &lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;T&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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;y&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;x&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;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the type parameter T can be any type. When you call Min, type inference kicks in:&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;minString&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"banana"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c"&gt;// T is inferred as string&lt;/span&gt;
&lt;span class="n"&gt;minInt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="c"&gt;// T is inferred as int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In these cases, the compiler can automatically determine the type T based on the values you provide. This eliminates the need to explicitly specify types, making your code cleaner and more concise.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Inference Needs a Nudge
&lt;/h3&gt;

&lt;p&gt;While type inference is convenient, there are situations where the compiler might need a little help. This is where the tilde &lt;strong&gt;(~)&lt;/strong&gt; operator comes into play.&lt;/p&gt;

&lt;p&gt;Consider a generic function that operates on numeric types:&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;func&lt;/span&gt; &lt;span class="n"&gt;Abs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;constraints&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;T&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;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// Might cause data loss&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the constraint constraints.Number ensures T is a numeric type. However, the math.Abs function expects a float64. This could lead to data loss if T is an integer type.&lt;/p&gt;

&lt;p&gt;To address this, we can use the tilde operator:&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;func&lt;/span&gt; &lt;span class="n"&gt;Abs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;constraints&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="err"&gt;~&lt;/span&gt;&lt;span class="kt"&gt;float64&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;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tilde operator tells the compiler to infer the underlying type of x as well. In this case, it ensures x is converted to &lt;strong&gt;float64&lt;/strong&gt; before calling math.Abs, preventing potential data loss.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pros &amp;amp; Cons
&lt;/h2&gt;

&lt;p&gt;While generics offer undeniable advantages, it's important to acknowledge the potential drawbacks and how to navigate them effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Reusability:&lt;/strong&gt; Generics empower you to write code that works with various types, eliminating the need for repetitive implementations for each specific type. This reduces code duplication and improves maintainability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type Safety:&lt;/strong&gt; Generics enforce type checks at compile time. This helps prevent runtime errors caused by incorrect type usage, leading to more robust and reliable applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability:&lt;/strong&gt; By capturing generic behavior in a single definition, code becomes more expressive and easier to understand. This improves code clarity and reduces the mental overhead for developers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disadvantages:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Learning Curve:&lt;/strong&gt; Generics introduce new concepts and syntax that require developers to adapt their understanding of Go. There's a learning curve involved in effectively utilizing generics in your projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Potential for Complexity:&lt;/strong&gt; While generics promote code reuse, over-reliance on complex generic abstractions can sometimes make code less readable and harder to maintain. Strive for a balance between generality and clarity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Backward Compatibility:&lt;/strong&gt; Existing code bases might require adjustments to accommodate generics. Careful consideration during migration is crucial to ensure compatibility and avoid breaking changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Finding the Right Balance
&lt;/h2&gt;

&lt;p&gt;Generics are a powerful tool in your Go development arsenal. By understanding their advantages and potential drawbacks, you can leverage them effectively to write cleaner, more maintainable, and type-safe code. Here are some tips:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start Simple:&lt;/strong&gt; Begin by introducing generics gradually in your projects, focusing on areas where they provide clear benefits in terms of code reuse and type safety.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prioritize Readability:&lt;/strong&gt; Don't sacrifice code clarity for excessive generality. Strive to write generic code that is easy to understand and maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Test Thoroughly:&lt;/strong&gt; As with any code change, ensure your generic implementations are well-tested to catch potential issues before they impact production.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By embracing generics thoughtfully, you can unlock their full potential to enhance the quality and efficiency of your Go projects.&lt;/p&gt;

</description>
      <category>go</category>
      <category>generics</category>
    </item>
    <item>
      <title>Intersection of Two Arrays II || Leetcode Problem Solution</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Sat, 21 Jan 2023 16:20:15 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/intersection-of-two-arrays-ii-leetcode-problem-solution-1k3a</link>
      <guid>https://dev.to/ssshekhu53/intersection-of-two-arrays-ii-leetcode-problem-solution-1k3a</guid>
      <description>&lt;h3&gt;
  
  
  Problem Statement
&lt;/h3&gt;

&lt;p&gt;Given two integer arrays &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sample Input
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;nums1&lt;/code&gt; = [1,2,2,1], &lt;code&gt;nums2&lt;/code&gt; = [2,2]&lt;/p&gt;

&lt;h4&gt;
  
  
  Sample Output
&lt;/h4&gt;

&lt;p&gt;[2,2]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Link to question:&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://leetcode.com/problems/intersection-of-two-arrays-ii/description/" rel="noopener noreferrer"&gt;https://leetcode.com/problems/intersection-of-two-arrays-ii/description/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    vector&amp;lt;int&amp;gt; intersect(vector&amp;lt;int&amp;gt;&amp;amp; nums1, vector&amp;lt;int&amp;gt;&amp;amp; nums2) {
        vector&amp;lt;int&amp;gt; result;
        int i, j, n1, n2;

        n1=nums1.size();
        n2=nums2.size();
        i=0;
        j=0;

        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());

        while(i&amp;lt;n1 &amp;amp;&amp;amp; j&amp;lt;n2) {
            if(nums1[i]==nums2[j]) {
                result.push_back(nums1[i]);
                i++;
                j++;
            }
            else if(nums1[i]&amp;gt;nums2[j])
                j++;
            else
                i++;
        }

        return result;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;It is given in the problem statement that we can give the result in any order. And this had made our task easy. Now we can simply sort both the arrays and traverse through them to get our desired result. &lt;/p&gt;

&lt;p&gt;We can use two different variables (&lt;strong&gt;i&lt;/strong&gt; and &lt;strong&gt;j&lt;/strong&gt; in this case) to traverse both the arrays. Using the condition &lt;code&gt;i&amp;lt;n1 &amp;amp;&amp;amp; j&amp;lt;n2&lt;/code&gt; in &lt;strong&gt;while&lt;/strong&gt; loop, eliminates the need of an extra statement to find the array of the shorter length. Now, among &lt;strong&gt;i&lt;/strong&gt; and &lt;strong&gt;j&lt;/strong&gt; reaches the end of the array, will make the condition to evaluate to &lt;code&gt;false&lt;/code&gt; and the loop will terminate.&lt;/p&gt;

&lt;p&gt;Inside the loop, we can check if the element at &lt;code&gt;ith&lt;/code&gt; position and &lt;code&gt;jth&lt;/code&gt; position of the &lt;code&gt;num1&lt;/code&gt; and &lt;code&gt;num2&lt;/code&gt; respectively are equal or not. If they are equal, then we get the element for our resultant array. If they are not equal, then there can be two cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;ith&lt;/code&gt; element of &lt;code&gt;num1&lt;/code&gt; is lesser than the &lt;code&gt;jth&lt;/code&gt; element of &lt;code&gt;num2&lt;/code&gt;&lt;/strong&gt; : In this case, we will increment the value of &lt;strong&gt;i&lt;/strong&gt; by &lt;strong&gt;1&lt;/strong&gt; till and go for next iteration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;ith&lt;/code&gt; element of &lt;code&gt;num1&lt;/code&gt; is greater than the &lt;code&gt;jth&lt;/code&gt; element of &lt;code&gt;num2&lt;/code&gt;&lt;/strong&gt; : In this case, we will increment the value of &lt;strong&gt;j&lt;/strong&gt; by &lt;strong&gt;1&lt;/strong&gt; till and go for next iteration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both the cases, we are trying to reach to such position where both the elements are equal so that we can get our next element for the resultant array. &lt;/p&gt;

&lt;p&gt;After some iterations, the shorter array will be full traversed. And if any of the array is fully traversed then we can't get more intersecting elements.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Add Two Numbers II | Leetcode Problem Solution With Explanation</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Fri, 15 Jul 2022 17:06:24 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/add-two-numbers-ii-leetcode-problem-solution-with-explanation-3e8n</link>
      <guid>https://dev.to/ssshekhu53/add-two-numbers-ii-leetcode-problem-solution-with-explanation-3e8n</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.&lt;/p&gt;

&lt;p&gt;You may assume the two numbers do not contain any leading zero, except the number 0 itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Input&lt;/strong&gt;&lt;br&gt;
l1 = [7,2,4,3], l2 = [5,6,4]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Output&lt;/strong&gt;&lt;br&gt;
[7,8,0,7]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Link to question:&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://leetcode.com/problems/add-two-numbers-ii/"&gt;https://leetcode.com/problems/add-two-numbers-ii/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution (Java)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack&amp;lt;int&amp;gt; n1, n2, sum;
        ListNode *res=new ListNode();
        ListNode *cur=res;
        ListNode *prev;
        int carry=0;
        while(l1!=nullptr) {
            n1.push(l1-&amp;gt;val);
            l1=l1-&amp;gt;next;
        }
        while(l2!=nullptr) {
            n2.push(l2-&amp;gt;val);
            l2=l2-&amp;gt;next;
        }
        while(!(n1.empty() &amp;amp;&amp;amp; n2.empty())) {
            if(n1.empty()) {
                sum.push((n2.top()+carry)%10);
                carry=(n2.top()+carry)/10;
                n2.pop();
            } 
            else if(n2.empty()) {
                sum.push((n1.top()+carry)%10);
                carry=(n1.top()+carry)/10;
                n1.pop();
            } 
            else {
                sum.push((n2.top()+n1.top()+carry)%10);
                carry=(n1.top()+n2.top()+carry)/10;
                n1.pop();
                n2.pop();
            } 
        }
        if(carry!=0)
            sum.push(carry);
        while(!sum.empty()) {
            cur-&amp;gt;val=sum.top();
            cur-&amp;gt;next=new ListNode();
            prev=cur;
            cur=cur-&amp;gt;next;
            sum.pop();
        }
        prev-&amp;gt;next=nullptr;
        return res;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;Firstly, push the numbers from both the linked lists into two separate stacks. This will store the linked list in reverse order and will also help in doing the addition operation. Now pop the elements from the stacks and add them and push the modulo 10 of the sum in the third stack and store the quotient 10 of sum in the carry (simple mathematical addition, just as we used to do in kindergarten). Lastly popped the elements from the third stack and inserted into the third linked list and that will the resultant linked list.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>competitiveprogram</category>
      <category>algorithms</category>
      <category>java</category>
    </item>
    <item>
      <title>Rings and Rods | Leetcode Problem Solution Using Bit Manipulation</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Fri, 03 Jun 2022 07:19:58 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/rings-and-rods-leetcode-problem-solution-using-bit-manipulation-10p3</link>
      <guid>https://dev.to/ssshekhu53/rings-and-rods-leetcode-problem-solution-using-bit-manipulation-10p3</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.&lt;/p&gt;

&lt;p&gt;You are given a string &lt;strong&gt;rings&lt;/strong&gt; of length &lt;strong&gt;2n&lt;/strong&gt; that describes the n rings that are placed onto the rods. Every two characters in rings forms a &lt;strong&gt;color-position pair&lt;/strong&gt; that is used to describe each ring where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;first character&lt;/strong&gt; of the ith pair denotes the ith ring's color ('R', 'G', 'B').&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;second character&lt;/strong&gt; of the ith pair denotes the rod that the ith ring is placed on ('0' to '9').&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;number of rods&lt;/strong&gt; that have all three colors of rings on them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Input&lt;/strong&gt;&lt;br&gt;
B0B6G0R6R0R6G9&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Output&lt;/strong&gt;&lt;br&gt;
1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The rod labeled 0 holds 3 rings with all colors: red, green, and blue.&lt;/li&gt;
&lt;li&gt;The rod labeled 6 holds 3 rings, but it only has red and blue.&lt;/li&gt;
&lt;li&gt;The rod labeled 9 holds only a green ring.
Thus, the number of rods with all three colors is 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Link to question:&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://leetcode.com/problems/rings-and-rods/"&gt;https://leetcode.com/problems/rings-and-rods/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution (C++)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int countPoints(String rings) {
        int count, n=rings.length();
        int rods[]=new int[10];
        Arrays.fill(rods, 0);
        for(int i=0; i&amp;lt;n; i+=2) {
            char color=rings.charAt(i);
            int rod=rings.charAt(i+1)-48;
            switch(color) {
                case 'R':
                    rods[rod]=rods[rod] | 1;
                    break;
                case 'G':
                    rods[rod]=rods[rod] | 2;
                    break;
                case 'B':
                    rods[rod]=rods[rod] | 4;
                    break;
            }
        }
        count=0;
        for(int item: rods)
            count+=item==7?1:0;
        return count;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>competitiveprogramming</category>
      <category>algorithms</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Strange Counter | Hackerrank Problem Solution With Explanation</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Sun, 29 May 2022 12:04:35 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/strange-counter-hackerrank-problem-solution-with-explanation-2mg0</link>
      <guid>https://dev.to/ssshekhu53/strange-counter-hackerrank-problem-solution-with-explanation-2mg0</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;There is a strange counter. At the first second, it displays the number . Each second, the number displayed by decrements by 1 until it reaches 1. In next second, the timer resets to &lt;strong&gt;2 x the initial number of the prior cycle&lt;/strong&gt; and continues counting down. The diagram below shows the counter values for each time &lt;em&gt;&lt;strong&gt;t&lt;/strong&gt;&lt;/em&gt; in the first three cycles:&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%2Fr0t7j7fdhtccqxrv66h1.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%2Fr0t7j7fdhtccqxrv66h1.png" alt="Strange Counter Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find and print the value displayed by the counter at time &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input Format&lt;/strong&gt;&lt;br&gt;
A single integer, the value of &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Input&lt;/strong&gt;&lt;br&gt;
4&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Output&lt;/strong&gt;&lt;br&gt;
6&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Link to question:&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.hackerrank.com/challenges/strange-code/problem" rel="noopener noreferrer"&gt;https://www.hackerrank.com/challenges/strange-code/problem&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution (Python3)
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;def strangeCounter(t):&lt;br&gt;
    # Write your code here&lt;br&gt;
    n = math.log(((t + 3) / 3), 2)&lt;br&gt;
    if math.floor(n) == math.ceil(n):&lt;br&gt;
        return 1&lt;br&gt;
    n = int(math.ceil(n))&lt;br&gt;
    term = 3 * (math.pow(2, n) - 1)&lt;br&gt;
    return int(term - t + 1)&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Explanation&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;From the given problem statement, we can clearly see that:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time taken to complete 1st cycle: &lt;em&gt;3s&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Time taken to complete 2nd cycle: &lt;em&gt;6s&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Time taken to complete 3rd cycle: &lt;em&gt;12s&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And so on.&lt;/p&gt;

&lt;p&gt;If we observe a little bit then we will find that time taken by cycles is forming a series and that series is in &lt;strong&gt;G.P. (Geometric Progression)&lt;/strong&gt; with &lt;strong&gt;common ratio&lt;/strong&gt; as &lt;strong&gt;2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is clear that the time at which is any cycle ends, is the cumulative sum of all the time taken to complete the cycle till that cycle. Viz.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time at which 1st cycle ends: &lt;em&gt;3&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Time at which  cycle ends: Time taken to complete 1st cycle + Time taken to complete 2nd cycle = &lt;em&gt;3&lt;/em&gt; + &lt;em&gt;6&lt;/em&gt; = &lt;em&gt;9&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And so on.&lt;/p&gt;

&lt;p&gt;If this series is in &lt;strong&gt;G.P.&lt;/strong&gt; then the cumulative sum, i.e. the end time for any cycle, can be calculated using the below formula:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;S = a(rⁿ-1)/(r-1)&lt;/em&gt;&lt;/strong&gt;,&lt;/p&gt;

&lt;p&gt;where &lt;strong&gt;&lt;em&gt;S&lt;/em&gt;&lt;/strong&gt; is the sum of the series, &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; is the first term of the series, &lt;strong&gt;&lt;em&gt;r&lt;/em&gt;&lt;/strong&gt; is the common ratio and &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt; is the number of terms in the series.&lt;/p&gt;

&lt;p&gt;Here, in the problem, we are given the sum of the series (i.e. &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt; in this case), we know the first term and the common ratio (i.e. &lt;strong&gt;&lt;em&gt;3&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;2&lt;/em&gt;&lt;/strong&gt;). So, we have to calculate &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt;. Using the above formula, we can deduce:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;n=log₂((S+3)/3)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we get a whole number as &lt;strong&gt;&lt;em&gt;n&lt;/em&gt;&lt;/strong&gt; then given &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt; is the end time of the cycle. But if we get a floating number, then &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt; is the time between the end times of previous cycle and next cycle. In this case, using the ceil value of &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt; and the formula of the &lt;strong&gt;Sum of G.P.&lt;/strong&gt; we can calculate the end time for the current cycle, say &lt;strong&gt;&lt;em&gt;terms&lt;/em&gt;&lt;/strong&gt;. And the difference between the &lt;strong&gt;&lt;em&gt;terms&lt;/em&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;em&gt;t&lt;/em&gt;&lt;/strong&gt; is our answer.&lt;/p&gt;

</description>
      <category>competitiveprogramming</category>
      <category>algorithms</category>
      <category>hackerrank</category>
      <category>python</category>
    </item>
    <item>
      <title>The Captain's Room | Hackerrank Problem Solution With Explanation</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Sat, 30 Apr 2022 18:43:50 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/the-captains-room-hackerrank-problem-solution-with-explanation-3l7a</link>
      <guid>https://dev.to/ssshekhu53/the-captains-room-hackerrank-problem-solution-with-explanation-3l7a</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Mr. Anant Asankhya is the manager at the INFINITE hotel. The hotel has an infinite amount of rooms.&lt;/p&gt;

&lt;p&gt;One fine day, a finite number of tourists come to stay at the hotel.&lt;br&gt;
The tourists consist of:&lt;br&gt;
→ A Captain.&lt;br&gt;
→ An unknown group of families consisting of  members per group where &lt;strong&gt;K ≠ 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Captain was given a separate room, and the rest were given one room per group.&lt;/p&gt;

&lt;p&gt;Mr. Anant has an unordered list of randomly arranged room entries. The list consists of the room numbers for all of the tourists. The room numbers will appear &lt;strong&gt;K&lt;/strong&gt; times per group except for the Captain's room.&lt;/p&gt;

&lt;p&gt;Mr. Anant needs you to help him find the Captain's room number.&lt;br&gt;
The total number of tourists or the total number of groups of families is not known to you.&lt;br&gt;
You only know the value of &lt;strong&gt;K&lt;/strong&gt; and the room number list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input Format&lt;/strong&gt;&lt;br&gt;
The first line consists of an integer, &lt;strong&gt;K&lt;/strong&gt;, the size of each group.&lt;br&gt;
The second line contains the unordered elements of the room number list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output Format&lt;/strong&gt;&lt;br&gt;
Output the Captain's room number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Input&lt;/strong&gt;&lt;br&gt;
5&lt;br&gt;
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Output&lt;/strong&gt;&lt;br&gt;
8&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Link to question:&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.hackerrank.com/challenges/py-the-captains-room/problem"&gt;https://www.hackerrank.com/challenges/py-the-captains-room/problem&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution (Python3)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if __name__ == '__main__':
    k = int(input())
    rooms = list(map(int, input().split()))
    roomSet = set(rooms)
    roomSum = sum(rooms)
    roomSetSum = sum(roomSet) * k
    captiansRoom = (roomSetSum - roomSum) // (k - 1)
    print(captiansRoom)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;The logic behind this is simple mathematics. If all the unique elements were present in the array &lt;strong&gt;K&lt;/strong&gt; times then their sum would have been &lt;strong&gt;sum of unique elements mutliplied K times&lt;/strong&gt;. But there is one unique element that is present only one time. So the &lt;strong&gt;roomSetSum&lt;/strong&gt; variable's value will be  more than the actual sum of elements of the given array, let's say it &lt;strong&gt;x&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If we see it from a different angle then we will find that our desired element, let's say it &lt;strong&gt;z&lt;/strong&gt; is present only one time but is missing &lt;strong&gt;k-1&lt;/strong&gt; times from the array. So we can conclude that &lt;strong&gt;x&lt;/strong&gt; is nothing but &lt;strong&gt;z&lt;/strong&gt; multiplied by &lt;strong&gt;k-1&lt;/strong&gt;. Now all we've to do is to do reverse engineering and just find the &lt;strong&gt;x&lt;/strong&gt; &lt;em&gt;(that is the difference between the sum of all the elements of rooms array if it contained all the unique elements exactly &lt;strong&gt;k&lt;/strong&gt; times and actual sum of all the elements of the rooms array, i.e. the array that contains all the elements &lt;strong&gt;k&lt;/strong&gt; times but a single element only one time)&lt;/em&gt; and divide it with &lt;strong&gt;k-1&lt;/strong&gt; and voila! We got the answer.&lt;/p&gt;

</description>
      <category>python</category>
      <category>hackerrank</category>
      <category>competitveprogramming</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Reverse Linked List | Leetcode Problem</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Wed, 12 Jan 2022 17:06:54 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/reverse-linked-list-5a6l</link>
      <guid>https://dev.to/ssshekhu53/reverse-linked-list-5a6l</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given the head of a singly linked list, reverse the list, and return the reversed list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8q215zhavqlri28fae3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8q215zhavqlri28fae3.jpg" alt="Image description" width="542" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;**Input:** head = [1,2,3,4,5]&lt;br&gt;
**Output:** [5,4,3,2,1]&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Link to the question:&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://leetcode.com/problems/reverse-linked-list/"&gt;https://leetcode.com/problems/reverse-linked-list/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    ListNode* reverseList(ListNode *cur, ListNode *next) {
        ListNode *last;
        if(next-&amp;gt;next!=nullptr)
            last=reverseList(next, next-&amp;gt;next);
        else
            last=next;
        next-&amp;gt;next=cur;
        return last;
    }

    ListNode* reverseList(ListNode* head) {
        ListNode *last;
        if(head==nullptr || head-&amp;gt;next==nullptr)
            return head;
        if(head-&amp;gt;next!=nullptr)
            last=reverseList(head, head-&amp;gt;next);
        head-&amp;gt;next=nullptr;
        return last;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Problem while compiling and executing Java program with JAR file</title>
      <dc:creator>Shashank Shekhar</dc:creator>
      <pubDate>Fri, 23 Jul 2021 14:54:10 +0000</pubDate>
      <link>https://dev.to/ssshekhu53/problem-while-compiling-and-executing-java-program-with-jar-file-13bb</link>
      <guid>https://dev.to/ssshekhu53/problem-while-compiling-and-executing-java-program-with-jar-file-13bb</guid>
      <description>&lt;p&gt;One fine day, I was creating a mini project in Java. The concept of the project was to demonstrate the CRUD operation using Swing, i.e. using GUI. As it was to demonstrate CRUD, I used JDBC in the program as well. If you are familiar with JDBC then you must be knowing that it requires a JDBC driver’s JAR file to successfully compile and execute a JDBC program.&lt;/p&gt;

&lt;p&gt;I followed all the steps, wrote a clean code and added the JAR file in the root directory of the project. I compiled the program using the following command:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;javac -cp “.;relative/path/to/jar/file” filename.java&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The compilation was successful. Then I tried to execute the program with the following command:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;java -cp “.;relative/path/to/jar/file” classname&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But instead of successfully executing, it displayed the following error:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error: Could not find or load main class classname&lt;br&gt;
Caused by: java.lang.ClassNotFoundException: classname&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I googled a lot about the right procedure to compile and execute a JDBC program but either the result contained the same procedure or contained the steps to program on IDE like NetBeans.&lt;/p&gt;

&lt;p&gt;After doing a lot of research I went to Stackoverflow, and from the answers there I concluded that I had done a slight mistake in the commands. I was specifying the relative path to the JAR file whereas I was supposed to provide a fully qualified path of the JAR file, i.e. the absolute path for the file. Also, it didn’t require to specify classpath while compiling as the JAR file was in the same directory.&lt;/p&gt;

&lt;p&gt;So the new compiling and executing command goes like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;javac filename.java&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;java -cp “.:fully/qualified/path/to/jar/file” classname&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another thing to notice here is that I changed the semicolon(;) with a colon(:) in the classpath to separate two different classpaths. Because Linux based OS uses the colon as a separator but Windows uses a semicolon as a separator as the colon is used for drive specification in the Windows system.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
  </channel>
</rss>
