<?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: Sourjaya Das</title>
    <description>The latest articles on DEV Community by Sourjaya Das (@sourjaya).</description>
    <link>https://dev.to/sourjaya</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%2F1012465%2F1ec31fb4-3b17-4c31-9cae-d4fd485909e6.png</url>
      <title>DEV Community: Sourjaya Das</title>
      <link>https://dev.to/sourjaya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sourjaya"/>
    <language>en</language>
    <item>
      <title>Build a CLI tool : Generating hex dumps with Golang</title>
      <dc:creator>Sourjaya Das</dc:creator>
      <pubDate>Tue, 09 Apr 2024 15:18:38 +0000</pubDate>
      <link>https://dev.to/sourjaya/build-a-cli-tool-generating-hex-dumps-with-golang-4pa</link>
      <guid>https://dev.to/sourjaya/build-a-cli-tool-generating-hex-dumps-with-golang-4pa</guid>
      <description>&lt;p&gt;In this article, we will look at how to build a CLI tool using Go standard packages.We will be trying to build our own version of &lt;code&gt;xxd&lt;/code&gt;. This project challenge was originally posted &lt;a href="https://codingchallenges.fyi/challenges/challenge-xxd"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, what exactly is &lt;code&gt;xxd&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
Xxd is a CLI tool for linux that creates a hex dump of a given file or standard input.It can also convert a hex dump back to its original binary form.&lt;/p&gt;

&lt;p&gt;To demonstrate what &lt;code&gt;xxd&lt;/code&gt; does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create or take any existing file from your system. And run the following command:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# xxd &amp;lt;filename&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;# xxd &amp;lt;filepath&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;# To dump the hex dump to another file&lt;/span&gt;
&lt;span class="c"&gt;# xxd &amp;lt;filename/filepath&amp;gt; &amp;gt; &amp;lt;filename/filepath&amp;gt; &lt;/span&gt;
xxd file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Suppose the file contents were:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hey! How you doing?
I hope you are doing fine.
Believe in yourself.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;When we run the command mentioned above, we get the following output:&lt;/li&gt;
&lt;/ul&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%2F6zzvst6x4s0b9t2i5a2u.png" 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%2F6zzvst6x4s0b9t2i5a2u.png" alt="Sample1" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To understand what was printed out lets look at the first line of the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000: 4865 7921 2048 6f77 2079 6f75 2064 6f69  Hey! How you doi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The first part&lt;/strong&gt; : &lt;code&gt;00000000&lt;/code&gt; is the file offset, that is the position from which this line prints the bytes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The second part&lt;/strong&gt; : &lt;code&gt;4865 7921 2048 6f77 2079 6f75 2064 6f69&lt;/code&gt; is the actual bytes of data in its hexadecimal form with two octets(bytes) grouped together(48 is hex of a byte,65 is hex of another byte in the group,and so on).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The third part&lt;/strong&gt; : &lt;code&gt;Hey! How you doi&lt;/code&gt; is the actual content(text) of those bytes printed in the line.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;📝&lt;br&gt;
&lt;em&gt;Any ASCII character with hexadecimal value less than &lt;code&gt;20&lt;/code&gt; and more than &lt;code&gt;7e&lt;/code&gt; will be converted to '.' while printing out the original text in the third part.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, what is the need of such a tool? There can be many use cases of a hex dump which includes &lt;strong&gt;binary file analysis and comparison&lt;/strong&gt;, &lt;strong&gt;data transformation&lt;/strong&gt;, &lt;strong&gt;digital forensics and security&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At first glance it seems easy to code such a tool. Read the file or read from the standard input, convert each byte to its hexadecimal, and print the list of hexadecimal numbers with the original text using proper arrangement. But the challenge lies in the different edge cases that pops up due to the use of additional &lt;code&gt;options&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;xxd&lt;/code&gt; has its own flags that help to manipulate the output. We will look at the behaviours of this flags, when we discuss about the edge cases.&lt;/p&gt;

&lt;p&gt;Now that we have a brief overview of what we need to build, lets dive deep into the intricacies of this tool.&lt;/p&gt;


&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;
&lt;h4&gt;
  
  
  1. Prerequisites
&lt;/h4&gt;
&lt;h4&gt;
  
  
  2. Understanding the behaviour of the flags
&lt;/h4&gt;
&lt;h4&gt;
  
  
  3. Writing and Understanding the code
&lt;/h4&gt;
&lt;h4&gt;
  
  
  4. What's next
&lt;/h4&gt;


&lt;h2&gt;
  
  
  1. Prerequisites
&lt;/h2&gt;

&lt;p&gt;As we will be using golang for our project,we should make sure :&lt;/p&gt;
&lt;h3&gt;
  
  
  1.1. Golang is installed in our system.
&lt;/h3&gt;

&lt;p&gt;To check wether &lt;code&gt;GO&lt;/code&gt; is installed in our system or not use the command &lt;code&gt;go version&lt;/code&gt; in your terminal. If it is not installed, check the official installation &lt;a href="https://go.dev/doc/install"&gt;steps&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.2. Use a code editor of your choice.
&lt;/h3&gt;

&lt;p&gt;And that's about all that we need to start coding.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. Understanding the behaviour of the flags
&lt;/h2&gt;

&lt;p&gt;Before we start writing our code we have to make sure we have a good understanding of what each flags do and how the output changes with change of values of this flags.&lt;br&gt;
As mentioned in the &lt;a href="https://codingchallenges.fyi/challenges/challenge-xxd"&gt;challenge&lt;/a&gt; we will be focusing on the functionalities of six flags :&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flags&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;-e&lt;/td&gt;
&lt;td&gt;Output in little-endian format.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-g&lt;/td&gt;
&lt;td&gt;Specify the number of bytes per group.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-l&lt;/td&gt;
&lt;td&gt;Specify the no of octets to dump from the input.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-c&lt;/td&gt;
&lt;td&gt;Specify the number of octets to print each line.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-s&lt;/td&gt;
&lt;td&gt;Specify the offset to start printing from.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-r&lt;/td&gt;
&lt;td&gt;Revert from hexadecimal dump to original binary form.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  2.1. &lt;code&gt;-e&lt;/code&gt; flag
&lt;/h3&gt;

&lt;p&gt;when we enter the command with &lt;code&gt;-e&lt;/code&gt; flag:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-e&lt;/span&gt; file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we get a output like:&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%2Fbl9l7lrgsau7jmy62vrp.png" 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%2Fbl9l7lrgsau7jmy62vrp.png" alt="Sample2" width="800" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if we look at each groups, there are 4 octets in reversed ordering. So the default behaviour changes.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.2. &lt;code&gt;-g&lt;/code&gt; flag
&lt;/h3&gt;

&lt;p&gt;when we use the &lt;code&gt;-g&lt;/code&gt; flag:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-g&lt;/span&gt; 5 file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we get:&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%2F4yx0dk6olzaneqpxxj3l.png" 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%2F4yx0dk6olzaneqpxxj3l.png" alt="Sample3" width="800" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the output we see 5 octets grouped together until the columns are filled for each row.&lt;/p&gt;

&lt;p&gt;Then again if we use both &lt;code&gt;-e&lt;/code&gt; and &lt;code&gt;-g&lt;/code&gt; together:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; 5 file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we get:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xxd: number of octets per group must be a power of 2 with -e.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this case, we have to make sure octets per group is given to be a power of 2, to make it work with the -e flag.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.3. &lt;code&gt;-c&lt;/code&gt; flag
&lt;/h3&gt;

&lt;p&gt;When we use &lt;code&gt;-c&lt;/code&gt; flag:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-c&lt;/span&gt; 5 file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the result is:&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%2Ffl2hm1bsnecvjpz9psg4.png" 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%2Ffl2hm1bsnecvjpz9psg4.png" alt="Sample4" width="800" height="819"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that there are atmost 5 octets per row(line).&lt;/p&gt;
&lt;h3&gt;
  
  
  2.4. &lt;code&gt;-l&lt;/code&gt; flag
&lt;/h3&gt;

&lt;p&gt;For the command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-l&lt;/span&gt; 20 f.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the output will be:&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%2Fb6ak76njkrm9ybczzaqs.png" 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%2Fb6ak76njkrm9ybczzaqs.png" alt="Sample5" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The total no of octets displayed are 20.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.5. &lt;code&gt;s&lt;/code&gt; flag.
&lt;/h3&gt;

&lt;p&gt;If we write:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-s&lt;/span&gt; 3 file.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we get:&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%2F89o9v98n5bxt9q3pwohd.png" 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%2F89o9v98n5bxt9q3pwohd.png" alt="Sample6" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The offset will start from the 3rd byte in the input file. But if value of flag &lt;code&gt;-s&lt;/code&gt; is negative, then the offset will be set relative to the end of file. &lt;br&gt;
Another edge case to consider if the value of &lt;code&gt;-s&lt;/code&gt; flag is negative when no file name is given, the seek won't happen.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nt"&gt;-3&lt;/span&gt;
&lt;span class="c"&gt;#Output&lt;/span&gt;
xxd: Sorry, cannot seek.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is also true for inputs like:&lt;br&gt;
&lt;code&gt;xxd -s -0&lt;/code&gt; and &lt;code&gt;xxd -s +-5&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  2.6. &lt;code&gt;-r&lt;/code&gt; flag
&lt;/h3&gt;

&lt;p&gt;This flag is used to revert back from a hex dump to its original binary form. Suppose &lt;code&gt;file.hex&lt;/code&gt; contains the hex dump of &lt;code&gt;file.txt&lt;/code&gt;. If we want to get the text content back we do:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;xxd &lt;span class="nt"&gt;-r&lt;/span&gt; file.hex
&lt;span class="c"&gt;#or&lt;/span&gt;
xxd &lt;span class="nt"&gt;-r&lt;/span&gt; file.hex &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; file2.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The output will be:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hey! How you doing?
I hope you are doing fine.
Believe in yourself.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;📝&lt;br&gt;
&lt;em&gt;We can use decimal, octal or hexadecimal values for the flags.&lt;br&gt;
octal values are represented with a leading 0 like &lt;code&gt;014&lt;/code&gt; and hexadecimal is represented like &lt;code&gt;0x0c&lt;/code&gt; or &lt;code&gt;0x0C&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is important to mention that if we put a non numeric value like &lt;code&gt;abcd&lt;/code&gt; as any flag value, when the file name is not provided, the default flag values will be used. Also if a value like &lt;code&gt;5jkl&lt;/code&gt; is given as a flag value, the value will be read as &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The return values are as follows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;no errors encountered.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-1&lt;/td&gt;
&lt;td&gt;operation not supported.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;error while parsing options.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;problems with input file.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;problems with output file.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4,5&lt;/td&gt;
&lt;td&gt;desired seek position is unreachable.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  3. Writing and Understanding the code
&lt;/h2&gt;

&lt;p&gt;Before starting with the code, its important to have an idea about how we will tackle the problem. At my first attempt at building this tool, I took a naive path, to read the file, some bytes at a time, and stored them in a slice of bytes. Then I printed out each byte in its hex format,one by one. Well this solution worked fine when there were no flags involved, and when the output format did not depend on those flag inputs. But when I started to build the logic for all the edge cases, the code started to become messy and unreadable. &lt;/p&gt;

&lt;p&gt;That's when I had to switch the way I was processing those bytes. Instead of directly converting each individual byte to its hex representation, I converted the whole chunk of bytes to a string of hex values. This change helped in tackling most of the edge cases I talked about earlier.&lt;/p&gt;
&lt;h3&gt;
  
  
  3.1. Folder Structure
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── 📁sdxxd
    └── 📁xxd
        └── xxd.go
    └── main.go
    └── go.mod
    └── go.sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  3.2 Create your GO Project
&lt;/h3&gt;

&lt;p&gt;Before writing your code you need to setup the directory where you will house your project. Then, open the terminal from the directory, and enter the following command to initialize your project.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# go mod init &amp;lt;your_module_path&amp;gt;&lt;/span&gt;
go mod init github.com/Sourjaya/sdxxd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The go mod init command creates a &lt;code&gt;go.mod&lt;/code&gt; file to track your code's dependencies. Using your own github repository will provide a unique module path for the project.&lt;/p&gt;

&lt;p&gt;Now, in &lt;code&gt;main.go&lt;/code&gt; write the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"github.com/Sourjaya/sdxxd/xxd"&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;xxd&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Driver&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;Here we call the &lt;code&gt;Driver&lt;/code&gt; function from &lt;strong&gt;xxd&lt;/strong&gt; package.&lt;/p&gt;
&lt;h3&gt;
  
  
  3.3. Utility functions and the structs in use
&lt;/h3&gt;

&lt;p&gt;In the xxd folder create a new go file &lt;code&gt;xxd.go&lt;/code&gt;:&lt;/p&gt;


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

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Here we declare three structs &lt;code&gt;Flags&lt;/code&gt; , &lt;code&gt;ParsedFlags&lt;/code&gt; and &lt;code&gt;IsSetFlags&lt;/code&gt; . In function &lt;code&gt;NewFlags()&lt;/code&gt; we initialize the flags and check if certain flag values have been provided or not.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📝 &lt;br&gt;
&lt;em&gt;Here to parse the flags from the terminal we are not going to use golang &lt;code&gt;flags&lt;/code&gt; package because this package does not have the support for this input form: &lt;code&gt;xxd -s5 -g3&lt;/code&gt;, where there is no gap between the flag and the flag values. Instead we are using &lt;code&gt;pflags&lt;/code&gt; package.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, lets look at some of the helper functions we are going to need and what is the need of them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;numberParse()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This function will be used to parse the flag values and with the help of regular expression, filter out the numerical value from it.&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="c"&gt;// Function to parse number from a string using regular expression&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;numberParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&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="n"&gt;res&lt;/span&gt; &lt;span class="kt"&gt;int64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&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="c"&gt;// regular expression&lt;/span&gt;
    &lt;span class="n"&gt;re&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;regexp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MustCompile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`-?0[xX][0-9a-fA-F]+|-\b0[0-7]*\b|-\b[1-9][0-9]*\b|0[xX][0-9a-fA-F]+|\b0[0-7]*\b|\b[1-9][0-9]*\b`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// Find the match&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FindString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// if a certain match is found convert into decimal, octal or hexadecimal and return. else return 0.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;s&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="n"&gt;strconv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;64&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="m"&gt;0&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;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;reverseString()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This function is for reversing a hex string input. This function is exclusively used when the output should be in little-endian format.&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="c"&gt;// Function to reverse a string&lt;/span&gt;
&lt;span class="c"&gt;// input: The input hex string to be reversed.&lt;/span&gt;
&lt;span class="c"&gt;// Returns the reversed hex string.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&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="c"&gt;// Decode hex string to byte slice&lt;/span&gt;
    &lt;span class="n"&gt;hexStr&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReplaceAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;bytes&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;hex&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;hexStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// Reverse the byte slice&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&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;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c"&gt;// Encode the reversed byte slice back to hex string&lt;/span&gt;
    &lt;span class="n"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EncodeToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;whitespace&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&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;reversed&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;whitespace&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;reversed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;byteToHex()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before printing the result we will need to convert the slice of bytes to a hex string. This function is for this purpose.&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="c"&gt;// Function to convert a byte slice to a hex string with specified grouping.&lt;/span&gt;
&lt;span class="c"&gt;// byteBuffer: The input byte slice to be converted.&lt;/span&gt;
&lt;span class="c"&gt;// count: The number of bytes per group.&lt;/span&gt;
&lt;span class="c"&gt;// Returns the hex string representation of the byte slice.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;byteToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byteBuffer&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;count&lt;/span&gt; &lt;span class="kt"&gt;int&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="c"&gt;// encode byte slice to string&lt;/span&gt;
    &lt;span class="n"&gt;encodedString&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EncodeToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byteBuffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// add extra whitespaces&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;-&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;byteBuffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;encodedString&lt;/span&gt; &lt;span class="o"&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;Sprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encodedString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;encodedString&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;byteToSting()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To display the third section of the result, we need to convert the byte slice to its text form. This function will do exactly that.&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="c"&gt;// input: The input byte slice to be converted.&lt;/span&gt;
&lt;span class="c"&gt;// Returns the string representation of the byte slice.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;bytesToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&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;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;output&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;input&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="c"&gt;// convert ASCII byte slice to its equivalent character string&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;input&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;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0x20&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0x7e&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'.'&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&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;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;size()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The size of the chunk of bytes to read is dependent on the columns value. We can use any stop value, but I used an arbitrary value of 2048. Its essential to read the bytes in chunks because reading large files will be comparatively faster this way, than to read it as a whole.&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="c"&gt;// calculate size of chunk to read for each iteration&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cols&lt;/span&gt; &lt;span class="kt"&gt;int&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;div&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;SIZE&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;SIZE&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;0&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="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;cols&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;div&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;trimBytes()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This function will be needed when the reverse conversion takes place, that is from a hex dump to the original content.&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="c"&gt;// Helper function to trim the spaces from a line&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;trimBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;strings&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;words&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  3.4. Structuring the code
&lt;/h3&gt;

&lt;p&gt;After we have written the helper functions its time to put them to use. We will start with the &lt;code&gt;Driver()&lt;/code&gt; function.&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="c"&gt;// Driver function to use the functionalities of this package&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;Driver&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;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setFlags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;NewFlags&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c"&gt;// if no file name is provided read from standard input&lt;/span&gt;
    &lt;span class="k"&gt;if&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;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&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="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;processStdIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;setFlags&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;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;processFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;setFlags&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;Here, the flag structs are set and the first thing that is checked whether there is a file name in the list of arguments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📝 &lt;br&gt;
&lt;em&gt;&lt;code&gt;args&lt;/code&gt; is a list of arguments starting from after all the flag inputs.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If there is a file that the user has mentioned, call &lt;code&gt;(*Flags).processFile()&lt;/code&gt; method else if the file name is absent or if the file name is given as &lt;code&gt;-&lt;/code&gt; , call &lt;code&gt;(*Flags).processStdIn()&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;(f *Flags).processFile()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this method, we first open the file. In case &lt;code&gt;-r&lt;/code&gt; flag is set, we call the &lt;code&gt;revert()&lt;/code&gt; function. We will look what &lt;code&gt;revert()&lt;/code&gt; does in a few minutes. If the flag is not present, we read a set no. of bytes at a time, from the file and pass it to &lt;code&gt;InputParse()&lt;/code&gt; .&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;(f *Flags).processStdIn()&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, we check if &lt;code&gt;-r&lt;/code&gt; flag is set, and call &lt;code&gt;revert()&lt;/code&gt; accordingly. Otherwise, we scan the standard input and print the resultant hex dump. Here we have to consider additional edge cases, like the result will be displayed upto the no of rows whose columns have been filled completely, else the prompt waits for additional input to read. Unless we interrupt the program, it will continue to run until &lt;code&gt;-l&lt;/code&gt; value is reached(only when &lt;code&gt;-l&lt;/code&gt; is set).&lt;/p&gt;

&lt;p&gt;The code for this two functions are given below:&lt;/p&gt;


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



&lt;p&gt;Now if you look at the code, you will see three functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;revert()&lt;/code&gt;&lt;/strong&gt;
This function is used to convert from hexadecimal dump to the original binary form. There can be two types of input into this function. &lt;code&gt;*os.File&lt;/code&gt; when file is given and &lt;code&gt;*bufio.Scanner&lt;/code&gt; when read is done from standard input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;(f *Flags).checkFlags()&lt;/code&gt;&lt;/strong&gt;
This method properly parses each flag value(originally string value) to numerical value, which then can be used by the &lt;code&gt;InputParse()&lt;/code&gt; method. This method is also responsible to terminate the program if there is any error while parsing the flags.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code for this two functions:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;(flags *Parsedflags).InputParse()&lt;/code&gt;&lt;/strong&gt;
All that is left is two use the helper functions appropriately to generate the proper hex dump. To do that we call this function.
&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ParsedFlags&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;InputParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;offset&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;length&lt;/span&gt; &lt;span class="kt"&gt;int&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="c"&gt;// convert byte slice to hex string&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;byteToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// function to generate hex dump output string&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumpHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&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;Here, first we convert the slice of bytes to hexadecimal string and then call &lt;code&gt;dumpHex()&lt;/code&gt; passing in the offset(this helps in proper indexing of lines),flag values, original slice of bytes and the buffer(hex string). &lt;/p&gt;

&lt;p&gt;So, finally we reach a point where only the conversion is left. To convert from the original input to its hex dump we use the &lt;code&gt;dumpHex()&lt;/code&gt; method.&lt;/p&gt;


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



&lt;p&gt;Since there are two characters(one octet is represented by two characters) per hexadecimal, we loop till twice of the length of input bytes. Then first we print the offset. The next step is to print the grouped octets. The no of octets depends on the flag value of &lt;code&gt;-g&lt;/code&gt; as well as the &lt;code&gt;-c&lt;/code&gt; value. We have to make sure that we reverse each group before printing if little-endian mode is set. &lt;/p&gt;

&lt;p&gt;Once the octets are printed, the text equivalent to the octets are displayed beside them. This three part process is repeated for each row(line) until the end of file or input. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📝&lt;br&gt;
&lt;em&gt;Make sure that if the &lt;code&gt;-l&lt;/code&gt; flag value is set, no of octets that will be printed is equal to that value.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The complete code can be found in this &lt;a href="https://github.com/Sourjaya/sdxxd"&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.5. Building the Go binary and testing the tool.
&lt;/h3&gt;

&lt;p&gt;Once we have finished writing our code, we will run the code &lt;code&gt;go mod tidy&lt;/code&gt; to make sure all the dependencies are in order. Now, let's build the binary executable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The build file is successfully generated. We can finally test our tool.To test it, first we will create a tar file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"File 1 contents"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; file1.txt
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"File 2 contents"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; file2.txt
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"File 3 contents"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; file3.txt
&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-cf&lt;/span&gt; files.tar file1.txt file2.txt file3.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we will use &lt;code&gt;files.tar&lt;/code&gt; to check it out:&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%2Fxahggdeztlh85kb6dztp.gif" 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%2Fxahggdeztlh85kb6dztp.gif" alt="Demo" width="716" height="494"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  5. What's Next
&lt;/h2&gt;

&lt;p&gt;As you may have noticed, this code reads and processes the file(or the input) in a sequential way, and there is no parallel, concurrent processing involved. For the sake of simplicity, I have not used the concepts of concurrency. Therefore, this tool will work, but struggle when there are large files involved. &lt;/p&gt;

&lt;p&gt;Also when it comes to the options that the original &lt;code&gt;xxd&lt;/code&gt; tool has, we have implemented only 6 of the options. There are other options as well that we haven't looked at yet.&lt;/p&gt;

&lt;p&gt;So there is always room to improve and optimize the code adding to its list of functionalities.&lt;/p&gt;

&lt;p&gt;If you would like to share your feedback please feel free to drop a comment below.&lt;/p&gt;

</description>
      <category>go</category>
      <category>cli</category>
    </item>
    <item>
      <title>FullScreen API: Explained in 256 characters</title>
      <dc:creator>Sourjaya Das</dc:creator>
      <pubDate>Fri, 22 Mar 2024 16:50:15 +0000</pubDate>
      <link>https://dev.to/sourjaya/fullscreen-api-explained-in-256-characters-3fp2</link>
      <guid>https://dev.to/sourjaya/fullscreen-api-explained-in-256-characters-3fp2</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explainer
&lt;/h2&gt;

&lt;p&gt;You don't want to write multiple lines of &lt;code&gt;css&lt;/code&gt; and &lt;code&gt;js&lt;/code&gt; to toggle full screen mode, do you? That's where this API comes in handy with &lt;code&gt;requestFullscreen()&lt;/code&gt;, &lt;code&gt;exitFullscreen()&lt;/code&gt; to present an element in fullscreen mode, and to exit fullscreen mode as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Context
&lt;/h2&gt;

&lt;p&gt;Here is a simple example of using &lt;strong&gt;&lt;a href="https://codepen.io/Sourjaya/pen/rNbmjGx"&gt;FullScreen API&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>frontendchallenge</category>
      <category>devchallenge</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Build and Deploy: With AWS (Lambda, API Gateway and DynamoDB) using Golang</title>
      <dc:creator>Sourjaya Das</dc:creator>
      <pubDate>Thu, 14 Mar 2024 05:16:38 +0000</pubDate>
      <link>https://dev.to/sourjaya/build-and-deploy-rest-api-with-aws-lambda-api-gateway-and-dynamodb-using-golang-58ap</link>
      <guid>https://dev.to/sourjaya/build-and-deploy-rest-api-with-aws-lambda-api-gateway-and-dynamodb-using-golang-58ap</guid>
      <description>&lt;p&gt;As of 2024, AWS has officially deprecated the &lt;code&gt;go1.x&lt;/code&gt; runtime which came into effect on December 31,2023 as mentioned in their official blog &lt;a href="https://aws.amazon.com/blogs/compute/migrating-aws-lambda-functions-from-the-go1-x-runtime-to-the-custom-runtime-on-amazon-linux-2/" rel="noopener noreferrer"&gt;post&lt;/a&gt;. In this post, let's revisit and look at how you can build a REST API that interacts with DynamoDB and deploy it as a lambda function using Amazon Linux 2023 runtime. &lt;/p&gt;

&lt;p&gt;For demonstration, I have only shown two functionalities (fetch and create) in this article. But the complete code with all the functionalities can be found &lt;a href="https://github.com/Sourjaya/football-aws-lambda" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are a few standard ways you can deploy the code as a lambda function, such as using a container(Docker), using serverless framework, using terraform scripts or using AWS SAM. &lt;br&gt;
But using &lt;code&gt;.zip&lt;/code&gt; file archives to deploy into AWS lambda is one of the more straight forward ways, as mentioned in the official AWS documentation.&lt;/p&gt;




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

&lt;h4&gt;
  
  
  1. Prerequisites
&lt;/h4&gt;

&lt;h4&gt;
  
  
  2. WRITE the code for the Golang REST API
&lt;/h4&gt;

&lt;h4&gt;
  
  
  3. BUILD the executable file
&lt;/h4&gt;

&lt;h4&gt;
  
  
  4. DEPLOY to AWS
&lt;/h4&gt;

&lt;h4&gt;
  
  
  5. TEST the API
&lt;/h4&gt;



&lt;h2&gt;
  
  
  1. Prerequisites
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1.1. Golang should be installed in your system.
&lt;/h3&gt;

&lt;p&gt;To check wether &lt;code&gt;GO&lt;/code&gt; is installed in your system or not use the command &lt;code&gt;go version&lt;/code&gt; in your terminal. If it is not installed, check the official installation &lt;a href="https://go.dev/doc/install" rel="noopener noreferrer"&gt;steps&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.2. You should have an AWS account and preferrably logged in as an IAM user.
&lt;/h3&gt;

&lt;p&gt;To sign in as a root or an IAM user follow the &lt;a href="https://docs.aws.amazon.com/signin/latest/userguide/how-to-sign-in.html" rel="noopener noreferrer"&gt;steps&lt;/a&gt; mentioned in the official documentation.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.3. Use a code editor of your choice.
&lt;/h3&gt;
&lt;h3&gt;
  
  
  1.4. AWS CLI should be installed and configured.
&lt;/h3&gt;

&lt;p&gt;Creating policies and roles as well as deploying the lambda function can be done using the AWS management console as well as the AWS CLI(on the terminal). In case of using &lt;a href="https://aws.amazon.com/getting-started/guides/setup-environment/module-three/" rel="noopener noreferrer"&gt;AWS CLI&lt;/a&gt;, it should be properly configured.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. WRITE the code for the Golang REST API
&lt;/h2&gt;
&lt;h3&gt;
  
  
  2.1. Folder Structure
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── 📁player-api
    └── 📁cmd
        └── main.go
    └── go.mod
    └── go.sum
    └── 📁pkg
        └── 📁handlers
            └── handlers.go
            └── response.go
        └── 📁player
            └── player.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;dl&gt;
  &lt;dt&gt;&lt;b&gt;NOTE&lt;/b&gt;&lt;/dt&gt;
  &lt;dd&gt;You may structure the project folder anyway you want. But it is always a good practice to follow a standard pattern to organize the folder. The above structure is one of the ways you can set up your &lt;code&gt;Go&lt;/code&gt; projects.
  &lt;/dd&gt;
&lt;/dl&gt;


&lt;h3&gt;
  
  
  2.2 Create your GO Project
&lt;/h3&gt;

&lt;p&gt;Before writing your code you need to setup the directory where you will house your project. After that, open the terminal from the directory, and enter the following command to initialize your project.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# go mod init [module path]&lt;/span&gt;
go mod init github.com/Sourjaya/player-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The go mod init command creates a &lt;code&gt;go.mod&lt;/code&gt; file to track your code's dependencies. Using your own github repository will provide a unique module path for the project.&lt;br&gt;
Now, in &lt;code&gt;main.go&lt;/code&gt; write the following code:&lt;/p&gt;

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

&lt;p&gt;In &lt;code&gt;main.go&lt;/code&gt; create a session by passing in the region, then  create an instance to the DynamoDB client and use &lt;code&gt;lambda.Start(handler)&lt;/code&gt; to interact with an internal Lambda endpoint to pass requests to the handler. Then in the &lt;code&gt;handler&lt;/code&gt; function check for the HTTP method type of the request and call the appropriate functions and return those functions.&lt;br&gt;
Make sure you have an environment variable named &lt;code&gt;AWS_REGION&lt;/code&gt; in your system. To create the environment variable in Linux, use this command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Open ~/.profile&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;nano ~/.profile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then write the &lt;code&gt;export&lt;/code&gt; command at the end of the file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#export AWS_REGION=[your_aws_region]&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;AWS_REGION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ap-south-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And then press &lt;code&gt;CTRL+S&lt;/code&gt; to save and &lt;code&gt;CTRL+X&lt;/code&gt; to exit nano. Now &lt;code&gt;logout&lt;/code&gt;of your system and log back in.&lt;/p&gt;

&lt;p&gt;After you have written the main file, in &lt;code&gt;pkg/handlers/handlers.go&lt;/code&gt; define and write the functions for each http method type. Write the following code:&lt;/p&gt;

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

&lt;p&gt;The &lt;code&gt;GetPlayer&lt;/code&gt; function calls &lt;code&gt;GetPlayerByID&lt;/code&gt; if ID is provided as URL parameter, to fetch the player information with respect to the provided key, else it calls &lt;code&gt;GetPlayers&lt;/code&gt; to fetch information of all the players in the table. The two functions are available in the &lt;code&gt;players&lt;/code&gt; package. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;CreatePlayer&lt;/code&gt; function calls &lt;code&gt;CreatePlayer&lt;/code&gt; from the player package to add a new player record in the table. Both &lt;code&gt;GetPlayer&lt;/code&gt; and &lt;code&gt;Create Player&lt;/code&gt; takes &lt;code&gt;request&lt;/code&gt;, &lt;code&gt;tablename&lt;/code&gt; and &lt;code&gt;client&lt;/code&gt; as function parameters and returns a &lt;code&gt;response&lt;/code&gt; and a &lt;code&gt;http status code&lt;/code&gt;. The &lt;code&gt;Unhandled&lt;/code&gt; function return a &lt;code&gt;405&lt;/code&gt; status code and a error message.&lt;/p&gt;

&lt;p&gt;The response method is responsible to create the &lt;code&gt;APIGatewayProxyResponse&lt;/code&gt; which will contain appropriate headers, status code and the json response body.&lt;/p&gt;

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

&lt;p&gt;Now, in &lt;code&gt;player.go&lt;/code&gt; write the functions that will interact with DynamoDB.&lt;/p&gt;

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

&lt;p&gt;Function &lt;code&gt;GetPlayerByID&lt;/code&gt; takes the ID, tablename and client as parameters and returns a &lt;code&gt;Player&lt;/code&gt; structure containing the player info and an error(if found). &lt;/p&gt;

&lt;p&gt;Function &lt;code&gt;GetPlayers&lt;/code&gt; takes tablename and client as parameters and returns a slice(list) containing info about each player in the database table along with an error(if found).&lt;/p&gt;

&lt;p&gt;Function &lt;code&gt;Create Player&lt;/code&gt; takes request, tablename and client as parameters and returns a &lt;code&gt;Player&lt;/code&gt; structure containing the player info and an error(if found).&lt;/p&gt;



&lt;h2&gt;
  
  
  3. BUILD the executable file
&lt;/h2&gt;
&lt;h3&gt;
  
  
  3.1 Install the dependencies
&lt;/h3&gt;

&lt;p&gt;Now that you have written all the code needed to build the executable file, it is necessary to install all the dependencies and remove unused modules. To do that, in the terminal execute the command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# This will generate go.sum file&lt;/span&gt;
go mod tidy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  3.2 Create the executable file and zip it
&lt;/h3&gt;

&lt;p&gt;If you are using &lt;strong&gt;Linux/Mac&lt;/strong&gt; system execute this command(according to your system architecture use any one of the following commands) to generate the executable:&lt;/p&gt;
&lt;h5&gt;
  
  
  For x86_64 architecture
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64 go build &lt;span class="nt"&gt;-tags&lt;/span&gt; lambda.norpc &lt;span class="nt"&gt;-o&lt;/span&gt; bootstrap main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  For arm64 architecture
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;arm64 go build &lt;span class="nt"&gt;-tags&lt;/span&gt; lambda.norpc &lt;span class="nt"&gt;-o&lt;/span&gt; bootstrap main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  (Optional) To create a stable binary package for standard C library (libc) versions
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;arm64 &lt;span class="nv"&gt;CGO_ENABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 go build &lt;span class="nt"&gt;-o&lt;/span&gt; bootstrap &lt;span class="nt"&gt;-tags&lt;/span&gt; lambda.norpc main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;If you are using &lt;strong&gt;Windows&lt;/strong&gt; system execute the following set of commands in terminal(using CGO_ENABLED is optional):&lt;/p&gt;
&lt;h5&gt;
  
  
  For x86_64 architecture
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux
&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64
go build &lt;span class="nt"&gt;-tags&lt;/span&gt; lambda.norpc &lt;span class="nt"&gt;-o&lt;/span&gt; bootstrap main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  For arm64 architecture
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux
&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;arm64
go build &lt;span class="nt"&gt;-tags&lt;/span&gt; lambda.norpc &lt;span class="nt"&gt;-o&lt;/span&gt; bootstrap main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;dl&gt;
  &lt;dt&gt;&lt;b&gt;NOTE&lt;/b&gt;&lt;/dt&gt;
  &lt;dd&gt;Since we are using &lt;code&gt;provided.al2023&lt;/code&gt;(Amazon Linux 2023 runtime) we have to name our go executable binary file as &lt;code&gt;bootstrap&lt;/code&gt;.
  &lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Now to create the &lt;code&gt;.zip&lt;/code&gt; file in &lt;strong&gt;Linux/Mac&lt;/strong&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# zip -jrm [zip file name along with the extension]&lt;/span&gt;
&lt;span class="c"&gt;# the -m flag deletes the executable &lt;/span&gt;
&lt;span class="c"&gt;# file which is not needed anymore&lt;/span&gt;
zip &lt;span class="nt"&gt;-jrm&lt;/span&gt; football_api.zip bootstrap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And to create the &lt;code&gt;.zip&lt;/code&gt; file in &lt;strong&gt;Windows&lt;/strong&gt; :&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -acf football_api.zip bootstrap
del bootstrap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;h2&gt;
  
  
  4. DEPLOY to AWS
&lt;/h2&gt;
&lt;h3&gt;
  
  
  4.1 Create the Lambda Function
&lt;/h3&gt;
&lt;h3&gt;
  
  
  4.1.1 Using AWS CLI
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Create Execution Policy
&lt;/h4&gt;

&lt;p&gt;To create an execution policy create a json file with the following content:&lt;br&gt;
&lt;strong&gt;policy.json&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Sid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Stmt1428341300017"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"dynamodb:DeleteItem"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"dynamodb:GetItem"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"dynamodb:PutItem"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"dynamodb:Query"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"dynamodb:Scan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"dynamodb:UpdateItem"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Sid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"logs:CreateLogGroup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"logs:CreateLogStream"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"logs:PutLogEvents"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now execute the command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Syntax&lt;/span&gt;
:&lt;span class="s1"&gt;'
aws iam create-policy \
    --policy-name [policy_name] \
    --policy-document file://[file_path/file_name]
'&lt;/span&gt;

&lt;span class="c"&gt;#Command&lt;/span&gt;
aws iam create-policy &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--policy-name&lt;/span&gt; lambda-api &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--policy-document&lt;/span&gt; file://./policy.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;You have successfully created a policy. Remember to note the policy ARN from the response in the terminal. The policy ARN would look something like &lt;code&gt;arn:aws:iam::111111111111:policy/lambda-api&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Execution Role and attach Policy
&lt;/h4&gt;

&lt;p&gt;To create an execution role create a json file with the following content:&lt;br&gt;
&lt;strong&gt;trust-policy.json&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Principal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="nl"&gt;"Service"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lambda.amazonaws.com"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sts:AssumeRole"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now execute the command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Syntax&lt;/span&gt;
:&lt;span class="s1"&gt;'
aws iam create-role --role-name [role_name] \
--assume-role-policy-document file://[file_path/file_name]
'&lt;/span&gt;

&lt;span class="c"&gt;#Command&lt;/span&gt;
&lt;span class="c"&gt;#Remember to note the role ARN as well.&lt;/span&gt;
aws iam create-role &lt;span class="nt"&gt;--role-name&lt;/span&gt; lambda-api &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--assume-role-policy-document&lt;/span&gt; file://./trust-policy.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, we have to attach the &lt;code&gt;policy&lt;/code&gt; we created to this &lt;code&gt;role&lt;/code&gt;. &lt;br&gt;
To do that use this command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Syntax&lt;/span&gt;
:&lt;span class="s1"&gt;'
aws iam attach-role-policy --role-name [role_name] \
--policy-arn [policy_arn that was noted]
'&lt;/span&gt;
&lt;span class="c"&gt;#Command&lt;/span&gt;
aws iam attach-role-policy &lt;span class="nt"&gt;--role-name&lt;/span&gt; lambda-api &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--policy-arn&lt;/span&gt; arn:aws:iam::111111111111:policy/lambda-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Create Lambda function
&lt;/h4&gt;

&lt;p&gt;To create a lambda function using AWS CLI use the following command in the terminal:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Syntax&lt;/span&gt;
:&lt;span class="s1"&gt;'
aws lambda create-function --function-name [lambda_function_name] \
--runtime provided.al2023 --handler [handler_name] \
--architectures [your_system_architecture] \
--role arn:aws:iam::[aws_id]:role/[role_name] \
--zip-file fileb://[zip_file]
'&lt;/span&gt;

&lt;span class="c"&gt;# Command&lt;/span&gt;
aws lambda create-function &lt;span class="nt"&gt;--function-name&lt;/span&gt; new-lambda &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--runtime&lt;/span&gt; provided.al2023 &lt;span class="nt"&gt;--handler&lt;/span&gt; bootstrap &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--role&lt;/span&gt; arn:aws:iam::111111111111:role/lambda-api &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--zip-file&lt;/span&gt; fileb://football_api.zip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;dl&gt;
  &lt;dt&gt;&lt;b&gt;NOTE&lt;/b&gt;&lt;/dt&gt;
  &lt;dd&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;--architectures&lt;/code&gt; option is only required if you're using arm64. The default value is x86_64.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;--role&lt;/code&gt;, specify the Amazon Resource Name (ARN) of the execution role.&lt;/li&gt;
&lt;li&gt;I have not shown my AWS ID in any of the above commands. Put your 12-digit AWS ID in place of those 1's and use it.  &lt;/li&gt;
&lt;li&gt;Replace &lt;code&gt;'\'&lt;/code&gt; from the commands with &lt;code&gt;'^'&lt;/code&gt; if you are using a  &lt;b&gt;Windows&lt;/b&gt; system &lt;/li&gt;
&lt;/ul&gt;
  &lt;/dd&gt;
&lt;/dl&gt;


&lt;h3&gt;
  
  
  4.1.2 Using AWS Management Console
&lt;/h3&gt;

&lt;p&gt;Login to the management console. And follow the steps.&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Execution Policy
&lt;/h4&gt;

&lt;p&gt;To create an execution policy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select &lt;em&gt;&lt;strong&gt;Security credentials&lt;/strong&gt;&lt;/em&gt; from the dropdown when you click on your account at the top right.&lt;/li&gt;
&lt;/ol&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%2Fdjlvppg088ilklo2ijls.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%2Fdjlvppg088ilklo2ijls.png" alt="Security credential" width="800" height="938"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;em&gt;&lt;strong&gt;Access management--&amp;gt;Policies&lt;/strong&gt;&lt;/em&gt; .&lt;/li&gt;
&lt;li&gt;Click on &lt;em&gt;&lt;strong&gt;Create Policy&lt;/strong&gt;&lt;/em&gt; button.&lt;/li&gt;
&lt;li&gt;Now in &lt;em&gt;&lt;strong&gt;Policy editor&lt;/strong&gt;&lt;/em&gt; and under &lt;em&gt;&lt;strong&gt;JSON&lt;/strong&gt;&lt;/em&gt; tab put the content of &lt;code&gt;policy.json&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Now click on &lt;em&gt;&lt;strong&gt;Next&lt;/strong&gt;&lt;/em&gt; and give it a Name(lambda-api) and Description(optional) and then click &lt;em&gt;&lt;strong&gt;Create Policy&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You have successfully created a policy.&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Execution Role and attach Policy
&lt;/h4&gt;

&lt;p&gt;To create an Execution Role:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;em&gt;&lt;strong&gt;Access management--&amp;gt;Roles&lt;/strong&gt;&lt;/em&gt; .&lt;/li&gt;
&lt;li&gt;Under &lt;em&gt;&lt;strong&gt;Trusted entity type&lt;/strong&gt;&lt;/em&gt; select &lt;em&gt;&lt;strong&gt;AWS service&lt;/strong&gt;&lt;/em&gt; and under &lt;em&gt;&lt;strong&gt;Use case&lt;/strong&gt;&lt;/em&gt; select &lt;em&gt;&lt;strong&gt;lambda&lt;/strong&gt;&lt;/em&gt; and click &lt;em&gt;&lt;strong&gt;Next&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;In &lt;em&gt;&lt;strong&gt;Add permissions&lt;/strong&gt;&lt;/em&gt; search and select the policy(lamda-api) you created and click &lt;em&gt;&lt;strong&gt;Next&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Now give it a Name(lambda-api) and click &lt;strong&gt;Create role&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Create Lambda function
&lt;/h4&gt;

&lt;p&gt;To create a lambda function using AWS Console follow the steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Search &lt;em&gt;&lt;strong&gt;Lambda&lt;/strong&gt;&lt;/em&gt; in the search box and go to the &lt;code&gt;Functions&lt;/code&gt; page.&lt;/li&gt;
&lt;li&gt;Click on &lt;em&gt;&lt;strong&gt;Create Function&lt;/strong&gt;&lt;/em&gt; button.&lt;/li&gt;
&lt;li&gt;Give the function a Name(new-lambda), select the Runtime(Amazon Linux 2023) and select the Architecture(x86_64 in my case).&lt;/li&gt;
&lt;li&gt;Under &lt;em&gt;&lt;strong&gt;Permissions&lt;/strong&gt;&lt;/em&gt; tab change the execution role.&lt;/li&gt;
&lt;li&gt;You may choose the role that was created from &lt;em&gt;&lt;strong&gt;Use an existing role&lt;/strong&gt;&lt;/em&gt; or choose &lt;strong&gt;&lt;em&gt;Simple microservice permissions&lt;/em&gt;&lt;/strong&gt; from &lt;strong&gt;Policy templates&lt;/strong&gt; by selecting &lt;em&gt;&lt;strong&gt;Create a new role from AWS policy templates&lt;/strong&gt;&lt;/em&gt; .&lt;/li&gt;
&lt;li&gt;Now after you click on &lt;em&gt;&lt;strong&gt;Create function&lt;/strong&gt;&lt;/em&gt; you have to &lt;em&gt;&lt;strong&gt;Edit&lt;/strong&gt;&lt;/em&gt; the &lt;em&gt;&lt;strong&gt;Runtime Settings&lt;/strong&gt;&lt;/em&gt; and change handler name to &lt;code&gt;bootstrap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The only step that is left is to upload the &lt;em&gt;&lt;strong&gt;Code Source&lt;/strong&gt;&lt;/em&gt; which will be the &lt;code&gt;zip&lt;/code&gt; file that was created.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You have successfully created a Lambda function.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.2 Create a table in DynamoDB
&lt;/h3&gt;

&lt;p&gt;We can create a table in DynamoDB using both the AWS CLI or the Console. I prefer to use the Console, as the table attributes are created through code. But if you want to learn how to use AWS CLI to use DynamoDB follow this &lt;a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To create a table using DynamoDB:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Search &lt;em&gt;&lt;strong&gt;DynamoDB&lt;/strong&gt;&lt;/em&gt; in the search box and go to the &lt;code&gt;Tables&lt;/code&gt; page.&lt;/li&gt;
&lt;li&gt;Click on &lt;em&gt;&lt;strong&gt;Create Table&lt;/strong&gt;&lt;/em&gt; and give the table a Name(you have mentioned in your code).&lt;/li&gt;
&lt;li&gt;Enter &lt;em&gt;&lt;strong&gt;Partition Key&lt;/strong&gt;&lt;/em&gt;(which will be the primary key). In this case it will be the &lt;code&gt;id&lt;/code&gt; field.&lt;/li&gt;
&lt;li&gt;Click on &lt;em&gt;&lt;strong&gt;Create Table&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  4.3 Create a REST API in APIGateway
&lt;/h3&gt;

&lt;p&gt;To create the API:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Search &lt;em&gt;&lt;strong&gt;API Gateway&lt;/strong&gt;&lt;/em&gt; in the search box and in the &lt;code&gt;API&lt;/code&gt; page click on &lt;em&gt;&lt;strong&gt;Create API&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Now scroll down and click &lt;em&gt;&lt;strong&gt;Build&lt;/strong&gt;&lt;/em&gt; on &lt;em&gt;&lt;strong&gt;REST API&lt;/strong&gt;&lt;/em&gt; tab. &lt;/li&gt;
&lt;li&gt;Under &lt;em&gt;&lt;strong&gt;API details&lt;/strong&gt;&lt;/em&gt; tab select &lt;em&gt;&lt;strong&gt;New API&lt;/strong&gt;&lt;/em&gt; and then give tha API a name and select &lt;code&gt;Regional&lt;/code&gt; in &lt;em&gt;&lt;strong&gt;API endpoint type&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;After you click on &lt;em&gt;&lt;strong&gt;Create API&lt;/strong&gt;&lt;/em&gt;, the page will be redirected to &lt;em&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/em&gt; under the API. Click on &lt;em&gt;&lt;strong&gt;Create method&lt;/strong&gt;&lt;/em&gt; under &lt;em&gt;&lt;strong&gt;Methods&lt;/strong&gt;&lt;/em&gt; tab.&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;ANY&lt;/code&gt; as &lt;em&gt;&lt;strong&gt;Method type&lt;/strong&gt;&lt;/em&gt;, Chose the &lt;em&gt;&lt;strong&gt;Lambda function&lt;/strong&gt;&lt;/em&gt; name(or alias) and the region(in my case it was &lt;code&gt;ap-south-1&lt;/code&gt;) .&lt;/li&gt;
&lt;li&gt;Enable the &lt;em&gt;&lt;strong&gt;Lambda proxy integration&lt;/strong&gt;&lt;/em&gt; and &lt;em&gt;&lt;strong&gt;Default timeout&lt;/strong&gt;&lt;/em&gt; options and click on &lt;em&gt;&lt;strong&gt;Create method&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The final step is to click on &lt;em&gt;&lt;strong&gt;Deploy API&lt;/strong&gt;&lt;/em&gt;, create a &lt;em&gt;&lt;strong&gt;New Stage&lt;/strong&gt;&lt;/em&gt;(staging) and &lt;em&gt;&lt;strong&gt;Deploy&lt;/strong&gt;&lt;/em&gt;. Make sure to note the &lt;code&gt;Invoke URL&lt;/code&gt; which will look like this: &lt;code&gt;https://unp18fmgcc.execute-api.ap-south-1.amazonaws.com/staging&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You will see something like this:&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%2Fgpulnqb3adcq0uvjn6cg.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%2Fgpulnqb3adcq0uvjn6cg.png" alt="Lambda Overview" width="800" height="403"&gt;&lt;/a&gt;&lt;br&gt;
You have successfully deployed a REST API using a serverless stack. Now we will look at how we can Test the API and wether the endpoints are working or not.&lt;/p&gt;


&lt;h2&gt;
  
  
  5. TEST the API
&lt;/h2&gt;

&lt;p&gt;The API will have the following Endpoints:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HTTP Method&lt;/th&gt;
&lt;th&gt;EndPoint&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;/staging&lt;/td&gt;
&lt;td&gt;Retrieve all players.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;/staging?id={ID}&lt;/td&gt;
&lt;td&gt;Retrieve a player by ID.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;/staging&lt;/td&gt;
&lt;td&gt;Create a new player.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can Test your API using the good old &lt;code&gt;curl&lt;/code&gt; command. The commands are given below:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. Get All Players.
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Syntax&lt;/span&gt;
&lt;span class="c"&gt;#curl -X GET [Invoke URL]&lt;/span&gt;
&lt;span class="c"&gt;#Command&lt;/span&gt;
curl &lt;span class="nt"&gt;-X&lt;/span&gt; GET https://unp18fmgcc.execute-api.ap-south-1.amazonaws.com/staging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  2. Get Player by ID.
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Syntax&lt;/span&gt;
&lt;span class="c"&gt;#curl -X GET [Invoke URL]?id=[id]&lt;/span&gt;
&lt;span class="c"&gt;#Command&lt;/span&gt;
curl &lt;span class="nt"&gt;-X&lt;/span&gt; GET https://unp18fmgcc.execute-api.ap-south-1.amazonaws.com/staging?id&lt;span class="o"&gt;=&lt;/span&gt;7ff4d720-a825-4727-ad95-e37dd62b90cf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  3. Create Player.
&lt;/h4&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Syntax&lt;/span&gt;
:&lt;span class="s1"&gt;'
curl --header "Content-Type: application/json" --request POST --data [JSON data] [Invoke URL]
'&lt;/span&gt;
&lt;span class="c"&gt;#Command&lt;/span&gt;
curl &lt;span class="nt"&gt;--header&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="nt"&gt;--request&lt;/span&gt; POST &lt;span class="nt"&gt;--data&lt;/span&gt; &lt;span class="s1"&gt;'{"firstName": "Luis", "lastName": "Suarez",    "country":"uru","position":"FW","club":"Inter Miami"}'&lt;/span&gt; https://unp18fmgcc.execute-api.ap-south-1.amazonaws.com/staging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Postman&lt;/strong&gt; can also be used to Test the endpoints and you can make a  postman collection similar to:&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%2Ftgdfghqv18goql00zxeb.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%2Ftgdfghqv18goql00zxeb.png" alt="Postman" width="800" height="402"&gt;&lt;/a&gt; &lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-golang.html" rel="noopener noreferrer"&gt;AWS Lambda&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.html" rel="noopener noreferrer"&gt;AWS DynamoDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-with-lambda-integration.html" rel="noopener noreferrer"&gt;AWS APIGateway&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/getting-started/guides/setup-environment" rel="noopener noreferrer"&gt;AWS Tutorials&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;I wanted to document what I learned and how I deployed to AWS lambda in an article so that beginners like me can get help from it. &lt;/p&gt;

&lt;p&gt;If you found this article informative, and if you have any feedback, do drop a comment. And let's connect on &lt;a href="https://twitter.com/sourjaya_das" rel="noopener noreferrer"&gt;𝕏&lt;/a&gt;. Thank You.&lt;/p&gt;

</description>
      <category>go</category>
      <category>aws</category>
      <category>dynamodb</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
