<?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: Harendra Kumar Kanojiya</title>
    <description>The latest articles on DEV Community by Harendra Kumar Kanojiya (@harendra21).</description>
    <link>https://dev.to/harendra21</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%2F166164%2F18bf6553-8467-478e-9dc2-5ca88129ecef.jpeg</url>
      <title>DEV Community: Harendra Kumar Kanojiya</title>
      <link>https://dev.to/harendra21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harendra21"/>
    <language>en</language>
    <item>
      <title>Looking for powerful, free tools to level up your development workflow?</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Tue, 17 Dec 2024 07:38:53 +0000</pubDate>
      <link>https://dev.to/harendra21/looking-for-powerful-free-tools-to-level-up-your-development-workflow-4oog</link>
      <guid>https://dev.to/harendra21/looking-for-powerful-free-tools-to-level-up-your-development-workflow-4oog</guid>
      <description>&lt;p&gt;When it comes to modern software development, having the right tools makes all the difference. Developer Tools like code editors, version control, and debugging tools streamline coding and collaboration. Automation tools simplify repetitive tasks, from testing to CI/CD pipelines, boosting productivity. On the Backend side, frameworks and databases handle application logic and data management efficiently. Cloud Storage solutions ensure scalable, secure, and remote access to data, making storage headaches a thing of the past. Lastly, Deployment Tools help developers roll out updates seamlessly, manage servers, and ensure applications run smoothly across environments. Together, these tools power a faster, more efficient development cycle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1sul5r7wg7sncp38wz86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1sul5r7wg7sncp38wz86.png" alt="opensource alternatives for developers" width="800" height="671"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>techtools</category>
      <category>tools</category>
    </item>
    <item>
      <title>Unlock The Secrets Of Microservice Architecture</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Wed, 28 Aug 2024 10:52:00 +0000</pubDate>
      <link>https://dev.to/harendra21/unlock-the-secrets-of-microservice-architecture-1cmi</link>
      <guid>https://dev.to/harendra21/unlock-the-secrets-of-microservice-architecture-1cmi</guid>
      <description>&lt;p&gt;There are many types of software design patterns available, MicroService-based architecture is one of the most common architectures that is widely used by big giants such as Google, Netflix, Amazon, and Facebook. So basically you can say that we prefer to use the micro-service architecture to support large-scale systems due to its benefits, which we will see in a few minutes.&lt;/p&gt;

&lt;p&gt;Before beginning with this article I want to let you know about how many types of main software architecture are available, below is the list of them —&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Monolithic&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Microservices&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; Serverless&lt;/li&gt;
&lt;li&gt; Event-Driven&lt;/li&gt;
&lt;li&gt; Layered (N-Tier)&lt;/li&gt;
&lt;li&gt; Service-Oriented Architecture (SOA)&lt;/li&gt;
&lt;li&gt; Component-Based&lt;/li&gt;
&lt;li&gt; Peer-to-Peer (P2P)&lt;/li&gt;
&lt;li&gt; Domain-Driven Design (DDD)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What are  &lt;strong&gt;Microservices?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The microservice-based architecture is a design pattern in which we divide the larger services into smaller independent services and those services can communicate with each other with the help of well-defined APIs. Every service has its roles and responsibilities and it focuses only on that business function. Some characteristics of microservice include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Modularity&lt;/strong&gt;:  We are developing the services in independent modules that are easy to develop, maintain, reuse, and scale.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Decentralization&lt;/strong&gt;: This is another key characteristic of microservice Architecture, different services can use different technologies such as databases, programming languages, etc.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Scalability&lt;/strong&gt;: We can scale independent services based on the demand which is more useful to optimize the resources.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Autonomy&lt;/strong&gt;: We can develop, deploy, and run each service independently, which helps us to improve flexibility and resilience.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Continuous Deployment&lt;/strong&gt;: The independent running service is more flexible to update and maintain, which helps to achieve continuous integration and deployment. &lt;a href="https://medium.com/@harendra21/understand-microservice-based-architecture-977bdc676ae3" rel="noopener noreferrer"&gt;read more&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microservices</category>
      <category>softwaredevelopment</category>
      <category>software</category>
      <category>architecture</category>
    </item>
    <item>
      <title>How To Write Maintainable Go Code?</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Wed, 21 Aug 2024 09:21:00 +0000</pubDate>
      <link>https://dev.to/harendra21/how-to-write-maintainable-go-code-45hp</link>
      <guid>https://dev.to/harendra21/how-to-write-maintainable-go-code-45hp</guid>
      <description>&lt;p&gt;Hi everyone, Gophers You have to write code that can be maintained when dealing with golang. &lt;strong&gt;Simplicity&lt;/strong&gt;, &lt;strong&gt;readability&lt;/strong&gt;, and &lt;strong&gt;clarity&lt;/strong&gt; are the fundamental components of maintainable code; these qualities help new team members grasp your work. When an individual departs, it facilitates their ability to swiftly and effortlessly continue where they left off in terms of code. If you want to write more maintainable code, you might want to think about applying these strategies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the &lt;code&gt;main&lt;/code&gt; as small as possible
&lt;/h2&gt;

&lt;p&gt;As we all know the main is the first goroutine function at the start of your go program, so we call it  &lt;strong&gt;&lt;em&gt;main goroutine&lt;/em&gt;&lt;/strong&gt;. It is unique because the main package is compiled into an executable program rather than being treated like a regular package by the compiler and has exported names. The main function (&lt;code&gt;main.main&lt;/code&gt;), which serves as a  &lt;strong&gt;&lt;em&gt;Go program’s entry point&lt;/em&gt;&lt;/strong&gt;, is located inside the main package. The primary function and package are expected to perform as little as feasible.&lt;/p&gt;

&lt;p&gt;It is strongly advised not to implement the business logic inside the main package and instead to drive the program with &lt;code&gt;main.main&lt;/code&gt; since it is  &lt;strong&gt;&lt;em&gt;difficult to write tests&lt;/em&gt;&lt;/strong&gt;  for the code inside &lt;code&gt;main.main&lt;/code&gt;. The structure and maintainability of the software are enhanced by separating the driver and business logic into different packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
 &lt;span class="s"&gt;"fmt"&lt;/span&gt;  
 &lt;span class="s"&gt;"log"&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c"&gt;// main should only handle high-level orchestration  &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="c"&gt;// Initialize the application  &lt;/span&gt;
 &lt;span class="n"&gt;app&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;initializeApp&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;log&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;"Failed to initialize the app: %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="c"&gt;// Run the application  &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;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="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;log&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;"Application error: %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="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// Application struct encapsulates the application's state  &lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Application&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;config&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="c"&gt;// example field  &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// initializeApp initializes the application and returns an instance of it  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;initializeApp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Application&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="c"&gt;// Simulate loading configuration  &lt;/span&gt;
 &lt;span class="n"&gt;config&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;loadConfig&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="c"&gt;// Create and return an Application instance  &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;Application&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;config&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="c"&gt;// run starts the application logic  &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;app&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;run&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="c"&gt;// Simulate running the application  &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;"Running application with config:"&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;config&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="c"&gt;// loadConfig simulates loading the configuration for the application  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;loadConfig&lt;/span&gt;&lt;span class="p"&gt;()&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;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;// Simulate configuration loading  &lt;/span&gt;
 &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"App Configuration"&lt;/span&gt;  
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;config&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;h2&gt;
  
  
  Always use meaningful names
&lt;/h2&gt;

&lt;p&gt;In Golang, it is crucial to provide variables, functions, structs and interfaces meaningful names. Thus, we have selected significant names and clarify what they stand for. Do not just pick names at random that have no connection to the topic. Though picking a name might occasionally be very difficult, you will not regret it because it makes your code easier to understand and read for both you and other people.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example with bad names&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
 &lt;span class="s"&gt;"fmt"&lt;/span&gt;  
 &lt;span class="s"&gt;"math"&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c"&gt;// m function performs some calculations and prints results  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
 &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;5.0&lt;/span&gt;  
 &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;cA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The result is %.2f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

 &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;4.0&lt;/span&gt;  
 &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sA&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;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The result is %.2f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// cA calculates something related to circles  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;cA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="kt"&gt;float64&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="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;Pi&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// sA calculates something related to squares  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sA&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;float64&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="k"&gt;return&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;s&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see that in this example, the names do not indicate what we hope to accomplish in this section of code, which is highly problematic for both reliable and well-written code. Nobody will understand what we want to do with this code when they see this, and eventually, it will be too late for you to grasp either.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example with meaningful names&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
 &lt;span class="s"&gt;"fmt"&lt;/span&gt;  
 &lt;span class="s"&gt;"math"&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c"&gt;// main function orchestrates the calculation and output  &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;radius&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;5.0&lt;/span&gt;  
 &lt;span class="n"&gt;circleArea&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;calculateCircleArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the circle with radius %.2f is %.2f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;circleArea&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

 &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;4.0&lt;/span&gt;  
 &lt;span class="n"&gt;squareArea&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;calculateSquareArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The area of the square with side length %.2f is %.2f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;squareArea&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// calculateCircleArea computes the area of a circle given its radius  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;calculateCircleArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="kt"&gt;float64&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="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;Pi&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// calculateSquareArea computes the area of a square given the length of one side  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;calculateSquareArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="kt"&gt;float64&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sideLength&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Try to write meaningful comments wherever possible
&lt;/h2&gt;

&lt;p&gt;The only method to give a sense of what you want to do with the code is to provide comments, which are a more accurate approach to understanding what is occurring with the code. As a result, you must include thorough comments with every significant part of the code.&lt;/p&gt;

&lt;p&gt;Not only is it crucial to write comments, but we also need to update the comments anytime we update the code to avoid being misled. Block comments and inline  &lt;a href="https://golang.withcodeexample.com/blog/golang-documentation-and-comments-guide/" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;comments&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  are both supported in Golang. Additionally, as it is illogical to explain self-explanatory code, I would caution against making excessive comments on the 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="p"&gt;(&lt;/span&gt;  
 &lt;span class="s"&gt;"errors"&lt;/span&gt;  
 &lt;span class="s"&gt;"fmt"&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c"&gt;// main is the entry point of the application  &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;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&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="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="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  

 &lt;span class="c"&gt;// Calculate the sum of the slice elements  &lt;/span&gt;
 &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;calculateSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of numbers: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

 &lt;span class="c"&gt;// Find the maximum value in the slice  &lt;/span&gt;
 &lt;span class="n"&gt;max&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;findMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error: %v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Maximum value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// calculateSum adds up all the integers in the provided slice  &lt;/span&gt;
&lt;span class="c"&gt;// It returns the total sum of the elements  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;calculateSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&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="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
 &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;  

 &lt;span class="c"&gt;// Iterate over each number in the slice and add it to the sum  &lt;/span&gt;
 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&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;nums&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;num&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;sum&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// findMax returns the maximum value in the provided slice of integers  &lt;/span&gt;
&lt;span class="c"&gt;// If the slice is empty, it returns an error  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;findMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&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="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="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;// Check if the slice is empty  &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;nums&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="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="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;"slice is empty, cannot determine maximum value"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt;  

 &lt;span class="c"&gt;// Assume the first element is the maximum  &lt;/span&gt;
 &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;nums&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="c"&gt;// Iterate over the slice to find the actual maximum  &lt;/span&gt;
 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
   &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&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;max&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;h2&gt;
  
  
  Don’t repeat the piece of code
&lt;/h2&gt;

&lt;p&gt;Writing the same code over and over again is a very terrible habit in all programming languages, not just Golang. The  &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself#:~:text=%22Don't%20repeat%20yourself%22,redundancy%20in%20the%20first%20place." rel="noopener noreferrer"&gt;&lt;strong&gt;DRY&lt;/strong&gt;&lt;/a&gt;  (Do not Repeat Yourself) concept explicitly forbids using this technique. I am going to make a separate function if I need the same code in at least two different places.&lt;/p&gt;

&lt;p&gt;Repeatedly writing the same code may cause overhead when it comes time to update it, forcing us to change it everywhere and sometimes missing it altogether.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of bad practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
 &lt;span class="s"&gt;"fmt"&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c"&gt;// main function demonstrates the bad practice of code repetition  &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="c"&gt;// Repeating the calculation for different sets of numbers  &lt;/span&gt;
 &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;  
 &lt;span class="n"&gt;sum1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;  
 &lt;span class="n"&gt;product1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Product of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

 &lt;span class="n"&gt;num3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num4&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="m"&gt;20&lt;/span&gt;  
 &lt;span class="n"&gt;sum2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;num3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num4&lt;/span&gt;  
 &lt;span class="n"&gt;product2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;num3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num4&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Product of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

 &lt;span class="n"&gt;num5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num6&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;  
 &lt;span class="n"&gt;sum3&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;num5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num6&lt;/span&gt;  
 &lt;span class="n"&gt;product3&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;num5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;num6&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Product of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product3&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;&lt;strong&gt;Example of good practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;  

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;  
 &lt;span class="s"&gt;"fmt"&lt;/span&gt;  
&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c"&gt;// main function demonstrates the good practice of avoiding code repetition  &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="c"&gt;// Using a reusable function to calculate and display results  &lt;/span&gt;
 &lt;span class="n"&gt;displayResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;displayResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;displayResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// displayResults calculates the sum and product of two numbers and prints the results  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;displayResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
 &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;  
 &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sum of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Product of %d and %d is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&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;h2&gt;
  
  
  Don’t use deep nesting in your code
&lt;/h2&gt;

&lt;p&gt;Everyone may find it annoying when there are too many nested codes used because it makes the code harder to read. Therefore, it is quite challenging to understand code — whether it was developed by us or by someone else — when reading hierarchical code. Always strive to keep the code from repeatedly nesting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of bad practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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;"fmt"&lt;/span&gt;  

&lt;span class="c"&gt;// main function demonstrates the bad practice of deep nesting  &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;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt;  

 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;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;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;2&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;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;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;"The number is positive, even, and greater than 10."&lt;/span&gt;&lt;span class="p"&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;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;"The number is positive, even, and 10 or less."&lt;/span&gt;&lt;span class="p"&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;else&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;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;"The number is positive, odd, and greater than 10."&lt;/span&gt;&lt;span class="p"&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;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;"The number is positive, odd, and 10 or less."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
   &lt;span class="p"&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;else&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;"The number is not positive."&lt;/span&gt;&lt;span class="p"&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;p&gt;&lt;strong&gt;Example of good practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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;"fmt"&lt;/span&gt;  

&lt;span class="c"&gt;// main function demonstrates the good practice of avoiding deep nesting  &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;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt;  

 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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;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;"The number is not positive."&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;describeNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// describeNumber prints the description of the number based on its value  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;describeNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&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;number&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;2&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;describeEvenNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&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;describeOddNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// describeEvenNumber handles even numbers  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;describeEvenNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;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;"The number is positive, even, and greater than 10."&lt;/span&gt;&lt;span class="p"&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;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;"The number is positive, even, and 10 or less."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="c"&gt;// describeOddNumber handles odd numbers  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;describeOddNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;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;"The number is positive, odd, and greater than 10."&lt;/span&gt;&lt;span class="p"&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;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;"The number is positive, odd, and 10 or less."&lt;/span&gt;&lt;span class="p"&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;h2&gt;
  
  
  Return early and use conditions very wisely
&lt;/h2&gt;

&lt;p&gt;In programming  &lt;strong&gt;Return early&lt;/strong&gt; is the practice where we need to return from the function as soon as we achieve the goal to avoid the unnecessary processing with the code and reduce the code complexity. It also helps to prevent deep nesting which I already explained in the above example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of bad practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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;"fmt"&lt;/span&gt;  

&lt;span class="c"&gt;// checkTemperature categorizes the temperature into different ranges  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;checkTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="s"&gt;"Freezing cold"&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;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;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;15&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;"Cold"&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;25&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;"Mild"&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;25&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;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;35&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;"Warm"&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hot"&lt;/span&gt;  
      &lt;span class="p"&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Error"&lt;/span&gt;  
     &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&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;else&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;"Error"&lt;/span&gt;  
  &lt;span class="p"&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;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;temp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;22&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;"The weather is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;checkTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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;&lt;strong&gt;Example of good practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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;"fmt"&lt;/span&gt;  

&lt;span class="c"&gt;// checkTemperature categorizes the temperature into different ranges  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;checkTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="s"&gt;"Freezing cold"&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;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;15&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;"Cold"&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;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;25&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;"Mild"&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;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;35&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;"Warm"&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;"Hot"&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;temp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;22&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;"The weather is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;checkTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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;h2&gt;
  
  
  Use switch case more often instead of if/else
&lt;/h2&gt;

&lt;p&gt;The switch statement is the most effective way to reduce the length of functions in comparison with the if-else statement. The switch case statement  &lt;strong&gt;directly executes&lt;/strong&gt;  the matching condition and exits the code to avoid unnecessary processing but the if-else statement checks all conditions until it matches with the one which causes unnecessary processing.&lt;/p&gt;

&lt;p&gt;Switch statement also helps to improve the  &lt;strong&gt;code readability&lt;/strong&gt;  and  &lt;strong&gt;reduce the code complexity&lt;/strong&gt;. Switch cases provide  &lt;strong&gt;ease of maintenance,&lt;/strong&gt;  &lt;strong&gt;performance optimization,&lt;/strong&gt;  and  &lt;strong&gt;easy debugging&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of bad practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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;"fmt"&lt;/span&gt;  

&lt;span class="c"&gt;// categorizeTemperature categorizes the temperature using if-else  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;categorizeTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="s"&gt;"Freezing cold"&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;15&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;"Cold"&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;25&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;"Mild"&lt;/span&gt;  
 &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;35&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;"Warm"&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hot"&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;temp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;22&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;"The weather is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;categorizeTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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;&lt;strong&gt;Example of good practice&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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;"fmt"&lt;/span&gt;  

&lt;span class="c"&gt;// categorizeTemperature categorizes the temperature using switch  &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;categorizeTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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="k"&gt;switch&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;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Freezing cold"&lt;/span&gt;  
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Cold"&lt;/span&gt;  
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Mild"&lt;/span&gt;  
 &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;35&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Warm"&lt;/span&gt;  
 &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hot"&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;temp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;22&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;"The weather is:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;categorizeTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&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;h2&gt;
  
  
  Continuous code refactoring
&lt;/h2&gt;

&lt;p&gt;It is crucial to periodically restructure the code in huge codebases and update it to reflect new developments. Golang is constantly changing it, thus it is necessary to update the codebase to the most recent version and apply the most recent modifications when needed. One of the most crucial aspects of code maintenance is the ability to maintain our code more modern and secure through refactoring.&lt;/p&gt;

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

&lt;p&gt;By following the small steps we can improve the code quality and maintainability of the code. There are small steps but very effective and I think most of them are not only stick to the golang, you can implement the same concepts in Java, Python, JavaScript, or any programming language, and it will be almost the same for others.&lt;/p&gt;

&lt;p&gt;Thank you for reading this blog post, if you are interested in learning more about golang or full-stack development,  &lt;strong&gt;&lt;em&gt;consider following me&lt;/em&gt;&lt;/strong&gt;. I keep writing posts about it regularly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;em&gt;Medium —&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://medium.com/@harendra21" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;https://medium.com/@harendra21&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;em&gt;Twitter (X) —&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://x.com/harendraverma2" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;https://x.com/harendraverma2&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>coding</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Introducing Go 1.23: What's New in the Latest Release?</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Wed, 14 Aug 2024 06:17:00 +0000</pubDate>
      <link>https://dev.to/harendra21/introducing-go-123-whats-new-in-the-latest-release-h89</link>
      <guid>https://dev.to/harendra21/introducing-go-123-whats-new-in-the-latest-release-h89</guid>
      <description>&lt;p&gt;Go, the popular programming language has released version 1.23, bringing many improvements, optimizations, and new features. Released six months after Go 1.22, this update enhances the toolchain, runtime, and libraries while maintaining backward compatibility.&lt;/p&gt;

&lt;h4&gt;
  
  
  Language Changes
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Range-Over-Func Becomes Official&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The "range-over-func" experiment introduced in Go 1.22 is now part of the official language. This allows the &lt;code&gt;range&lt;/code&gt; clause in a &lt;code&gt;for-range&lt;/code&gt; loop to accept iterator functions of specific types. This update enables more flexible iteration patterns, improving the efficiency and readability of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preview of Generic Type Aliases&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Go 1.23 also introduces preview support for generic type aliases. By building the toolchain with &lt;code&gt;GOEXPERIMENT=aliastypeparams&lt;/code&gt;, developers can experiment with this feature within a package, although cross-package usage is not yet supported.&lt;/p&gt;

&lt;h4&gt;
  
  
  Toolchain Enhancements
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Telemetry&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
One of the most significant updates in Go 1.23 is the introduction of Go telemetry. This opt-in system allows the Go toolchain to collect anonymous usage and breakage statistics, helping the Go team understand toolchain performance and usage patterns. Users can opt into telemetry by running &lt;code&gt;go telemetry on&lt;/code&gt;, which sends weekly reports to telemetry.go.dev.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go Command Updates&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Several new flags and commands have been introduced to enhance the Go command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GOROOT_FINAL&lt;/strong&gt;: Setting this environment variable no longer affects the toolchain, simplifying the installation process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;go env -changed&lt;/strong&gt;: Prints only the settings that differ from the default values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;go mod tidy -diff&lt;/strong&gt;: Allows users to see the necessary changes as a unified diff without modifying files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;go list -m -json&lt;/strong&gt;: Now includes &lt;code&gt;Sum&lt;/code&gt; and &lt;code&gt;GoModSum&lt;/code&gt; fields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;godebug Directive&lt;/strong&gt;: A new directive in &lt;code&gt;go.mod&lt;/code&gt; and &lt;code&gt;go.work&lt;/code&gt; files that applies a &lt;code&gt;GODEBUG&lt;/code&gt; setting for the module or workspace.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Vet and Cgo Improvements
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;go vet&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The &lt;code&gt;go vet&lt;/code&gt; subcommand now includes the &lt;code&gt;stdversion&lt;/code&gt; analyzer, which flags references to symbols that are too new for the version of Go in effect. This helps maintain compatibility with older Go versions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cgo Enhancements&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;cmd/cgo&lt;/code&gt; now supports the &lt;code&gt;-ldflags&lt;/code&gt; flag, which allows passing flags to the C linker, helping to avoid errors with large &lt;code&gt;CGO_LDFLAGS&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Runtime and Compiler Optimizations
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Runtime&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The traceback printed after an unhandled panic or fatal error now has better formatting, making it easier to distinguish between error messages and stack traces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compiler&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Go 1.23 reduces the build time overhead when using Profile Guided Optimization (PGO), with overhead now in the single-digit percentages. The compiler also optimizes stack usage by overlapping stack frame slots of local variables accessed in disjoint regions of a function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Linker Updates
&lt;/h4&gt;

&lt;p&gt;The linker now enforces stricter rules on the use of &lt;code&gt;//go:linkname&lt;/code&gt; directives, disallowing references to internal symbols in the standard library unless they are explicitly marked. This improves security and reliability in Go programs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Standard Library Changes
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Time Package&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Go 1.23 introduces significant changes to the &lt;code&gt;time.Timer&lt;/code&gt; and &lt;code&gt;time.Ticker&lt;/code&gt; implementations. Unstopped timers and tickers now become eligible for garbage collection immediately, improving memory management. Additionally, the associated channels are now unbuffered, which guarantees that no stale values will be sent or received after a &lt;code&gt;Reset&lt;/code&gt; or &lt;code&gt;Stop&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New Packages&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;unique&lt;/strong&gt;: A new package that provides facilities for canonicalizing values, allowing programs to deduplicate values and reduce memory footprint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;iter&lt;/strong&gt;: A package that defines user-defined iterators and provides various functions for working with slices and maps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;structs&lt;/strong&gt;: A package that provides types for struct fields, starting with &lt;code&gt;HostLayout&lt;/code&gt;, which conforms to host platform memory layout expectations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Minor Library Updates&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Several libraries, including &lt;code&gt;archive/tar&lt;/code&gt;, &lt;code&gt;crypto/tls&lt;/code&gt;, &lt;code&gt;crypto/x509&lt;/code&gt;, and &lt;code&gt;database/sql&lt;/code&gt;, have received minor updates, improving functionality and security.&lt;/p&gt;

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

&lt;p&gt;Go 1.23 is a robust update that enhances the language's capabilities, optimizes performance, and introduces new tools and features that developers will find useful. Whether you're interested in experimenting with new language features or taking advantage of the latest toolchain improvements, Go 1.23 has something for everyone. As always, the Go team encourages developers to explore the new features and contribute feedback to help shape the future of the language.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>this is title</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Mon, 15 Apr 2024 10:03:34 +0000</pubDate>
      <link>https://dev.to/harendra21/this-is-title-3ah4</link>
      <guid>https://dev.to/harendra21/this-is-title-3ah4</guid>
      <description>&lt;h2&gt;
  
  
  hello
&lt;/h2&gt;

</description>
    </item>
    <item>
      <title>Hello, World!</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Mon, 15 Apr 2024 09:45:53 +0000</pubDate>
      <link>https://dev.to/harendra21/hello-world-19ml</link>
      <guid>https://dev.to/harendra21/hello-world-19ml</guid>
      <description>&lt;p&gt;Hello DEV, this is my first post&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>help</category>
    </item>
    <item>
      <title>Web Scraping With Selenium In Python</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Tue, 09 Apr 2024 17:31:00 +0000</pubDate>
      <link>https://dev.to/harendra21/web-scraping-with-selenium-in-python-49i5</link>
      <guid>https://dev.to/harendra21/web-scraping-with-selenium-in-python-49i5</guid>
      <description>&lt;p&gt;Hello, developers. In this brief lesson, I will teach you “how to use Selenium and its various capabilities to scrape and scan any web page.” From identifying items to waiting for dynamic material to load, we’ve got you covered. Change the size of the window and capture screenshots. To prevent blockages, use proxies and special headers. With our headless browser, you can do all of that and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python3 is required for the code to function. It comes pre-installed on some platforms. After that, install Selenium, Chrome, and the Chrome driver. As of this writing, the browser and driver versions should be the same, the latest Chrome browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Selenium with PIP
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;selenium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other browsers (Edge, IE, Firefox, Opera, and Safari) are supported, and the code should function with little modifications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Once everything is in place, we’ll create our first test. Print the current URL and title of an example URL. The browser will automatically follow redirection and load all resources, including images, stylesheets, javascript, and others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;selenium&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;  

&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;http://zenrows.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chrome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# https://www.zenrows.com/  
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Web Scraping API &amp;amp; Data Extraction - ZenR
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Download chromedriver&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://chromedriver.chromium.org/downloads" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;here&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your &lt;a href="https://chromedriver.chromium.org/downloads" rel="noopener noreferrer"&gt;Chromedriver&lt;/a&gt; is not in an executable path, you must provide it or transfer it to a location in the path (for example, /usr/bin/).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;chrome_driver_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/path/to/chromedriver&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;  
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;executable_path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;chrome_driver_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
&lt;span class="c1"&gt;# ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will notice that the browser is visible, haven’t you? By default, it will not operate headlessly. We can provide the driver alternatives, which is what we want to do for scraping.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ChromeOptions&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headless&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;  
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
&lt;span class="c1"&gt;# ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Identifying Elements and Content
&lt;/h2&gt;

&lt;p&gt;Once the website has loaded, we may begin searching for the information we want. Selenium provides numerous methods for accessing items, including ID, tag name, class, XPath, and CSS selectors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Read in detail&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://selenium-python.readthedocs.io/locating-elements.html" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;here&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assume we want to use the text input to search for anything on Amazon. From the preceding alternatives, we may utilize select by tag: driver.find element(By. TAG NAME, “input”). However, because there are numerous inputs on the page, this might be an issue. We notice that the page contains an ID by viewing it, so we update the selector: driver.find element (By.ID, “twotabsearchtextbox”).&lt;/p&gt;

&lt;p&gt;IDs are probably not changed frequently, and they are a more secure technique of retrieving information than classes. The issue is generally that they are not found. Assuming there is no ID, we may choose the search form and then the input inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;selenium&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;  
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;selenium.webdriver.common.by&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;By&lt;/span&gt;  

&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://www.amazon.com/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="nb"&gt;input&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="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CSS_SELECTOR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;form[role=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;search&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;] input[type=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no one-size-fits-all solution; each alternative is suited for a certain set of circumstances. You must select the one that best meets your requirements.&lt;/p&gt;

&lt;p&gt;As we browse down the page, we’ll see a plethora of items and categories. And a common class that appears frequently: a-list-item. A similar method (find elements in plural) is required to match all of the things rather than simply the first occurrence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#...  
&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="n"&gt;items&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="nf"&gt;find_elements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CLASS_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a-list-item&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Frtjfqme3pfu16f77f2ah.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%2Frtjfqme3pfu16f77f2ah.png" alt="chrome dev tool" width="800" height="595"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we must do something with the elements we have chosen.&lt;br&gt;&lt;br&gt;
Getting to Know the Elements&lt;/p&gt;

&lt;p&gt;We’ll do a search using the above-mentioned input. To do this, we will use the send keys function, which will type and press enter to send the form. We might alternatively type into the input, then locate and click on the submit button (element.click()). It is simpler in this scenario because the Enter key works properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;selenium.webdriver.common.keys&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Keys&lt;/span&gt;  
&lt;span class="c1"&gt;#...  
&lt;/span&gt;&lt;span class="nb"&gt;input&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="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CSS_SELECTOR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;form[role=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;search&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;] input[type=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;]&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Python Books&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ENTER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take note that the script does not wait and exits as soon as the search is completed. The natural next step is to do something, so we’ll list the results using find elements as shown previously. The s-result-item class may be used to inspect the result.&lt;/p&gt;

&lt;p&gt;These are divs containing numerous inner tags that we will only choose. If we’re interested, we could take the link’s href values and visit each item; however, we won’t do so for the time being. However, because the h2 tags include the title of the book, we must choose a title for each element. We can keep using the found element because it works for both the driver and any web element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ...  
&lt;/span&gt;&lt;span class="n"&gt;items&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="nf"&gt;find_elements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CLASS_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;s-result-item&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
&lt;span class="n"&gt;h2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TAG_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;h2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Prints a list of around fifty items  
&lt;/span&gt;
&lt;span class="c1"&gt;# Learning Python, 5th Edition ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t depend too heavily on this method because some tags may be empty or lack a title. We should properly design error control for a real-world use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Element Screenshots and Screenshots
&lt;/h2&gt;

&lt;p&gt;Screenshots are a useful tool for both testing and storing changes. We can capture a snapshot of the current browser context or a specific element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ...  
&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_screenshot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;page.png&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fdhn5gbgmtndx3cz25y2f.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%2Fdhn5gbgmtndx3cz25y2f.png" alt="amazon homepage" width="800" height="765"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ...  
&lt;/span&gt;&lt;span class="n"&gt;card&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="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CLASS_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a-cardui&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;screenshot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amazon_card.png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fm6cj5uscwzokt1ykdxhz.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%2Fm6cj5uscwzokt1ykdxhz.png" alt="card" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Run Javascript
&lt;/h2&gt;

&lt;p&gt;The final Selenium feature we’d like to highlight is Javascript execution. Some tasks are simpler to complete directly in the browser, or we want to ensure that everything is performed properly. We may use execute a script to pass the JS code to be run. It can be used with or without parameters.&lt;/p&gt;

&lt;p&gt;In the examples below, we can see both scenarios. There is no need for params to obtain the User-Agent as seen by the browser. That may be useful for ensuring that the one sent is being updated appropriately in the navigator object, as some security checks may raise red flags otherwise. The second will accept an h2 as an input and use getClientRects to return its left location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;webdriver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="n"&gt;agent&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="nf"&gt;execute_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;return navigator.userAgent&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Mozilla/5.0 ... Chrome/96 ...  
&lt;/span&gt;
&lt;span class="n"&gt;header&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="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CSS_SELECTOR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;h2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="n"&gt;headerText&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="nf"&gt;execute_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;return arguments[0].getClientRects()[0].left&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headerText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 242.5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Selenium is a powerful tool with several uses, but you must use it in your unique way. Use each feature to your advantage. And, many times, there are several paths to the same destination; choose the one that will benefit you the most — or is the simplest.&lt;/p&gt;

&lt;p&gt;Once you’ve gotten the hang of it, you’ll want to expand your scrape and obtain additional pages. Other obstacles may occur here, such as crawling at scale and blocks. Some pointers are provided above to assist you: Examine the header and proxy sections. However, keep in mind that crawling at scale is a difficult task. Please do not claim that we did not warn you.&lt;/p&gt;

&lt;p&gt;I hope you’ve gained a better knowledge of how Selenium works in Python (it goes the same for other languages). Don't forget to share this article on social media to help me to write more articles like this in future. Thank you for reading.&lt;/p&gt;

</description>
      <category>python</category>
      <category>webscrapping</category>
      <category>python3</category>
      <category>programming</category>
    </item>
    <item>
      <title>Go Vs Rust: Navigating The Programming Landscape</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Fri, 16 Feb 2024 18:30:00 +0000</pubDate>
      <link>https://dev.to/harendra21/go-vs-rust-navigating-the-programming-landscape-22b1</link>
      <guid>https://dev.to/harendra21/go-vs-rust-navigating-the-programming-landscape-22b1</guid>
      <description>&lt;p&gt;When building software today, developers are spoiled for choice with programming languages. Two that stand out are Go and Rust - both powerful but quite different. This article compares these languages across various factors to help you determine which is better suited for your needs.&lt;/p&gt;

&lt;p&gt;We weigh their approaches to concurrency, safety, speed, interoperability and more. We look at where each excels - &lt;a href="https://golang.withcodeexample.com/blog/golang-tutorial-for-beginners/" rel="noopener noreferrer"&gt;Go&lt;/a&gt; for &lt;a href="https://aws.amazon.com/what-is/cloud-native/#:~:text=Cloud%20native%20is%20the%20software,quickly%20to%20meet%20customer%20demands." rel="noopener noreferrer"&gt;cloud-native&lt;/a&gt; development and Rust for systems programming. Their vibrant open-source communities are assessed as well. By evaluating Go and Rust side-by-side, we aim to provide the information you need to select the optimal language for your next project. Whether you prioritize productivity, performance or robustness, you’re sure to find a language that fits. Read on to discover the key differences between these two compiler powerhouses.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go Vs Rust
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Golang&lt;/th&gt;
&lt;th&gt;Rust&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Typing&lt;/td&gt;
&lt;td&gt;Static typing&lt;/td&gt;
&lt;td&gt;Static typing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory safety&lt;/td&gt;
&lt;td&gt;Garbage collected&lt;/td&gt;
&lt;td&gt;Ownership based&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Fast compile times, optimized executables&lt;/td&gt;
&lt;td&gt;Equally fast and efficient&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Concurrency&lt;/td&gt;
&lt;td&gt;Goroutines, channels&lt;/td&gt;
&lt;td&gt;Fearless concurrency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interoperability&lt;/td&gt;
&lt;td&gt;C bindings&lt;/td&gt;
&lt;td&gt;C bindings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Portability&lt;/td&gt;
&lt;td&gt;Compiles to standalone binary&lt;/td&gt;
&lt;td&gt;Compiles to standalone binary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Package manager&lt;/td&gt;
&lt;td&gt;Modules&lt;/td&gt;
&lt;td&gt;Cargo&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Licensing&lt;/td&gt;
&lt;td&gt;BSD style&lt;/td&gt;
&lt;td&gt;MIT/Apache 2.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Learning curve&lt;/td&gt;
&lt;td&gt;Easy, simple syntax&lt;/td&gt;
&lt;td&gt;Moderate, some unique concepts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maturity&lt;/td&gt;
&lt;td&gt;Mature, production-ready&lt;/td&gt;
&lt;td&gt;Young, rapidly evolving&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code style&lt;/td&gt;
&lt;td&gt;Formatted by gofmt&lt;/td&gt;
&lt;td&gt;rustfmt for formatting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;error handling&lt;/td&gt;
&lt;td&gt;Multiple return values&lt;/td&gt;
&lt;td&gt;enum Result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Metaprogramming&lt;/td&gt;
&lt;td&gt;Limited to generating code&lt;/td&gt;
&lt;td&gt;Macros allow metaprogramming&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Community&lt;/td&gt;
&lt;td&gt;Large and rapidly growing&lt;/td&gt;
&lt;td&gt;Small but active and friendly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Industry adoption&lt;/td&gt;
&lt;td&gt;High, especially for cloud, web and DevOps&lt;/td&gt;
&lt;td&gt;Growing, but smaller user base than Go&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tooling&lt;/td&gt;
&lt;td&gt;Full-featured CLI, cover, pprof&lt;/td&gt;
&lt;td&gt;Cargo, rustup, rustfmt, clippy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Go vs Rust requests
&lt;/h2&gt;

&lt;p&gt;&lt;a href="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccefb87493b552b827/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" class="article-body-image-wrapper"&gt;&lt;img src="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccefb87493b552b827/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" alt="go vs rust requests"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Philosophies and Mindsets of Go and Rust
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Go: Simplifying Complexity
&lt;/h3&gt;

&lt;p&gt;Go, affectionately known as Golang, boasts a philosophy grounded in simplicity and efficiency. Developed by Google engineers, it was designed to address the challenges of modern software engineering while prioritizing readability and maintainability. The language’s approachable syntax and minimalistic design make it an attractive choice for developers seeking to build scalable, concurrent, and reliable systems. Go places a strong emphasis on creating and running software at scale, making it particularly well-suited for cloud-native applications and large-scale distributed systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rust: Empowering Developers with Safety and Performance
&lt;/h3&gt;

&lt;p&gt;Contrary to Go’s focus on simplicity, Rust prioritizes safety and performance without compromising on productivity. Born out of Mozilla’s Research division, Rust was conceived as a systems programming language that could eliminate entire classes of bugs, particularly memory safety violations and data races. Rust achieves this feat through its sophisticated ownership model, which ensures that memory is managed safely and efficiently at compile time, without the need for a garbage collector. With its powerful abstractions and fearless concurrency, Rust empowers developers to write fast, safe, and concurrent code, making it an ideal choice for building performance-critical applications, system software, and embedded systems.&lt;/p&gt;

&lt;p&gt;&lt;a href="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccec6fdcec9548cc70/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" class="article-body-image-wrapper"&gt;&lt;img src="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccec6fdcec9548cc70/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" alt="golang"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Features of Go and Rust
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Memory Management: A Tale of Automation vs Control
&lt;/h3&gt;

&lt;p&gt;In the realm of &lt;a href="https://golang.withcodeexample.com/blog/memory-management-golang-safeguarding-efficiency-stability/" rel="noopener noreferrer"&gt;memory management&lt;/a&gt;, Go and Rust diverges significantly in their approaches. Go opts for a garbage-collected runtime, where memory allocation and deallocation are handled automatically by the runtime environment. This approach simplifies memory management for developers, eliminating the need for manual memory management and reducing the risk of memory leaks and dangling pointers. However, the trade-off is a potential performance overhead, as the garbage collector periodically pauses the program to reclaim unused memory.&lt;/p&gt;

&lt;p&gt;On the other hand, Rust takes a more hands-on approach to memory management, eschewing a garbage collector in favour of manual memory management with compile-time checks. The language introduces ownership and borrowing concepts, which enforce strict rules at compile time to ensure memory safety and prevent data races. While this approach offers unparalleled performance and control over memory usage, it comes with a steeper learning curve and requires developers to be more mindful of memory management issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrency and Multi-threading: Lightweight vs System Threads
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://golang.withcodeexample.com/blog/power-of-concurrency-in-go/" rel="noopener noreferrer"&gt;Concurrency&lt;/a&gt; and multi-threading are fundamental aspects of modern software development, and both Go and Rust offer robust solutions for concurrent programming. In Go, concurrency is achieved through lightweight threads called goroutines, which are managed by the Go runtime. Goroutines enable concurrent execution of functions with minimal overhead, making it easy to write highly concurrent programs without worrying about thread management or synchronization primitives.&lt;/p&gt;

&lt;p&gt;In contrast, Rust leverages the power of system threads for concurrency, providing low-level control over thread creation, synchronization, and communication. The language’s ownership model ensures thread safety at compile time, eliminating the risk of data races and deadlocks. While Rust’s approach to concurrency may require more explicit handling of threads and synchronization primitives, it offers unparalleled control and performance for highly concurrent applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance: Balancing Efficiency and Abstractions
&lt;/h3&gt;

&lt;p&gt;Performance is a critical consideration for many developers, especially those working on high-performance applications or systems software. Both Go and Rust offer compelling performance characteristics, albeit through different means. In Go, the language’s minimalistic design and runtime optimizations result in a small memory footprint and efficient garbage collection, making it well-suited for modern processors and cloud-native environments. Additionally, Go’s concurrency model enables efficient utilization of CPU cores, further enhancing its performance for parallel workloads.&lt;/p&gt;

&lt;p&gt;Rust, on the other hand, takes performance to the next level with its zero-cost abstractions and fine-grained control over memory layout and execution. The language’s emphasis on compile-time safety checks and optimization opportunities ensures that Rust code can rival the performance of handcrafted C or C++ code, making it an excellent choice for performance-critical applications and systems programming tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccecc83d335f34289e/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" class="article-body-image-wrapper"&gt;&lt;img src="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccecc83d335f34289e/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" alt="rust"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Developer Opinions and Job Market
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Stack Overflow Developer Survey: Insights into Developer Preferences
&lt;/h3&gt;

&lt;p&gt;To gauge developer sentiment and preferences, we turn to the annual Stack Overflow Developer Survey, which provides valuable insights into the programming languages, tools, and technologies favoured by developers worldwide. According to the latest survey data, both Go and Rust enjoy a healthy level of popularity among developers, with Go often ranking among the top 10 most loved and wanted languages, thanks to its simplicity, performance, and strong ecosystem of libraries and tools.&lt;/p&gt;

&lt;p&gt;However, when it comes to satisfaction and interest in using the language again, Rust developers tend to express higher levels of satisfaction and enthusiasm compared to their Go counterparts. This sentiment is reflected in the survey data, which shows that a higher percentage of Rust developers express a desire to continue using Rust in the future, highlighting the language’s strong community support and favourable developer experience.&lt;/p&gt;

&lt;p&gt;&lt;a href="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccee4326ec8be9a598/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" class="article-body-image-wrapper"&gt;&lt;img src="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccee4326ec8be9a598/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" alt="golang stackoverflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccee434d7663fda6e1/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" class="article-body-image-wrapper"&gt;&lt;img src="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccee434d7663fda6e1/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" alt="rust stacksoverflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://survey.stackoverflow.co/2023/#section-admired-and-desired-programming-scripting-and-markup-languages" rel="noopener noreferrer"&gt;Source&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Salary: A Measure of Demand and Value
&lt;/h3&gt;

&lt;p&gt;In addition to developer sentiment, salary data provides valuable insights into the demand for specific programming languages and their perceived value in the job market. According to industry reports and salary surveys, both Go and Rust developers command competitive salaries globally, reflecting the high demand for skilled practitioners in both languages. Whether you’re a seasoned Go developer or a Rust enthusiast, you can expect to find ample opportunities and competitive compensation in today’s job market.&lt;/p&gt;

&lt;h3&gt;
  
  
  Average salary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The average golang developer salary in the USA is &lt;strong&gt;$135,000&lt;/strong&gt; per year or &lt;strong&gt;$64.90&lt;/strong&gt; per hour.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The average Rust developer salary in the USA is &lt;strong&gt;$109,905&lt;/strong&gt; per year, or &lt;strong&gt;$52.84&lt;/strong&gt; per hour.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recommendation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Consider Your Learning Style and Goals
&lt;/h3&gt;

&lt;p&gt;&lt;a href="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccec47a85e780fdaeb/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" class="article-body-image-wrapper"&gt;&lt;img src="//appwrite.withcodeexample.com/v1/storage/buckets/65cb3fcd6bbe0f7e04d4/files/65ccec47a85e780fdaeb/preview?width=1280&amp;amp;height=0&amp;amp;gravity=center&amp;amp;quality=90&amp;amp;project=65ca51d2711f0f5e1fa8" alt="choose"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you weigh the merits of Go and Rust, it’s essential to consider your learning style, career aspirations, and long-term goals. If you’re new to programming or prefer a language that’s easy to learn and approachable, Go may be the ideal choice. Its simplicity, readability, and a robust standard library make it an excellent starting point for beginners and experienced developers alike. Additionally, Go’s focus on concurrency and scalability makes it well-suited for building cloud-native applications, microservices, and distributed systems.&lt;/p&gt;

&lt;p&gt;On the other hand, if you’re seeking a challenge and are passionate about systems programming, performance optimization, and low-level control, Rust may be the perfect fit. While Rust has a steeper learning curve compared to Go, its emphasis on safety, performance, and concurrency makes it a compelling choice for building high-performance applications, system software, and embedded systems. Additionally, Rust’s strong type system and expressive syntax enable developers to write safe and efficient code with confidence, making it an excellent choice for projects where reliability and performance are paramount.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources for Learning: Where to Begin Your Journey
&lt;/h3&gt;

&lt;p&gt;Regardless of which language you choose, embarking on your journey with Go or Rust is an exciting opportunity to expand your skill set, tackle new challenges, and contribute to the vibrant communities surrounding these languages. To help you get started, we’ve compiled a list of resources for learning Go and Rust, ranging from interactive tutorials to project-based learning platforms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;exercism.io&lt;/strong&gt; : An interactive platform for practising Go and Rust through hands-on coding exercises and mentorship.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;codecraftrs.com&lt;/strong&gt; : A project-based learning platform that offers tutorials, screencasts, and real-world projects for Go and Rust enthusiasts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Official Documentation&lt;/strong&gt; : Explore the official documentation and guides provided by the Go and Rust communities for comprehensive insights&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;In the ever-evolving landscape of programming languages, the choice between Go and Rust represents a significant decision for developers seeking to expand their skill set and tackle new challenges. Both languages offer compelling features, strong communities, and diverse applications, making them valuable assets in any developer’s toolkit.&lt;/p&gt;

&lt;p&gt;As we’ve explored in this article, Go embodies simplicity, efficiency, and scalability, making it an excellent choice for building cloud-native applications, microservices, and distributed systems. Its approachable syntax, built-in concurrency, and a robust standard library make it an ideal starting point for beginners and experienced developers alike.&lt;/p&gt;

&lt;p&gt;On the other hand, Rust champions safety, performance, and low-level control offering developers the power to build fast, reliable, and secure systems. With its ownership model, fearless concurrency, and zero-cost abstractions, Rust empowers developers to tackle performance-critical applications, system software, and embedded systems with confidence and precision.&lt;/p&gt;

&lt;p&gt;Ultimately, the decision between Go and Rust comes down to your learning style, goals, and aspirations. Whether you’re drawn to the simplicity of Go or the challenge of Rust, both languages offer rich learning experiences, vibrant communities, and exciting opportunities for growth and exploration.&lt;/p&gt;

&lt;p&gt;We encourage you to explore both languages, experiment with different projects and tutorials, and engage with the vibrant communities surrounding Go and Rust. By embracing the strengths of each language and adapting them to your unique needs and preferences, you’ll embark on a rewarding journey of discovery, innovation, and mastery in the world of programming.&lt;/p&gt;

</description>
      <category>go</category>
      <category>rust</category>
    </item>
    <item>
      <title>The Golang Playground: Enhance Your Skills with Ease</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Wed, 24 Jan 2024 00:00:00 +0000</pubDate>
      <link>https://dev.to/harendra21/the-golang-playground-enhance-your-skills-with-ease-4664</link>
      <guid>https://dev.to/harendra21/the-golang-playground-enhance-your-skills-with-ease-4664</guid>
      <description>&lt;p&gt;The &lt;a href="https://go.dev/play/" rel="noopener noreferrer"&gt;Golang Playground&lt;/a&gt; is an online tool that allows to experiment, practice and enhance their coding skills in a convenient and user environment. Whether you are a beginner or a developer, Golang provides a hassle-free to write, compile and execute Go code without the need for any local or installation. In the blog post, we explore the various features benefits, and tips to the most out of Golang Playground.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Golang Playground its Purpose
&lt;/h3&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%2Fj845gzf74ypa25q5lycq.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%2Fj845gzf74ypa25q5lycq.jpg" alt="https://res.cloudinary.com/harendra21/image/upload/v1704893484/golangwithexample/how-does-go-playground-work-v0-SwUH3SyGWEl9JIsp9__Su_3uFJPZbzJg-mM0DgbteMY_n7yrot.jpg" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Golang Playground is a platform specifically designed for Go or Golang programming language enthusiasts. provides a sandbox where developers can write run, and share code without the need for a local development setup. main purpose of the Golang Playground is to facilitate, prototyping, and collaboration within the Go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features and Benefits of the Golang Playground
&lt;/h3&gt;

&lt;p&gt;The Golang Playground comes packed with a range of features that make it excellent for programmers of all levels. Some key features include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;**Online Accessibility The Golang Playground is accessible through any web browser, making it easy to write test code from anywhere, at any time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Execution and Output:&lt;/strong&gt; platform instantly compiles and executes the Go code, providing real-time output, errors and execution results.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Syntax Highlighting and Autocompletion:&lt;/strong&gt; Theolang Playground offers syntax highlighting and autocompletion, making the coding experience more-friendly and efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shareable Code Snippets:&lt;/strong&gt; It allows users to their code snippets with others by simply clicking on the “Share” button, generating unique URL that can be shared and accessed by anyone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version Control:&lt;/strong&gt; The Golang Playground seamlessly integrates with version control systems like Git, allowing developers clone, commit, and collaborate on code repositories.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to Access and Navigate Golang Playground
&lt;/h3&gt;

&lt;p&gt;Accessing the Golang Playground is incredibly simple. Just the official website at &lt;a href="https://play.golang.org" rel="noopener noreferrer"&gt;https://play.golang.org&lt;/a&gt;, and you’ll be greeted a clean and intuitive user interface. The main screen consists of sections: the code editor, the output window, and the console.&lt;/p&gt;

&lt;p&gt;To coding, simply enter your Go code in the editor section. Once you’re ready, click the “Run” button, and the code will be compiled and executed. The output, including any errors or results, will be displayed in the output window.&lt;/p&gt;

&lt;p&gt;Navigating through the Golang Playground is straightforward. The menu at the top of the page contains options to create a new playground, save your code, and share it with others. Additionally, you can access helpful resources, such as documentation and examples, by clicking on the “Resources” tab. Now that we’ve covered the basics of the Golang Playground, let’s dive into getting started with Go itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with &lt;a href="https://golang.withcodeexample.com/series/golang-best-practices/" rel="noopener noreferrer"&gt;Golang&lt;/a&gt;
&lt;/h2&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%2F6ipbk9lwi3hvgv592hsn.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%2F6ipbk9lwi3hvgv592hsn.png" alt="https://res.cloudinary.com/harendra21/image/upload/v1704893574/golangwithexample/0_KvlaCAJFzT86-D7J_jwbfs3.png" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview of Go: A Beginner’s Guide
&lt;/h3&gt;

&lt;p&gt;Go, also known as Golang, is a popular open-source programming language developed by Google. It was created with a focus on simplicity, efficiency, and scalability, making it an excellent choice for building networked and concurrent applications.&lt;/p&gt;

&lt;p&gt;If you’re new to Go, the Golang Playground is a fantastic place to start. It provides a beginner-friendly environment to experiment with the language’s syntax, concepts, and features. As you progress, you can gradually move on to more complex topics and real-world projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exploring the Basic Syntax of Go
&lt;/h3&gt;

&lt;p&gt;Before diving into writing code in the Golang Playground, it’s essential to understand the basic syntax of Go. Go follows a clean and concise syntax, which makes it easy to read and write code.&lt;/p&gt;

&lt;p&gt;Here are a few key aspects of the Go syntax:&lt;/p&gt;

&lt;h4&gt;
  
  
  Variables and Constants:
&lt;/h4&gt;

&lt;p&gt;In Go, variables are declared using the &lt;code&gt;var&lt;/code&gt; keyword, followed by the variable name and its type. Constants, on the other hand, are declared using the &lt;code&gt;const&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var message string = "Hello, World!"
const pi = 3.14159

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Control Structures: Loops and Conditionals:
&lt;/h4&gt;

&lt;p&gt;Go provides several control structures, including &lt;code&gt;if&lt;/code&gt; statements, &lt;code&gt;for&lt;/code&gt; loops, and &lt;code&gt;switch&lt;/code&gt; statements. These constructs allow you to control the flow of execution based on certain conditions.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if condition {
 // code block executed if the condition is true
}

for i := 0; i &amp;lt; 5; i++ {
 // code block executed repeatedly until the condition is false
}

switch variable {
case 1:
 // code executed if the variable is 1
case 2:
 // code executed if the variable is 2
default:
 // code executed if the variable doesn't match any case
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Creating Functions and Packages:
&lt;/h4&gt;

&lt;p&gt;Functions in Go allow you to group related code and execute it independently. You can define functions using the &lt;code&gt;func&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Go also has a concept of packages, which are collections of related functions and types. They enable code modularity and reusability.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func add(a, b int) int {
 return a + b
}

package main

import "fmt"

func main() {
 fmt.Println(add(2, 3))
}

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

&lt;/div&gt;



&lt;p&gt;Now that we have a basic understanding of Go’s syntax, let’s move on to actually writing our first program in the Golang Playground.&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing Your First Program in the Golang Playground
&lt;/h3&gt;

&lt;p&gt;The Golang Playground provides an ideal environment to get started with Go programming. Let’s explore the process of writing a simple program step by step.&lt;/p&gt;

&lt;h4&gt;
  
  
  Declaring Variables and Constants
&lt;/h4&gt;

&lt;p&gt;To declare variables or constants in the Golang Playground, follow the syntax we discussed earlier. For instance:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
 var name string = "John Doe"
 const age = 28
 fmt.Println("Name:", name)
 fmt.Println("Age:", age)
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using Control Structures: Loops and Conditionals
&lt;/h4&gt;

&lt;p&gt;The Golang Playground allows you to experiment with different control structures to create interactive and dynamic programs. Here’s an example that demonstrates the use of an &lt;code&gt;if&lt;/code&gt; statement and a &lt;code&gt;for&lt;/code&gt; loop:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
 num := 10

 if num &amp;gt; 0 {
 fmt.Println("Number is positive")
 } else {
 fmt.Println("Number is negative")
 }

 for i := 1; i &amp;lt;= num; i++ {
 fmt.Println(i)
 }
}

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Creating Functions and Packages
&lt;/h4&gt;

&lt;p&gt;You can define functions and packages within your code in the Golang Playground. Here’s an example:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func add(a, b int) int {
 return a + b
}

func main() {
 result := add(3, 4)
 fmt.Println("Sum:", result)
}

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

&lt;/div&gt;



&lt;p&gt;By following these examples, you can experiment with various concepts and gradually build your coding skills in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Features of the Golang Playground
&lt;/h2&gt;

&lt;p&gt;The Golang Playground not only supports basic features but also offers advanced functionalities that can greatly enhance your coding experience. Let’s explore some of these features in detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exploring Go Packages and Imports
&lt;/h3&gt;

&lt;p&gt;Go promotes code modularity and reusability through the concept of packages. With packages, you can organize your code into logical units and import them as needed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Understanding the Package Structure in Go
&lt;/h4&gt;

&lt;p&gt;In Go, packages are the building blocks of code organization. A package can consist of multiple Go source files that are grouped together. It allows you to divide your program’s functionality into separate, manageable units.&lt;/p&gt;

&lt;h4&gt;
  
  
  Importing Packages in Your Golang Playground Code
&lt;/h4&gt;

&lt;p&gt;The Golang Playground allows you to import existing packages within your code. You can use the standard library packages, as well as third-party packages, to enhance your program’s capabilities and leverage existing code.&lt;/p&gt;

&lt;p&gt;To import a package, use the &lt;code&gt;import&lt;/code&gt; keyword followed by the package’s import path.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

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

import (
 "fmt"
 "math/rand"
)

func main() {
 fmt.Println("Random number:", rand.Intn(100))
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Utilizing Go’s Built-in Libraries and Functions
&lt;/h3&gt;

&lt;p&gt;Go provides a rich standard library that offers a wide range of built-in packages and functions. These packages cover diverse areas such as string manipulation, file handling, networking, and more.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exploring Common Built-in Packages
&lt;/h4&gt;

&lt;p&gt;Some commonly used built-in packages include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fmt&lt;/code&gt;: Provides functions for formatted I/O, such as printing to the console.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os&lt;/code&gt;: Offers functionality for performing operating system-related tasks, such as file handling and environment variables.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;net/http&lt;/code&gt;: Enables building web servers and making HTTP requests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;time&lt;/code&gt;: Deals with time-related operations, such as measuring durations and formatting time strings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By familiarizing yourself with these packages, you can leverage their power to simplify and optimize your code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Leveraging Go Standard Library Functions
&lt;/h4&gt;

&lt;p&gt;In addition to packages, Go’s standard library also offers a wide range of functions that can be useful for various programming tasks. These functions cover areas such as string manipulation, mathematical calculations, and error handling.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

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

import (
 "fmt"
 "strings"
)

func main() {
 message := "Hello, World!"
 fmt.Println("Length:", len(message))
 fmt.Println("Uppercase:", strings.ToUpper(message))
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Managing Errors and Exception Handling in Go
&lt;/h3&gt;

&lt;p&gt;Error handling is a critical aspect of writing robust and reliable code. Go provides a straightforward yet powerful error handling mechanism.&lt;/p&gt;

&lt;h4&gt;
  
  
  Understanding Error Handling Principles in Go
&lt;/h4&gt;

&lt;p&gt;In Go, errors are represented as values of the &lt;code&gt;error&lt;/code&gt; type. Functions that can potentially fail return an error value, which can be checked and handled accordingly.&lt;/p&gt;

&lt;p&gt;Go encourages explicit error handling instead of relying on exceptions. This helps maintain code clarity and prevents potential bugs from being silently ignored.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing Error Handling Strategies in Your Code
&lt;/h4&gt;

&lt;p&gt;To handle errors in Go, you can use the &lt;code&gt;if&lt;/code&gt; statement in combination with the &lt;code&gt;err&lt;/code&gt; variable convention. This convention checks if the error is &lt;code&gt;nil&lt;/code&gt; (indicating success) or non-&lt;code&gt;nil&lt;/code&gt; (indicating an error occurred).&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

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

import (
 "fmt"
 "os"
)

func main() {
 file, err := os.Open("data.txt")
 if err != nil {
 fmt.Println("Error:", err)
 return
 }

 // Perform operations on the opened file
 _ = file.Close()
}

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

&lt;/div&gt;



&lt;p&gt;By implementing proper error handling strategies, you can ensure that your code gracefully handles unexpected situations and failures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enhancing Your Coding Skills with Golang Playground
&lt;/h2&gt;

&lt;p&gt;The Golang Playground offers several features and tools that can help you enhance your coding skills and efficiency. Let’s explore some of these features in more detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing and Debugging in the Golang Playground
&lt;/h3&gt;

&lt;p&gt;One of the key aspects of becoming a proficient programmer is writing thorough and reliable tests. The Golang Playground provides robust testing capabilities that allow you to validate your code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Writing Test Cases for Your Go Code
&lt;/h4&gt;

&lt;p&gt;In Go, tests are typically written in a separate file ending with &lt;code&gt;_test.go&lt;/code&gt;. These files contain test functions that use the &lt;code&gt;testing&lt;/code&gt; package and its assertion methods to check the correctness of the code.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

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

import (
 "testing"
)

func add(a, b int) int {
 return a + b
}

func TestAdd(t *testing.T) {
 result := add(3, 4)
 if result != 7 {
 t.Errorf("Expected 7, got %d", result)
 }
}

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

&lt;/div&gt;



&lt;p&gt;The Golang Playground allows you to write and run tests directly within the interface, helping you validate your code and uncover any issues.&lt;/p&gt;

&lt;h4&gt;
  
  
  Debugging Techniques and Best Practices
&lt;/h4&gt;

&lt;p&gt;Debugging is an essential skill for any programmer. While the Golang Playground’s main focus is on writing and running code, it does offer some debugging capabilities.&lt;/p&gt;

&lt;p&gt;For example, you can use the &lt;code&gt;fmt.Println()&lt;/code&gt; function to print the values of variables at various points in your program to understand how it’s executing.&lt;/p&gt;

&lt;p&gt;Additionally, using well-placed &lt;code&gt;panic&lt;/code&gt; statements can help identify specific sections of code that might be causing issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exploring Golang Playground’s Collaboration Features
&lt;/h3&gt;

&lt;p&gt;The Golang Playground also allows developers to collaborate and share their code with others. Here are a few features that facilitate collaboration:&lt;/p&gt;

&lt;h4&gt;
  
  
  Sharing Your Code and Collaborating with Others
&lt;/h4&gt;

&lt;p&gt;With the click of a button, you can generate a unique URL that allows others to view and interact with your code. This makes it incredibly easy to share your code and gather feedback from others.&lt;/p&gt;

&lt;h4&gt;
  
  
  Utilizing Version Control in the Golang Playground
&lt;/h4&gt;

&lt;p&gt;The Golang Playground seamlessly integrates with version control systems like Git. You can clone repositories, commit changes, and collaborate with others directly from the interface.&lt;/p&gt;

&lt;p&gt;By leveraging the collaboration features of the Golang Playground, you can broaden your network, learn from others, and get valuable insights on your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimizing Performance with Go’s Concurrency Tools
&lt;/h3&gt;

&lt;p&gt;Go is renowned for its excellent support for concurrency. The Golang Playground allows you to explore Go’s powerful concurrency tools and optimize the performance of your code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Understanding Concurrency in Go
&lt;/h4&gt;

&lt;p&gt;Concurrency is the ability to execute multiple tasks simultaneously. Go makes it easy to write concurrent programs by providing lightweight goroutines and channels.&lt;/p&gt;

&lt;p&gt;Goroutines are lightweight threads that allow functions to run concurrently. Channels provide a safe way for goroutines to communicate and synchronize their execution.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing Goroutines and Channels for Parallel Execution
&lt;/h4&gt;

&lt;p&gt;Here’s a simple example that uses goroutines and channels to find the sum of squares concurrently:&lt;br&gt;
&lt;/p&gt;

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

import (
 "fmt"
)

func calculateSquares(start, end int, result chan int) {
 sum := 0
 for i := start; i &amp;lt;= end; i++ {
 sum += i * i
 }
 result &amp;lt;- sum
}

func main() {
 result := make(chan int)

 go calculateSquares(1, 500, result)
 go calculateSquares(501, 1000, result)

 totalSum := &amp;lt;-result + &amp;lt;-result
 fmt.Println("Total Sum:", totalSum)
}

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

&lt;/div&gt;



&lt;p&gt;By leveraging the power of goroutines and channels, you can effectively utilize your system resources and improve the performance of your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary and Conclusion
&lt;/h2&gt;

&lt;p&gt;The Golang Playground is a valuable resource for programmers looking to enhance their coding skills in the Go programming language. In this blog post, we explored the various features and benefits of the Golang Playground, including its user-friendly interface, real-time code execution, and collaboration capabilities.&lt;/p&gt;

&lt;p&gt;We also delved into the fundamentals of Go, including its basic syntax and key concepts such as variables, control structures, functions, and packages. The Golang Playground provides an ideal environment to experiment with and learn these concepts.&lt;/p&gt;

&lt;p&gt;Furthermore, we discussed the advanced features of the Golang Playground, such as package imports, utilization of built-in libraries and functions, error handling strategies, and tools for testing, debugging, collaboration, and performance optimization.&lt;/p&gt;

&lt;p&gt;In conclusion, the Golang Playground offers programmers a convenient and accessible platform to improve their coding skills. Whether you’re a beginner or an experienced developer, the Golang Playground is an invaluable tool that can help you unleash your potential and master the art of Go programming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read More&lt;/strong&gt; - &lt;a href="https://golang.withcodeexample.com/blog/golang-tutorial-for-beginners/" rel="noopener noreferrer"&gt;Golang Tutorial for Beginners&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>Golang Switch Case</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Tue, 16 Jan 2024 00:00:00 +0000</pubDate>
      <link>https://dev.to/harendra21/golang-switch-case-4gca</link>
      <guid>https://dev.to/harendra21/golang-switch-case-4gca</guid>
      <description>&lt;p&gt;Discover the incredible power of the &lt;code&gt;switch&lt;/code&gt; statement in &lt;a href="https://golang.withcodeexample.com/" rel="noopener noreferrer"&gt;Golang&lt;/a&gt; – a game-changer in decision-making for your code. Unlike the sometimes clunky &lt;code&gt;if-else&lt;/code&gt; statements, Go’s &lt;code&gt;switch&lt;/code&gt; offers a cleaner and more straightforward way to handle conditions.&lt;/p&gt;

&lt;p&gt;In this exploration of Golang switch case, we’ll break down the &lt;code&gt;switch&lt;/code&gt; statement, using real-world examples to illustrate its efficiency. Say farewell to the complexities of &lt;code&gt;if-else&lt;/code&gt; blocks and welcome a more concise syntax that turns your code into a masterpiece.&lt;/p&gt;

&lt;p&gt;This journey isn’t just about making decisions – it’s about making them with finesse. Learn how the &lt;code&gt;switch&lt;/code&gt; statement becomes your ally, providing an elegant solution for handling diverse scenarios in your code. It’s not just about control flow; it’s about enhancing your coding experience.&lt;/p&gt;

&lt;p&gt;Join us as we navigate the world of Go, unveiling the secrets of the &lt;code&gt;switch&lt;/code&gt; statement and empowering you to harness its capabilities. Your coding landscape is on the brink of transformation – are you ready to revolutionize your approach with the unparalleled simplicity and effectiveness of Go’s &lt;code&gt;switch&lt;/code&gt; statement? Dive in and let your code do the talking.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://golang.withcodeexample.com/blog/golang-tutorial-for-beginners/" rel="noopener noreferrer"&gt;Basic Golang&lt;/a&gt; Switch Case Statement
&lt;/h3&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%2F6fd54t8wjfhhwnmav3oo.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%2F6fd54t8wjfhhwnmav3oo.png" alt="Basic Switch Statement" width="391" height="588"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The basic syntax of the &lt;code&gt;switch&lt;/code&gt; statement looks like this:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
 // Example 1
 day := "Monday"

 switch day {
 case "Monday":
 fmt.Println("It's Monday!")
 case "Tuesday":
 fmt.Println("It's Tuesday!")
 case "Wednesday":
 fmt.Println("It's Wednesday!")
 default:
 fmt.Println("It's some other day.")
 }

 // Example 2
 number := 42

 switch {
 case number &amp;lt; 0:
 fmt.Println("Negative number")
 case number == 0:
 fmt.Println("Zero")
 case number &amp;gt; 0:
 fmt.Println("Positive number")
 }
}

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

&lt;/div&gt;



&lt;p&gt;In the first example of golang switch case, the &lt;code&gt;switch&lt;/code&gt; statement is used to check the value of the &lt;code&gt;day&lt;/code&gt; variable. If &lt;code&gt;day&lt;/code&gt; matches any of the cases, the corresponding block of code is executed. If none of the cases match, the code inside the &lt;code&gt;default&lt;/code&gt; block is executed.&lt;/p&gt;

&lt;p&gt;In the second example, the &lt;code&gt;switch&lt;/code&gt; statement is used without an expression. Instead, it checks conditions within each case. The first case that evaluates to &lt;code&gt;true&lt;/code&gt; executes its corresponding block. If no condition is &lt;code&gt;true&lt;/code&gt;, the code inside the &lt;code&gt;default&lt;/code&gt; block (if present) is executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fallthrough in Switch
&lt;/h3&gt;

&lt;p&gt;Go’s &lt;code&gt;switch&lt;/code&gt; statement does not automatically fall through to the next case. However, you can explicitly use the &lt;code&gt;fallthrough&lt;/code&gt; keyword to achieve this behavior:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
 number := 2

 switch number {
 case 1:
 fmt.Println("One")
 fallthrough
 case 2:
 fmt.Println("Two")
 fallthrough
 case 3:
 fmt.Println("Three")
 // fallthrough // Uncommenting this line will fall through to the next case
 default:
 fmt.Println("Some other number")
 }
}

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

&lt;/div&gt;



&lt;p&gt;In this example, when &lt;code&gt;number&lt;/code&gt; is &lt;code&gt;2&lt;/code&gt;, the &lt;code&gt;fallthrough&lt;/code&gt; keyword is used after the &lt;code&gt;Two&lt;/code&gt; case. This causes the execution to fall through to the next case (&lt;code&gt;Three&lt;/code&gt;). Without the &lt;code&gt;fallthrough&lt;/code&gt; statement, the switch statement would exit after executing the matching case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type Switch
&lt;/h3&gt;

&lt;p&gt;Go’s &lt;code&gt;switch&lt;/code&gt; statement can also be used for type-switching. It allows you to check the type of an interface variable:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func checkType(x interface{}) {
 switch x.(type) {
 case int:
 fmt.Println("x is an integer")
 case string:
 fmt.Println("x is a string")
 default:
 fmt.Println("x is of unknown type")
 }
}

func main() {
 checkType(42)
 checkType("Hello")
 checkType(3.14)
}

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

&lt;/div&gt;



&lt;p&gt;In this example of golang switch case, the &lt;code&gt;checkType&lt;/code&gt; function uses a type switch to determine the type of the argument &lt;code&gt;x&lt;/code&gt;. The &lt;code&gt;case&lt;/code&gt; statements check if &lt;code&gt;x&lt;/code&gt; is an &lt;code&gt;int&lt;/code&gt; or a &lt;code&gt;string&lt;/code&gt;, and the &lt;code&gt;default&lt;/code&gt; case handles any other type.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;switch&lt;/code&gt; statement in Go is a versatile tool for making decisions based on the value or type of an expression. Whether you’re comparing values, handling multiple conditions, or checking types, the &lt;code&gt;switch&lt;/code&gt; statement provides a clean and efficient syntax for expressing decision logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing Between &lt;code&gt;switch&lt;/code&gt; and &lt;code&gt;if-else&lt;/code&gt; in Go
&lt;/h2&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%2Fbsqfekrftdwmk68jw83x.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%2Fbsqfekrftdwmk68jw83x.png" alt="Choosing Between switch and if-else in Go" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The choice between using a &lt;code&gt;switch&lt;/code&gt; statement and a series of &lt;code&gt;if-else&lt;/code&gt; statements in Go depends on the specific requirements and readability of the code. Both constructs are used for making decisions based on conditions, but they have different use cases and characteristics.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;switch&lt;/code&gt; Statement:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clear and Concise for Multiple Conditions:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier to Read and Maintain:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type Switching:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fallthrough:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of a &lt;code&gt;switch&lt;/code&gt; statement:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
 day := "Monday"

 switch day {
 case "Monday":
 fmt.Println("It's Monday!")
 case "Tuesday":
 fmt.Println("It's Tuesday!")
 default:
 fmt.Println("It's some other day.")
 }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;if-else&lt;/code&gt; Statements:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexible Conditions:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readable for Few Conditions:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Comparisons and Range Checks:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example of &lt;code&gt;if-else&lt;/code&gt; statements:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
 number := 42

 if number &amp;lt; 0 {
 fmt.Println("Negative number")
 } else if number == 0 {
 fmt.Println("Zero")
 } else {
 fmt.Println("Positive number")
 }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  When to Use Each:
&lt;/h3&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%2Fcxq4hix5srgt7tjrmafw.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%2Fcxq4hix5srgt7tjrmafw.jpg" alt="decision" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Opt for &lt;code&gt;switch&lt;/code&gt; in the Following Scenarios:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choose &lt;code&gt;if-else&lt;/code&gt; under the Following Circumstances:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Considerations:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Style:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Specific Requirements:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To wrap it up, when navigating the diverse landscape of decision-making in your code, both &lt;code&gt;switch&lt;/code&gt; and &lt;code&gt;if-else&lt;/code&gt; statements offer unique strengths. The selection between them hinges on the intricacy of your conditions and your code’s readability objectives. Opt for the &lt;code&gt;switch&lt;/code&gt; statement when aiming for clarity and conciseness with value-based conditions. On the flip side, embrace the flexibility of &lt;code&gt;if-else&lt;/code&gt; when dealing with more intricate or adaptable conditions.&lt;/p&gt;

&lt;p&gt;Have thoughts to share on your preference or experiences with these constructs? We’d love to hear from you! Drop a comment below and let’s continue the conversation. Your insights could be the key to helping fellow developers make informed decisions in their coding journeys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read More&lt;/strong&gt; - &lt;a href="https://golang.withcodeexample.com/blog/golang-tutorial-for-beginners/" rel="noopener noreferrer"&gt;Golang Tutorial for Beginners a Guide&lt;br&gt;
&lt;/a&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%2Ff3mfz461hg9nsx9kwkhp.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%2Ff3mfz461hg9nsx9kwkhp.png" alt="thank you" width="500" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>How To Send Email in Go: Goroutines and Channels</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Mon, 16 Oct 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/harendra21/how-to-send-email-in-go-goroutines-and-channels-2a73</link>
      <guid>https://dev.to/harendra21/how-to-send-email-in-go-goroutines-and-channels-2a73</guid>
      <description>&lt;p&gt;In the world of modern software development, communication is a key element. Sending emails is a common practice for various purposes, such as user notifications, reports, and more. Go, a statically typed and compiled language, provides an efficient and concurrent way to handle such tasks. In this article, we will explore how to send emails in Go using &lt;a href="https://golang.withcodeexample.com/blog/demystifying-goroutines-in-go/" rel="noopener noreferrer"&gt;goroutines&lt;/a&gt; and &lt;a href="https://golang.withcodeexample.com/blog/go-concurrency-channels-select-patterns/" rel="noopener noreferrer"&gt;channels&lt;/a&gt;. By the end of this tutorial, you’ll have a solid understanding of how to implement this feature in your Go applications.&lt;/p&gt;

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

&lt;p&gt;Before we dive into the code, let’s make sure you have the necessary tools and libraries installed on your system. You will need the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go programming language: Make sure you have Go installed. You can download it from the official website (&lt;a href="https://golang.org/" rel="noopener noreferrer"&gt;https://golang.org/)&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Setting Up the Environment
&lt;/h2&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%2Fl3mnfi8916z7cifv8zbc.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%2Fl3mnfi8916z7cifv8zbc.jpg" alt="https://res.cloudinary.com/harendra21/image/upload/v1697449301/golangwithexample/1_1qd63H2dn68MPaWvKZYR0g_urisfv.jpg" width="626" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you have Go installed, let’s set up the environment for sending emails. For this tutorial, we will use the “github.com/go-gomail/gomail” package, which simplifies email sending in Go.&lt;/p&gt;

&lt;p&gt;To install the “gomail” package, open your terminal and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get gopkg.in/gomail.v2

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Creating a Basic Email Sender
&lt;/h2&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%2Fuvxsq2uys71i6s5k1am8.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%2Fuvxsq2uys71i6s5k1am8.jpg" alt="https://res.cloudinary.com/harendra21/image/upload/v1697449365/golangwithexample/best_email_apps_ztoejq.jpg" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s start by creating a basic Go program that sends an email. We’ll use the “gomail” package for this purpose. Here’s a simple example of sending an email without using goroutines or channels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="s"&gt;"gopkg.in/gomail.v2"&lt;/span&gt;
 &lt;span class="s"&gt;"log"&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;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewMessage&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"From"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sender@example.com"&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"To"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"recipient@example.com"&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Subject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello, Golang Email!"&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;SetBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the body of the email."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDialer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"smtp.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;587&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"username"&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;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DialAndSend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&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="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this code, we create an email message using the “gomail” package, specify the sender and recipient addresses, set the email subject and body, and then use a dialer to send the email.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Using &lt;a href="https://golang.withcodeexample.com/blog/demystifying-goroutines-in-go/" rel="noopener noreferrer"&gt;Goroutines&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now, let’s enhance our email sending process using goroutines. Goroutines allow us to perform tasks concurrently, which can be incredibly useful when sending multiple emails. In this example, we’ll send emails to multiple recipients concurrently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="s"&gt;"gopkg.in/gomail.v2"&lt;/span&gt;
 &lt;span class="s"&gt;"log"&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;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&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;subject&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;body&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;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewMessage&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"From"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sender@example.com"&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"To"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Subject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&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;SetBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDialer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"smtp.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;587&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"username"&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;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DialAndSend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&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="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;log&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;"Failed to send email to"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&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;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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;log&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;"Email sent to"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&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;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;recipients&lt;/span&gt; &lt;span class="o"&gt;:=&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;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
 &lt;span class="n"&gt;Subject&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
 &lt;span class="n"&gt;Body&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="s"&gt;"recipient1@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello from Golang"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the first email."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"recipient2@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Greetings from Go"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the second email."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="c"&gt;// Add more recipients here&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;recipients&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="c"&gt;// Sleep to allow time for goroutines to finish&lt;/span&gt;
 &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&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;In this improved code, we have defined a “sendEmail” function that sends an email. We use goroutines to send emails to multiple recipients concurrently. This approach is more efficient and faster when you need to send emails to a large number of recipients.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Implementing a &lt;a href="https://golang.withcodeexample.com/blog/go-concurrency-channels-select-patterns/" rel="noopener noreferrer"&gt;Channel&lt;/a&gt; for Email Sending
&lt;/h2&gt;

&lt;p&gt;Now, let’s take our email sending functionality a step further by implementing a channel to manage the goroutines. Using a channel ensures that we can control and synchronize the email sending process effectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="s"&gt;"gopkg.in/gomail.v2"&lt;/span&gt;
 &lt;span class="s"&gt;"log"&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;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&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;subject&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;body&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;ch&lt;/span&gt; &lt;span class="k"&gt;chan&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;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewMessage&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"From"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sender@example.com"&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"To"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Subject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&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;SetBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDialer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"smtp.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;587&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"username"&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;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DialAndSend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&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="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Failed to send email to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;": "&lt;/span&gt; &lt;span class="o"&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;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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Email sent to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;to&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;recipients&lt;/span&gt; &lt;span class="o"&gt;:=&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;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
 &lt;span class="n"&gt;Subject&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
 &lt;span class="n"&gt;Body&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="s"&gt;"recipient1@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello from Golang"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the first email."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"recipient2@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Greetings from Go"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the second email."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="c"&gt;// Add more recipients here&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="n"&gt;emailStatus&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="k"&gt;chan&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;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;recipients&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;emailStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;recipients&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;emailStatus&lt;/span&gt;
 &lt;span class="n"&gt;log&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;status&lt;/span&gt;&lt;span class="p"&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;p&gt;In this updated code, we introduce a channel called “emailStatus” to communicate the status of email sending. Each goroutine sends its status to the channel, and the main function receives and logs these statuses. This approach allows us to manage and monitor email sending efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. &lt;a href="https://golang.withcodeexample.com/blog/mastering-error-handling-logging-go-guide/" rel="noopener noreferrer"&gt;Error Handling&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;When sending emails, it’s essential to handle errors gracefully. Let’s enhance our code to include error handling by implementing a retry mechanism for failed email sending.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
 &lt;span class="s"&gt;"gopkg.in/gomail.v2"&lt;/span&gt;
 &lt;span class="s"&gt;"log"&lt;/span&gt;
 &lt;span class="s"&gt;"time"&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;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&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;subject&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;body&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;ch&lt;/span&gt; &lt;span class="k"&gt;chan&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;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewMessage&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"From"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"sender@example.com"&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"To"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&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;SetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Subject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&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;SetBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gomail&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewDialer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"smtp.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;587&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"username"&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;var&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="kt"&gt;error&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="m"&gt;3&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="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;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DialAndSend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&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="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Email sent to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;to&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;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

 &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Retry after 5 seconds&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="s"&gt;"Failed to send email to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;": "&lt;/span&gt; &lt;span class="o"&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;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;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;recipients&lt;/span&gt; &lt;span class="o"&gt;:=&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;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
 &lt;span class="n"&gt;Subject&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
 &lt;span class="n"&gt;Body&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="s"&gt;"recipient1@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello from Golang"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the first email."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"recipient2@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Greetings from Go"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"This is the second email."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="c"&gt;// Add more recipients here&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="n"&gt;emailStatus&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="k"&gt;chan&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;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;recipients&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;emailStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;recipients&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;emailStatus&lt;/span&gt;
 &lt;span class="n"&gt;log&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;status&lt;/span&gt;&lt;span class="p"&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;p&gt;In this final example, we’ve added a retry mechanism to our email sending function. If an email fails to send, the code will retry up to three times, with a 5-second delay between each attempt. This ensures that even in the face of transient issues, the email will eventually be sent. Additionally, we’ve improved error handling by providing informative error messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we’ve explored how to send emails in Go using goroutines and channels. We started with a basic email sender, enhanced it with goroutines for concurrent sending, and then introduced a channel to manage the communication between goroutines and the main function. Finally, we implemented error handling with a retry mechanism.&lt;/p&gt;

&lt;p&gt;By following the examples provided in this article, you can efficiently send emails from your Go applications, even to multiple recipients, while ensuring robust error handling and efficient concurrency. This approach is especially useful for applications that rely on email communication for notifications, reports, or other purposes. Happy coding!&lt;/p&gt;

</description>
      <category>go</category>
    </item>
    <item>
      <title>Promise In Javascript</title>
      <dc:creator>Harendra Kumar Kanojiya</dc:creator>
      <pubDate>Fri, 22 Sep 2023 09:32:37 +0000</pubDate>
      <link>https://dev.to/harendra21/promise-in-javascript-4nl</link>
      <guid>https://dev.to/harendra21/promise-in-javascript-4nl</guid>
      <description>&lt;p&gt;यह मेरा पहला लेख है हिंदी में, बस कुछ नया आजमाने की कोशिश कर रहा हूँ। आशा है कि आपको यह पसंद आएगा।&lt;/p&gt;

&lt;p&gt;क्या आपने javascript में 'promises' के बारे में सीखा है? यह एक विषय है जिस पर बहुत से लोग तुरंत ही हाथ उठा देते हैं, लेकिन मैं आपके लिए इसे जितना सरल बनाने का प्रयास करूँगा, जितना हो सके।&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Promise का मतलब क्या है?
&lt;/h2&gt;

&lt;p&gt;"Promise" एक asynchronous प्रोग्रामिंग में मौलिक अवधारणा है, खासकर javascript और कई आधुनिक प्रोग्रामिंग भाषाओं में। इसका मतलब है कि यह एक मूल्य को प्रतिनिधित करता है (या किसी कार्य के अंतर्गत परिणाम को) जो शायद अब तक उपलब्ध नहीं है, लेकिन भविष्य में किसी समय विफलता के साथ (त्रुटि के साथ) या सफलता के साथ निर्धारित होगा।&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Promise की स्थितियाँ:
&lt;/h2&gt;

&lt;p&gt;Javascript में, एक Promise तीन स्थितियों में से एक में हो सकता है:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pending (लंबित):&lt;/strong&gt; यह वो प्रारंभिक स्थिति है जब एक Promise बनाई जाती है। इसका मतलब है कि Promise द्वारा प्रतिनिधित asynchronous कार्य अब तक पूरा नहीं हुआ है, और परिणाम (पूर्ण या असफल) उपलब्ध नहीं है। Promises इस स्थिति में शुरू होती हैं और फिर दूसरी स्थितियों में से किसी में जाती हैं।&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fulfilled (पूर्ण):&lt;/strong&gt; यह स्थिति असिंक्रोनस कार्य की सफल पूर्णता को प्रतिनिधित करती है। जब एक Promise पूर्ण स्थिति में चली जाती है, तो इसका मतलब है कि कार्य सफलतापूर्वक समाप्त हो गया है, और परिणाम (एक मूल्य या डेटा) उपलब्ध है। आप &lt;code&gt;.then()&lt;/code&gt; मेथड का उपयोग करके पूर्ण मूल्य को पहुँच सकते हैं।&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rejected (असफल):&lt;/strong&gt; यह स्थिति असिंक्रोनस कार्य की असफलता को प्रतिनिधित करती है। जब एक Promise असफल स्थिति में चली जाती है, तो इसका मतलब है कि कार्य के दौरान कोई त्रुटि या अपवाद हुआ है। आप असफलता के कारण (एक त्रुटि ऑब्जेक्ट या संदेश) को पहुँच सकते हैं और इसे &lt;code&gt;.catch()&lt;/code&gt; मेथड या &lt;code&gt;.then()&lt;/code&gt; मेथड के दूसरे आर्ग्यमेंट का उपयोग करके हैंडल कर सकते हैं।&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;यहां एक Promise की स्थितियों और स्थितियों के बदलावों का एक दृश्यात्मक प्रतिष्ठान है:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Initial State:      Pending
                   /       \
Fulfilled State:  Fulfilled  Rejected
(result available) (error occurred)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"Promises" का डिज़ाइन इसे असिंक्रोनस कार्यों के साथ काम करने के एक संरचित तरीके प्रदान करने के लिए है, जिससे आप सफलता और असफलता के मामलों को अलग-अलग हैंडल कर सकते हैं। आप &lt;code&gt;.then()&lt;/code&gt; का उपयोग करके स्पष्ट कर सकते हैं कि प्रॉमिस पूर्ण होने पर क्या करना है और &lt;code&gt;.catch()&lt;/code&gt; का उपयोग करके त्रुटियों का संबोधन कर सकते हैं जब प्रॉमिस असफल होता है। इससे तुलनात्मक प्रोक्षिप्त आधारित दृष्टिकोणों के मुकाबले असिंक्रोनस कोड को प्रबंधनीय और पढ़ने में आसानी हो जाती है।&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;strong&gt;प्रॉमिस कैसे बनाते हैं&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Javascript में एक प्रॉमिस बनाने के लिए, आप &lt;code&gt;Promise&lt;/code&gt; कंस्ट्रक्टर का उपयोग कर सकते हैं, जो एक एकल फ़ंक्शन को एक विधि के रूप में लेता है। इस फ़ंक्शन को कार्यकर्ता फ़ंक्शन कहा जाता है, और इसमें दो पैरामीटर होते हैं: &lt;code&gt;resolve&lt;/code&gt; और &lt;code&gt;reject&lt;/code&gt;। &lt;code&gt;resolve&lt;/code&gt; फ़ंक्शन का उपयोग प्रॉमिस को एक मूल्य से पूर्ण करने के लिए किया जाता है, और &lt;code&gt;reject&lt;/code&gt; फ़ंक्शन का उपयोग एक त्रुटि के साथ प्रॉमिस को अस्वीकृत करने के लिए किया जाता है।&lt;/p&gt;

&lt;p&gt;यहां एक प्रॉमिस बनाने की मूल संरचना है:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Asynchronous or time-consuming operation goes here&lt;/span&gt;
  &lt;span class="c1"&gt;// Typically, you would perform some async task, like fetching data or reading a file&lt;/span&gt;

  &lt;span class="c1"&gt;// If the operation is successful, call resolve with the result&lt;/span&gt;
  &lt;span class="c1"&gt;// resolve(result);&lt;/span&gt;

  &lt;span class="c1"&gt;// If an error occurs, call reject with an error object or message&lt;/span&gt;
  &lt;span class="c1"&gt;// reject(error);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;यहां एक और स्पष्ट उदाहरण है जो setTimeout का उपयोग करके देरी से असिंक्रोनस कार्य को सिम्युलेट करता है और कुछ समय बाद प्रॉमिस को पूर्ण करता है:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;milliseconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Resolved after &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;milliseconds&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; milliseconds`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;milliseconds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Usage:&lt;/span&gt;
&lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Wait for 2 seconds&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Resolved after 2000 milliseconds&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&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;/div&gt;



&lt;p&gt;इस उदाहरण में:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;हम एक फ़ंक्शन &lt;code&gt;delay&lt;/code&gt; की परिभाषा करते हैं जो एक प्रॉमिस लौटाता है। प्रॉमिस कंस्ट्रक्टर के अंदर, हम &lt;code&gt;setTimeout&lt;/code&gt; का उपयोग एक असिंक्रोनस देरी को सिम्युलेट करने के लिए करते हैं।&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;अगर असिंक्रोनस कार्य सफल होता है (यानी, &lt;code&gt;setTimeout&lt;/code&gt; पूरा हो जाता है), तो हम परिणाम के साथ &lt;code&gt;resolve&lt;/code&gt; को कॉल करते हैं।&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;कार्य के दौरान कोई त्रुटि होती है, तो हम किसी त्रुटि ऑब्जेक्ट या संदेश के साथ &lt;code&gt;reject&lt;/code&gt; को कॉल कर सकते हैं।&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;हम &lt;code&gt;.then()&lt;/code&gt; का उपयोग करते हैं ताकि निश्चित कर सकें कि प्रॉमिस पूर्ण होते समय क्या करना है (पूर्ण किया गया है), और हम &lt;code&gt;.catch()&lt;/code&gt; का उपयोग करते हैं ताकि कोई भी त्रुटियां जो हो सकती हैं, उन्हें हैंडल कर सकें।&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;यह एक सरल उदाहरण है, लेकिन व्यावासिक रूप में, आप setTimeout को असली असिंक्रोनस कार्यों के साथ बदलेंगे, जैसे कि API अनुरोध करना या एक फ़ाइल को पढ़ना। प्रॉमिस असिंक्रोनस कोड को हैंडल करने और इसे और पठनीय और रखने योग्य बनाने के लिए एक संरचित तरीका प्रदान करते हैं।&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 &lt;strong&gt;प्रॉमिस पूर्ण होते समय मूल्य लौटाना&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;जावास्क्रिप्ट प्रॉमिस में, आप प्रॉमिस पूर्ण होते समय मूल्य लौटा सकते हैं, प्रॉमिस के कार्यकर्ता फ़ंक्शन के भीतर &lt;code&gt;resolve&lt;/code&gt; फ़ंक्शन को प्रॉमिस के निर्वाचन के रूप में स्वीकृत मूल्य के रूप में एक आर्ग्यमेंट के रूप में प्रदान करके। यहां दिखाया गया है कि आप ऐसा कैसे कर सकते हैं:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createPromiseWithValue&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;valueToReturn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is the resolved value.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;valueToReturn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;createPromiseWithValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "This is the resolved value."&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&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;/div&gt;



&lt;p&gt;इस उदाहरण में, हमने &lt;code&gt;createPromiseWithValue&lt;/code&gt; नामक फ़ंक्शन का परिभाषित किया है, जो एक प्रॉमिस बनाता है और &lt;code&gt;resolve&lt;/code&gt; के द्वारा पूर्ण किया जाने वाले मूल्य को प्रदान करता है। जब प्रॉमिस पूर्ण होता है, तो हम &lt;code&gt;.then()&lt;/code&gt; का उपयोग करके पूर्ण हुए मूल्य को प्रिंट करते हैं।&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;प्रॉमिस असफल होने पर एक त्रुटि लौटाना&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;जावास्क्रिप्ट प्रॉमिस में, आप प्रॉमिस असफल होते समय एक त्रुटि संदेश या एक त्रुटि ऑब्जेक्ट को प्रॉमिस के कार्यकर्ता फ़ंक्शन के भीतर &lt;code&gt;reject&lt;/code&gt; फ़ंक्शन के एक आर्ग्यमेंट के रूप में प्रदान करके एक त्रुटि लौटा सकते हैं। यहां दिखाया गया है कि आप ऐसा कैसे कर सकते हैं:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createPromiseWithError&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is an error message.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;createPromiseWithError&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&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;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "This is an error message."&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;इस उदाहरण में, हमने &lt;code&gt;createPromiseWithError&lt;/code&gt; नामक फ़ंक्शन का परिभाषित किया है, जो एक प्रॉमिस बनाता है और &lt;code&gt;reject&lt;/code&gt; के द्वारा त्रुटि संदेश को असफल किया जाने वाले मूल्य के रूप में प्रदान करता है। जब प्रॉमिस असफल होता है, तो हम &lt;code&gt;.catch()&lt;/code&gt; का उपयोग करके त्रुटि संदेश को प्रिंट करते हैं।&lt;/p&gt;

&lt;p&gt;पढ़ने के लिए धन्यवाद, मुझे ट्विटर पर फ़ॉलो करें - &lt;a href="https://twitter.com/harendraverma2" rel="noopener noreferrer"&gt;@harendraverma2&lt;/a&gt;&lt;/p&gt;

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