<?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: Chiranjeevi Tirunagari</title>
    <description>The latest articles on DEV Community by Chiranjeevi Tirunagari (@vchiranjeeviak).</description>
    <link>https://dev.to/vchiranjeeviak</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%2F782529%2Ff8e826ae-cb31-4df5-9fd3-42d4d0a4926e.jpg</url>
      <title>DEV Community: Chiranjeevi Tirunagari</title>
      <link>https://dev.to/vchiranjeeviak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vchiranjeeviak"/>
    <language>en</language>
    <item>
      <title>Azure AD intro with Terraform examples</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Tue, 20 Jun 2023 16:25:16 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/azure-ad-intro-with-terraform-examples-13dl</link>
      <guid>https://dev.to/vchiranjeeviak/azure-ad-intro-with-terraform-examples-13dl</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Azure Active Directory (Azure AD) offers organizations a comprehensive cloud-based solution for efficient directory and identity management. With its array of features, including username and password management, role and permissions control, multi-factor authentication, and application monitoring, Azure AD is an indispensable tool for businesses of all sizes. This article explores the key aspects of Azure AD, starting with user management, followed by administrative units, and concluding with group management.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terraform setup for Azure AD
&lt;/h2&gt;

&lt;p&gt;First, we need to specify the providers that we are going to use so that Terraform knows which providers it needs to download.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;terraform&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;required_providers&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;azuread&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="err"&gt;source&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hashicorp/azuread"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="err"&gt;version&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~&amp;gt; 2.15.0"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;provider&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"azuread"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;tenant_id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;your_azure_account_tenant_id&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run &lt;code&gt;terraform init&lt;/code&gt; command after adding the above code to any of the files ( &lt;code&gt;providers.tf&lt;/code&gt; is a preferred name ) in the terraform directory, it downloads the required providers.&lt;/p&gt;

&lt;p&gt;For whatever code we use in this article to work, we need to authenticate to our Azure account so that the changes we create in code will actually happen. The easiest and recommended way to authenticate for Terraform is through service principal and client secret.&lt;/p&gt;

&lt;p&gt;To create one service principal, we need to have a Contributor role for Azure AD. If we have that, we can create a service principal using the command &lt;code&gt;az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/&amp;lt;your-subscription-number&amp;gt;"&lt;/code&gt; in Azure CLI which can be downloaded from &lt;a href="https://learn.microsoft.com/en-us/cli/azure/install-azure-cli" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/cli/azure/install-azure-cli&lt;/a&gt; here if you haven't already. This will return a response containing 4 important fields which we want. They are id ( subscription_id ), tenant_id, client_id, and client_secret. It is better to create a bash file which contains the following contents.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ARM_CLIENT_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;client_id&amp;gt;"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ARM_CLIENT_SECRET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;client_secret&amp;gt;"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ARM_TENANT_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;tenant_id&amp;gt;"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ARM_SUBSCRIPTION_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"subscription_id"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this script before running any plan. Now, we have the setup ready and we can move on. If you get any error during the creation of the service principal, check your permissions and if your permissions are fine, it might have created the principal but still threw the error as happened in my case.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Managing Users in Azure AD&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Azure AD provides robust user management capabilities that enable organizations to efficiently create and oversee user identities in the cloud. Here are some essential aspects of managing users in Azure AD:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;User Properties&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User Profile&lt;/strong&gt;: Each user has a profile that contains basic information such as their name, email address, and contact details.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Assigned Roles&lt;/strong&gt;: Users can be assigned specific roles within the organization, granting them access to various resources and services based on their responsibilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Administrative Units&lt;/strong&gt;: Azure AD allows for the grouping of users based on geographical or organizational units, facilitating streamlined user management. Administrative Units (AUs) enable organizations to assign specific administrators to oversee users from particular regions or units.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Group Membership&lt;/strong&gt;: Users can be part of different groups within Azure AD, allowing for efficient management of permissions and access rights.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Applications&lt;/strong&gt;: Azure AD enables the assignment of applications to users, granting them access to specific software and services.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Licenses&lt;/strong&gt;: Users can be assigned licenses that provide access to specific features or services within Azure AD.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Devices&lt;/strong&gt;: Azure AD associates devices with user accounts, providing information about the devices and their join type, such as Azure AD join or hybrid Azure AD join.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Role Assignments&lt;/strong&gt;: User roles define the resources to which a user has access, specifying their permissions and privileges.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authentication Methods&lt;/strong&gt;: Azure AD supports various authentication methods, allowing users to choose the method that best suits their needs.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating a user with Terraform
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;resource&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"random_password"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;length&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;special&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;override_special&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"!#$%^&amp;amp;*[]{}()"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;resource&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"azuread_user"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;user_principal_name&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;email-format-with-domain-specific-to-your-azure-account&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;display_name&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;password&lt;/span&gt;&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;random_password.test.result&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;force_password_change&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;given_name&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"article"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;surname&lt;/span&gt;&lt;span class="w"&gt;               &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"terraform"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform forces us to provide a password for every user. So, I am using this random password generator first, getting a random password from it &lt;a href="https://registry.terraform.io/providers/hashicorp/random/3.0.0/docs/resources/password" rel="noopener noreferrer"&gt;https://registry.terraform.io/providers/hashicorp/random/3.0.0/docs/resources/password&lt;/a&gt; and passing it to the user. I am also forcing the user to change the password when they first try to log in so that no one can see the password from the terraform state. Only the first 3 fields in the above "azuread_user" block are necessary and others are optional. I added "given_name" and "surname" blocks to help in the group creation which is coming next.&lt;/p&gt;

&lt;p&gt;You can check more options that can be provided while creating the user here &lt;a href="https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/user" rel="noopener noreferrer"&gt;https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/user&lt;/a&gt; .&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Streamlining User Management with Administrative Units&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Azure AD introduces the concept of Administrative Units (AUs) to simplify user management and enable efficient delegation of administrative responsibilities. AUs are particularly useful for grouping users based on specific regions or organizational units. Here's what you need to know about Azure Administrative Units:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Efficient User Grouping&lt;/strong&gt;: AUs help organize users based on geographical or organizational criteria, allowing administrators to manage users from specific regions or units more effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Assigning Administrators&lt;/strong&gt;: Within each Administrative Unit, administrators can be assigned specific roles to perform tasks relevant to their areas of responsibility. These roles include Authentication admin, Groups admin, Help desk admin, License admin, Password admin, and User admin.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By utilizing Azure Administrative Units, organizations can streamline user management, delegate administrative responsibilities, and ensure efficient control over user access and permissions based on specific regions or organizational units.&lt;/p&gt;

&lt;p&gt;AUs require a premium tier account and I don't have one. So I m not able to show you a tested terraform example here.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Simplifying User Collaboration with Azure AD Groups&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Azure AD groups provide a convenient way to manage and organize users within an organization, promoting collaboration and resource sharing. There are two primary types of groups in Azure AD:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security Groups&lt;/strong&gt;: Security groups are used to grant permissions to a set of users for specific cloud resources. They streamline access control and simplify user management by providing a collective set of permissions to a group of users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Microsoft 365 Groups&lt;/strong&gt;: Microsoft 365 groups facilitate collaboration by providing access to shared resources such as calendars, mailboxes, and more. They enable users to work together seamlessly on projects and initiatives.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Azure AD allows for different methods of grouping users into security groups:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Assigned Grouping&lt;/strong&gt;: Users can be manually assigned to a group, providing direct control over group membership.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic User Grouping&lt;/strong&gt;: Grouping users dynamically involves setting parameters based on user properties, and automatically adding or removing users from the group based on predefined criteria.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic Device Grouping&lt;/strong&gt;: Similar to dynamic user grouping, dynamic device grouping involves setting parameters based on device properties, and automatically grouping devices based on predefined criteria.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Azure AD group properties and functionalities include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: Basic information about the group, including membership type and unique identifier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Properties&lt;/strong&gt;: Group details such as name, type, description, and other relevant attributes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Members&lt;/strong&gt;: Users who are part of the group.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Owners&lt;/strong&gt;: Administrators with control over the group and its settings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Administrative Units&lt;/strong&gt;: Association of groups with specific AUs for streamlined management.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Group Memberships&lt;/strong&gt;: Hierarchical grouping of groups within other security groups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Applications&lt;/strong&gt;: Assignment of applications to groups for collective access to services.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Licenses&lt;/strong&gt;: Group-level assignment of licenses for consistent access to licensed features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Role Assignments&lt;/strong&gt;: Resources accessible to group members, defining permissions and privileges.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic Membership Rules&lt;/strong&gt;: Configuration rules for automatic group membership updates based on criteria and properties.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating groups with Terraform
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;resource&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"azuread_group"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;display_name&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;security_enabled&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;dynamic_membership&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;enabled&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;rule&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user.surname -eq &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;terraform&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "display_name" and either "security_enabled" or "mail_enabled" being true are required and all others are optional. To add users to a group depending on a condition, we use "dynamic_membership" which takes a rule according to which it groups users. Here, I am grouping users whose "surname" is equal to "terraform" and hence it adds only the user which we created before to this group.&lt;/p&gt;

&lt;p&gt;This also requires a premium plan to work. We can manually add users to a group in the basic tier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"azuread_client_config"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"current"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;resource&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"azuread_group"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;display_name&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;security_enabled&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;members&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;azuread_user.test.object_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;data.azuread_client_config.object_id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, we can add users manually to a group by specifying their "object_id" in the members list. The way we fetch already existing data from the cloud to the terraform just to use some details on our current is through a "data" block like how we did in the first line above. In the above code, I am getting the current user who is applying the Terraform plan which is the user associated with the service principal that we created earlier to use with Terraform. And then, I added the object_id of it in the members list. So, this time, the group contains two users. One is the Terraform user, and the other is the user we created above with Terraform code.&lt;/p&gt;

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

&lt;p&gt;Azure Active Directory (Azure AD) provides a robust and comprehensive solution for directory and identity management in the cloud. By effectively managing users, leveraging Administrative Units, and harnessing the power of Azure AD groups, organizations can streamline user management, delegate administrative responsibilities, and enhance collaboration among users. Azure AD offers a seamless and secure identity management solution that empowers businesses to embrace the cloud and unlock their full potential.&lt;/p&gt;

&lt;p&gt;If you find this article helpful and informative, please don't forget to like and share it. Also, follow &lt;a href="https://dev.to/vchiranjeeviak"&gt;Chiranjeevi Tirunagari&lt;/a&gt; for more such articles. Consider connecting with me on socials at &lt;a href="https://www.linkedin.com/in/vchiranjeeviak/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/vchiranjeeviak/&lt;/a&gt; and &lt;a href="https://twitter.com/vchiranjeeviak" rel="noopener noreferrer"&gt;https://twitter.com/vchiranjeeviak&lt;/a&gt; . Thank you.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Number types extended... ( in RUST )</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Wed, 05 Apr 2023 07:08:54 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/number-types-extended-in-rust--8hp</link>
      <guid>https://dev.to/vchiranjeeviak/number-types-extended-in-rust--8hp</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Welcome back to this article in which we are going to see more things about number types in Rust. If you haven't seen the &lt;a href="https://vchiranjeeviak.hashnode.dev/number-types-in-rust" rel="noopener noreferrer"&gt;&lt;strong&gt;previous article&lt;/strong&gt;&lt;/a&gt; where we discussed the basics of number types in Rust, please check that out first.&lt;/p&gt;

&lt;p&gt;Note: This article comprises examples that are inspired by &lt;strong&gt;Rust in Action&lt;/strong&gt; book by &lt;strong&gt;Tim McNamara&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;A little bit about me...&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Hi reader 🖐. I am Chiranjeevi Tirunagari and I am a software engineer. You can find more about me on &lt;a href="https://www.linkedin.com/in/vchiranjeeviak/" rel="noopener noreferrer"&gt;&lt;strong&gt;Linkedin&lt;/strong&gt;&lt;/a&gt; and &lt;a href="http://twitter.com/vchiranjeeviak" rel="noopener noreferrer"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt;. Also, subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;&lt;strong&gt;youtube channel&lt;/strong&gt;&lt;/a&gt; if you are interested in Rust, DevOps, and tech in general.&lt;/p&gt;

&lt;p&gt;Now, let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Comparing values&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We often must compare multiple values and do something accordingly in the code. We check if one value is smaller, greater, equal, or not equal to the other. In Rust, we can compare values if they belong to the same type. That's obvious. That happens in most languages, right? We can't compare an integer with a string in Java. Yeah, but it is much stricter in Rust. We can't compare one integer type ( like a u16 ) with another integer type ( like a u64 ).&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 rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;13_u16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300_u64&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;num1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Went through!"&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;If you try to compile this program, it throws an error saying "Types mismatched". But, we can't keep using the same type for all variables. Some variables may require less space when compared to others. We can't just use the bigger type just for the sake of comparison.&lt;/p&gt;

&lt;p&gt;But, what we can do is that, we can cast one type to the other just while comparing without affecting the actual type of the variable. So, we can get rid of the error to do what we wanted in the above program by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;13_u16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300_u64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Went through!"&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;If you run it, you should see the "Went through!" output.&lt;/p&gt;

&lt;p&gt;If you observe, we typecasted the smaller type into the larger one, not the other way around. That is because we get runtime issues when we try to typecast a larger type to a smaller one. Try to cast a bigger u128 number to the u8 type while printing to see that in action. You will see the change in values. This is because u8 can't accommodate bigger numbers that require u128 and so it wraps around.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Problem with floating-points&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Floating points are the numbers that are represented in base 10. But anything that is being stored in computer memory is stored in base 2. This is where the problems arise. This makes the numbers lose precision when dealing with floating points. The following program throws an error which is unexpected generally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// assert_eq! checks if both arguments are equal&lt;/span&gt;
    &lt;span class="c1"&gt;// If not, the program panics (throws runtime error and crashes)&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.3&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;And the error looks like this:&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%2Fq8a04ldzaury8a7se0ai.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%2Fq8a04ldzaury8a7se0ai.png" alt="error image" width="800" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, it evaluated &lt;code&gt;0.1 + 0.2&lt;/code&gt; as &lt;code&gt;0.30000000000000004&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What can we do about this?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can avoid comparisons between floating points.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ignore the small difference that we got.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The small difference that we got in the above calculation is called &lt;strong&gt;epsilon.&lt;/strong&gt; And we can write programs in such a way that epsilon is ignored when comparing. The program for the same is below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;absolute_difference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.abs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// assert! panics if the expression inside evaluates to false&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;absolute_difference&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nn"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;EPSILON&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// EPSILON is a constant available for both f32 and f64 types&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There shouldn't be any error in this as we are accounting for the &lt;strong&gt;epsilon&lt;/strong&gt; now.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Not a Number&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Not a Number (&lt;strong&gt;NAN&lt;/strong&gt;) is also a floating point number that doesn't have any value. This usually is resulted when we try to do operations that result in &lt;strong&gt;undefined&lt;/strong&gt;. Examples include taking square root for negative numbers, etc. To see this, let's write a program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;56_f32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.sqrt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// sqrt function returns the square root&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NAN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program panics even though both the arguments for &lt;code&gt;assert_eq!&lt;/code&gt; are &lt;code&gt;NAN&lt;/code&gt;. This is because &lt;code&gt;NAN&lt;/code&gt; is not equal to &lt;code&gt;NAN&lt;/code&gt;. You can confirm this using the following program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NAN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NAN&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;Even this panics, which confirms our statement that &lt;code&gt;NAN&lt;/code&gt; is not equal to &lt;code&gt;NAN&lt;/code&gt;. How do we confirm if a result is &lt;code&gt;NAN&lt;/code&gt; or not? We have a function called &lt;code&gt;is_nan()&lt;/code&gt; for this purpose. Let's change our program to use that function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;42.0_f32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.sqrt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="nf"&gt;.is_nan&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program shouldn't panic. We have a function called &lt;code&gt;is_infinity()&lt;/code&gt; to deal with infinity cases like dividing with zero, etc. An example of that is below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;11.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="nf"&gt;.is_infinite&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;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I hope this article helped you understand something that you already don't know.&lt;/p&gt;

&lt;p&gt;If you find this read-worthy, please don't forget to like, share your thoughts and share this article with your friends and community because that encourages me a lot. Also, don't forget to connect with me on the socials I mentioned in the intro. Subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;&lt;strong&gt;youtube channel&lt;/strong&gt;&lt;/a&gt; as well for similar content if you are a visual learner. Let's meet in the next article, bye-bye 👋 until then.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Number types ( in RUST )</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Mon, 03 Apr 2023 06:50:04 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/number-types-in-rust--43ng</link>
      <guid>https://dev.to/vchiranjeeviak/number-types-in-rust--43ng</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome to this article in which we are going to cover what is called number datatypes in Rust. Most of the things we will discuss in this article are not just confined to Rust, but they are also evident in other programming languages. But, if you are here to learn Rust, then make sure you take a look at the previous 2 articles in this series which teach writing "Hello World" in 2 different ways.&lt;/p&gt;

&lt;p&gt;So, let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;A little bit about me...&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Hi reader 🖐. I am Chiranjeevi Tirunagari and I am a software engineer. You can find more about me on &lt;a href="https://www.linkedin.com/in/vchiranjeeviak/" rel="noopener noreferrer"&gt;&lt;strong&gt;Linkedin&lt;/strong&gt;&lt;/a&gt; and &lt;a href="http://twitter.com/vchiranjeeviak" rel="noopener noreferrer"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt;. Also, subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;&lt;strong&gt;youtube channel&lt;/strong&gt;&lt;/a&gt; if you are interested in Rust, DevOps and tech in general.&lt;/p&gt;

&lt;p&gt;Now, let's really get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Datatype
&lt;/h2&gt;

&lt;p&gt;No matter which programming language you are dealing with, you must have heard one thing definitely which is &lt;strong&gt;Datatype.&lt;/strong&gt; I am not getting into explaining data now. If you don't know what data is, then this is probably not for you. Usually, we are required to store some data as part of the application or program that we are writing. This data can be of many types. The names, addresses, and all other text data belong to some types. Numbers belong to some other types. We can create our own types, etc.&lt;/p&gt;

&lt;p&gt;What we will see in this article is about &lt;strong&gt;Numbers&lt;/strong&gt; related datatypes that are present in Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Numbers
&lt;/h2&gt;

&lt;p&gt;There are different types of numbers in general when it comes to Mathematics. In programming, we usually have 3 types of numbers. They are &lt;strong&gt;integers (without a fractional part)&lt;/strong&gt;, &lt;strong&gt;floating-point (with a fractional part)&lt;/strong&gt; numbers, and &lt;strong&gt;complex (with an imaginary part)&lt;/strong&gt; numbers.&lt;/p&gt;

&lt;p&gt;In most applications, we don't need to use numbers with sharp precision or complex numbers or numbers with huge values. We can use Rust's default numbers. If you think, you need all of these, then it is recommended to use this crate called &lt;code&gt;num&lt;/code&gt;. Okay, what is a &lt;strong&gt;crate&lt;/strong&gt;? Good question. The simplest answer can be a package which provides some functionality out of the box for which we don't need to write code. We will discuss these in future articles.&lt;/p&gt;

&lt;p&gt;Now let's see the default number types provided by Rust. But, before that, keep in mind that no matter what type it is, everything is going to be stored inside memory and it occupies some space in the memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integers
&lt;/h3&gt;

&lt;p&gt;As the name suggests, these are the numbers which can be 0, positive or negative. These numbers don't consist of a fractional part. Examples can be -100, 0, 34, etc. Depending on the range that the value can possess, integers can be classified into two types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Signed Integers - values can be either positive or negative&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unsigned Integers - values can only be positive&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Depending on the space required to store in the memory, integers are classified into multiple types such as 8-bit, 16-bit, 32-bit, 64-bit and 128-bit, the max. So, a 16-bit integer only occupies 16 bits in the memory. That is how it works. But, how does the space required affect the type? It affects the range of the type.&lt;/p&gt;

&lt;p&gt;For example, let's consider an integer of 8 bits. We know that a bit can accommodate either 0 or 1. There are 2 possibilities to fill a bit. Then, how many possibilities are there in filling 8 bits?&lt;/p&gt;

&lt;p&gt;0 or 1 0 or 1 0 or 1 0 or 1 0 or 1 0 or 1 0 or 1 0 or 1&lt;/p&gt;

&lt;p&gt;_____ _____ _____ _____ _____ _____ _____ _____&lt;/p&gt;

&lt;p&gt;2^8 !&lt;/p&gt;

&lt;p&gt;which is equal to 256. That means an 8-bit integer's capacity is 256. If it is an unsigned integer, it can hold from 0 to 255 ( total 256 values ). If it is a signed number, it can hold from -128 to 127 ( total 256 values ).&lt;/p&gt;

&lt;p&gt;We denote a signed integer with &lt;code&gt;i&lt;/code&gt; and an unsigned integer with &lt;code&gt;u&lt;/code&gt;. Usually, these letters are followed by their sizes. For example, &lt;code&gt;i8&lt;/code&gt;, &lt;code&gt;i16&lt;/code&gt;, &lt;code&gt;u32&lt;/code&gt;, &lt;code&gt;u64&lt;/code&gt;, &lt;code&gt;i128&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;There are two other types of integers. They are &lt;code&gt;usize&lt;/code&gt; and &lt;code&gt;isize&lt;/code&gt;. &lt;code&gt;u&lt;/code&gt; and &lt;code&gt;i&lt;/code&gt; denote the same as before, but the size depends on the architecture of the system they are running in. If it is a 32-bit system, it will be of size 32. If it is a 64-bit system, it will be of size 64.&lt;/p&gt;

&lt;h4&gt;
  
  
  Variables
&lt;/h4&gt;

&lt;p&gt;Usually, we assign values to variables. Variables are just names which are bonded with some values. The syntax to create a variable is &lt;code&gt;let &amp;lt;variable-name&amp;gt;: &amp;lt;datatype&amp;gt; = &amp;lt;value&amp;gt;&lt;/code&gt;. The data type is not necessary when we are initialising it with some value. If we are just declaring the variable, then it is necessary to specify the datatype that the variable is going to accept.&lt;/p&gt;

&lt;p&gt;All the variables in Rust are immutable ( value can't be changed ) by default. But, we can make them mutable using the &lt;code&gt;mut&lt;/code&gt; ( call it mutt or mute, I like to call it mute, because that makes sense ) keyword. So, the syntax looks like &lt;code&gt;let mut &amp;lt;variable-name&amp;gt; = &amp;lt;value&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Size interpretation
&lt;/h4&gt;

&lt;p&gt;When we are not initialising a variable along with its data type, then the compiler can interpret the type depending on the value. We can give values like &lt;code&gt;29u32&lt;/code&gt; or &lt;code&gt;30_i64&lt;/code&gt; etc. The compiler can parse these and can interpret the values accordingly. Similarly in the case with other datatypes as well. Even if we don't add suffixes like the above, the compiler can still do the job of interpreting the type when the variable or the value inside it is used to do operations such as arithmetic operations, comparisons etc. with other variables or values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Floating-Point Numbers
&lt;/h3&gt;

&lt;p&gt;Floating-point numbers consist of a fractional part. For example, &lt;code&gt;100.45&lt;/code&gt;, &lt;code&gt;-56.32&lt;/code&gt;, &lt;code&gt;20.0&lt;/code&gt; are considered a floating-point number. There are two variants of floats in Rust. They are &lt;code&gt;f32&lt;/code&gt; and &lt;code&gt;f64&lt;/code&gt;. These types are associated with some constants like Infinity, Negative infinity, Max, Min, etc. for ease. They are also associated with some functions like &lt;code&gt;sqrt()&lt;/code&gt;, &lt;code&gt;floor()&lt;/code&gt;, etc.&lt;/p&gt;

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

&lt;p&gt;In this article, we have just seen the theory part of it. For most of us, this is just basic. In the next article, we will see some code around it.&lt;/p&gt;

&lt;p&gt;If you find this read-worthy, please don't forget to like, share your thoughts and share this article with your friends and community because that encourages me a lot. Also, don't forget to connect with me on the socials I mentioned in the intro. Subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;&lt;strong&gt;youtube channel&lt;/strong&gt;&lt;/a&gt; as well for similar content if you are a visual learner. Let's meet in the next article, bye-bye 👋 until then.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>rust</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Traditional Hello World ( in RUST )</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Fri, 31 Mar 2023 10:13:27 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/traditional-hello-world-in-rust--43c</link>
      <guid>https://dev.to/vchiranjeeviak/traditional-hello-world-in-rust--43c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you are new to Rust and you are here to know how to write "Hello World", I recommend you to check &lt;a href="https://dev.to/vchiranjeeviak/simplest-hello-world-ever-in-rust--23be"&gt;Simplest Hello World Ever ( in RUST )&lt;/a&gt; first and then check this one. Even if you are new to programming, please check the above-mentioned article up to the &lt;strong&gt;Installation (rustup)&lt;/strong&gt; part because that install rust compiler which is necessary for this article as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  A little bit about me...
&lt;/h2&gt;

&lt;p&gt;Hi reader 🖐. I am Chiranjeevi Tirunagari and I am a software engineer. You can find more about me on &lt;a href="https://www.linkedin.com/in/vchiranjeeviak/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt; and &lt;a href="http://twitter.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. Also, subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;youtube channel&lt;/a&gt; if you are interested in Rust, DevOps and tech in general.&lt;/p&gt;

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

&lt;p&gt;Okay so now that we have everything ( just the rust compiler ) setup, we can start.&lt;/p&gt;

&lt;p&gt;A tradition that is being followed from the ages while learning a new programming language is to write a &lt;strong&gt;Hello World&lt;/strong&gt; program that prints the text "Hello World" in the console or terminal from where you have run it.&lt;/p&gt;

&lt;p&gt;This is usually the easiest program to write in any language as is in the case of Rust. So, let's do that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hello World
&lt;/h2&gt;

&lt;p&gt;To write a program, we need a file. And, this file's name should end with &lt;code&gt;.rs&lt;/code&gt; extension which denotes the rust file. We can create files in multiple ways. But, as a programmer ( new or experienced ), it is our birthright to use terminal commands. The command to create a file is &lt;code&gt;touch &amp;lt;file-name&amp;gt;&lt;/code&gt;. Feel free to name it whatever you want. I am going to name it &lt;strong&gt;hey.rs&lt;/strong&gt; . So, the actual command that I need to use is &lt;code&gt;touch hey.rs&lt;/code&gt;. This will create the file and you can open it using your favourite code editor ( vim/neovim users, I m watching you 😅 ).&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;We will discuss functions in detail in future articles, but let's understand what functions are at the basic level. &lt;strong&gt;Functions&lt;/strong&gt; contain some code and we can run that code by calling these functions. In other words, when we call a function, it means that we are running code living inside that function. Different functions can have different names.&lt;/p&gt;

&lt;p&gt;But, how do we define a function with a name? write code inside it? and call it?&lt;/p&gt;

&lt;h4&gt;
  
  
  Definition
&lt;/h4&gt;

&lt;p&gt;To define a function, we need to use the keyword &lt;code&gt;fn&lt;/code&gt; which denotes function and we specify the name using the syntax &lt;code&gt;fn &amp;lt;function-name&amp;gt;() {}&lt;/code&gt;. We will see about the parenthesis &lt;code&gt;"()"&lt;/code&gt; later in some other article but this is how we define a function.&lt;/p&gt;

&lt;p&gt;Let's define a function with your name. I will do it with my name "Chiranjeevi" 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;chiranjeevi&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;h4&gt;
  
  
  Code
&lt;/h4&gt;

&lt;p&gt;For this article's purpose, we just need to know coding how to print "Hello World". So, let's just do that.&lt;/p&gt;

&lt;p&gt;To print anything to the terminal, the easiest way is to use &lt;code&gt;print!()&lt;/code&gt; or &lt;code&gt;println!()&lt;/code&gt; macros. We will see what macros are and how they are different from functions later. But, for now, what we need to know is that whatever we give inside the parenthesis &lt;code&gt;()&lt;/code&gt; for &lt;code&gt;print!&lt;/code&gt; and &lt;code&gt;println!&lt;/code&gt; is printed to the terminal. The only difference is that &lt;code&gt;println!&lt;/code&gt; shifts the cursor to the next line and &lt;code&gt;print!&lt;/code&gt; keeps it in the same line.&lt;/p&gt;

&lt;p&gt;Now, let's try to print "Hello World" using &lt;code&gt;print!&lt;/code&gt; 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;chiranjeevi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;print!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&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;You might have observed those quotes &lt;code&gt;"&lt;/code&gt; inside parenthesis &lt;code&gt;()&lt;/code&gt; and surrounding our message. So, that's how we do to pass static ( which don't need to change anytime you run ) strings ( spoiler alert: slice / literal ).&lt;/p&gt;

&lt;p&gt;And the lines are terminated with the semi-colon &lt;code&gt;;&lt;/code&gt; .&lt;/p&gt;

&lt;h4&gt;
  
  
  Function Call
&lt;/h4&gt;

&lt;p&gt;To do what we are expecting our function to do which is printing "Hello World", we need to call it. Otherwise, no matter how many functions you write, they are useless and don't do anything unless you call them. We call functions with the syntax &lt;code&gt;&amp;lt;function-name&amp;gt;();&lt;/code&gt;. So, to call the above function, we have to do &lt;code&gt;chiranjeevi()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, where do we do it? Let's see now 👇&lt;/p&gt;

&lt;h3&gt;
  
  
  Main
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;main&lt;/strong&gt; is also a function whose name happens to be main which contains the code that needs to be run when the application starts running. This code inside the main can call other functions too which will run some other code and so on. But the important point here is that the main function is where the code execution starts when an application is started running.&lt;/p&gt;

&lt;p&gt;Whatever "Hello World" program we are gonna write now is also an application (app). So, it needs to have a main function.&lt;/p&gt;

&lt;p&gt;Now you might have understood that we need to call the above-created function inside the main. Let's do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;chiranjeevi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;print!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;chiranjeevi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compilation
&lt;/h3&gt;

&lt;p&gt;To convert the code that we have written in Rust into an &lt;strong&gt;executable file&lt;/strong&gt; which can be run to perform what we have written as code, we need to compile the program. The command to compile a program is &lt;code&gt;rustc &amp;lt;file-name&amp;gt;&lt;/code&gt;. In my case, it is going to be &lt;code&gt;rustc hey.rs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will generate one or two files depending on your OS. In windows, there will be 2 or 1 in Mac/Unix with the name the same as the file you created. These are the binary files of program that we have written. To run these we need to do &lt;code&gt;./hey&lt;/code&gt; in Mac/Unix or &lt;code&gt;.\hey.exe&lt;/code&gt; in windows.&lt;/p&gt;

&lt;p&gt;You will see output something like this 👇&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%2Fscxj4zwub3ys7ujauduj.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%2Fscxj4zwub3ys7ujauduj.png" alt="Output" width="800" height="80"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try to see what the output looks like when &lt;code&gt;println!&lt;/code&gt; is used instead of &lt;code&gt;print!&lt;/code&gt;, you will get the difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Small change...
&lt;/h3&gt;

&lt;p&gt;If you see, we don't actually need another function which runs print line. We can directly write that line inside &lt;code&gt;main&lt;/code&gt; instead of writing in another function and calling that function. There won't be any change in the output, but we can reduce a function call.&lt;/p&gt;

&lt;p&gt;So the final program should look like 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;print!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&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;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Whoaaa!!!! We just completed our "Hello World" without the help of cargo. But, please don't work without cargo when you are dealing with Rust.&lt;/p&gt;

&lt;p&gt;If you find this read-worthy, please don't forget to like, share your thoughts and share this article with your friends and community because that encourages me a lot. Also, don't forget to connect with me on the socials I mentioned in the intro. Subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;&lt;strong&gt;youtube channel&lt;/strong&gt;&lt;/a&gt; as well for similar content if you are a visual learner. Let's meet in the next article, bye-bye 👋 until then.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Simplest Hello World Ever ( in RUST )</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Thu, 30 Mar 2023 05:05:46 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/simplest-hello-world-ever-in-rust--23be</link>
      <guid>https://dev.to/vchiranjeeviak/simplest-hello-world-ever-in-rust--23be</guid>
      <description>

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If this is not your first "Hello World" tutorial, then you probably already know how they start.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Creating a file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Writing main function (in some languages).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Writing print statement with "Hello World".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run it and claps 👏👏👏.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not going to be one of those. That kind of "Hello World" is coming in the next article, but for this one, it is going to be different and simpler.&lt;/p&gt;

&lt;h2&gt;
  
  
  A little bit about me...
&lt;/h2&gt;

&lt;p&gt;Hi reader 🖐. I am Chiranjeevi Tirunagari and I am a software engineer. You can find more about me on &lt;a href="https://www.linkedin.com/in/vchiranjeeviak/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt; and &lt;a href="http://twitter.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. Also, subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;youtube channel&lt;/a&gt; if you are interested in Rust, DevOps and tech in general.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up
&lt;/h2&gt;

&lt;p&gt;If you check any computer system which is being used by a software engineer for a long period, it is more probable to find a JDK ( may god bless if you don't know what it is, it is worth doing a google search if you don't know ) installed already. Because it is tried by almost everyone in tech at least once.&lt;/p&gt;

&lt;p&gt;It is not the case with Rust 🦀 ( this is the last time, I m going to use this emoji, I promise ) though. It is pretty new and only a handful of people got hands-on with it. Hence, you need to install the &lt;strong&gt;rust compiler&lt;/strong&gt; (rustc) to continue from here. If you already have one, feel free to skip this part.&lt;/p&gt;

&lt;p&gt;How do you check if it is already there? Execute &lt;code&gt;rustc -V&lt;/code&gt; in your terminal or command prompt. If you get some version numbers, you are good to go. If you see an error then you need to install it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation (rustup)
&lt;/h3&gt;

&lt;p&gt;If you go here: &lt;a href="https://rustup.rs/" rel="noopener noreferrer"&gt;https://rustup.rs/&lt;/a&gt; , you will see the command 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;curl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--proto&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'=https'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--tlsv1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-sSf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;https://sh.rustup.rs&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sh&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute this command in your terminal or command prompt which will install &lt;strong&gt;rustup&lt;/strong&gt; which is an installer for rust compiler and other rust tools.&lt;/p&gt;

&lt;p&gt;After this, you should be able to get your compiler version using &lt;code&gt;rustc -V&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cargo
&lt;/h2&gt;

&lt;p&gt;rustup installs something called &lt;strong&gt;cargo&lt;/strong&gt; along with rustc. To put it in simple terms, cargo is a &lt;strong&gt;package manager ( helps manage packages also called crates in rust )&lt;/strong&gt; and a &lt;strong&gt;build tool ( helps convert rust code to binary )&lt;/strong&gt; for rust. More about the cargo on some other day. Let's concentrate on the goal of this article which is 👇.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hello World
&lt;/h2&gt;

&lt;p&gt;Now that we have our setup ready, let's start with our actual motive. For that, we need to create a project. To do that, we have a cargo command &lt;code&gt;cargo new &amp;lt;project name&amp;gt;&lt;/code&gt;. Use this command in the terminal to create the project with the name you want. I am creating a project with the name &lt;strong&gt;hello&lt;/strong&gt;. So, my command looks like &lt;code&gt;cargo new hello&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you do &lt;code&gt;ls&lt;/code&gt; in unix/Mac or &lt;code&gt;dir&lt;/code&gt; in windows, you should see a directory with the name &lt;strong&gt;hello&lt;/strong&gt; or whatever name you have given. This directory contains some files which we will discuss in future articles.&lt;/p&gt;

&lt;p&gt;For now, let's change to that directory using &lt;code&gt;cd&lt;/code&gt; command. I am doing &lt;code&gt;cd hello&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now run &lt;code&gt;cargo run&lt;/code&gt; in the terminal and Tada!! ✨✨&lt;/p&gt;

&lt;p&gt;I hope you have seen "Hello, world!" in the terminal. The code for this is written inside &lt;code&gt;main.rs&lt;/code&gt; file inside &lt;code&gt;src&lt;/code&gt; sub-directory. When we run &lt;code&gt;cargo run&lt;/code&gt;, cargo automatically compiles all the rust files in the project and starts running the project. Since, we have only one rust file which prints "Hello, world!", it did just that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thoughts...
&lt;/h2&gt;

&lt;p&gt;We can do the same thing without using cargo by writing a rust file by ourselves without any project and writing the basic code that is required and compiling it and then running it. The reason why I started with cargo is that it is such a useful and powerful tool, so to show the taste of it. The only time I did everything manually without cargo is when I wrote my first hello world ( I m not experienced in rust, lol 😅 ) in Rust. But, it is good to know how things work. So, we will do it in the next article.&lt;/p&gt;

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

&lt;p&gt;If you find this read-worthy, please don't forget to like, share your thoughts and share this article with your friends and community because that encourages me a lot. Also, don't forget to connect with me on the socials I mentioned in the intro. Subscribe to my &lt;a href="https://www.youtube.com/@chiranjeevi_tirunagari" rel="noopener noreferrer"&gt;youtube channel&lt;/a&gt; as well for similar content if you are a visual learner. Let's meet in the next article, bye-bye 👋 until then.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>rust</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Essential prerequisites for programming</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Mon, 19 Dec 2022 16:35:46 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/essential-prerequisites-for-programming-366</link>
      <guid>https://dev.to/vchiranjeeviak/essential-prerequisites-for-programming-366</guid>
      <description>&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;When starting to code, most beginners do the same mistake of diving right into it without having a context. Whenever we are starting something, we should start from a context just like this article that gives us a high-level overview, which helps us in deciding if we want to continue on that path or if it is something that doesn't excite us.&lt;/p&gt;

&lt;p&gt;By the way, I am &lt;strong&gt;Chiranjeevi Tirunagari&lt;/strong&gt;, working as a Product Engineer at Oslash. When I started to code, I didn't have much context on anything related to coding for many reasons. I did it anyway since it was my major in college. But, having the right context could have made me a better coder/programmer/engineer than I was, a lot faster.&lt;/p&gt;

&lt;p&gt;The motive of this article is to provide the right context before someone starts their coding journey for which I am going touch on &lt;strong&gt;4&lt;/strong&gt; topics which are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What does exactly programming mean in the context of computers?&lt;/li&gt;
&lt;li&gt;Code vs Program vs Software.&lt;/li&gt;
&lt;li&gt;The basic architecture of computers.&lt;/li&gt;
&lt;li&gt;How does program execution work?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. What does exactly programming mean in the context of computers?
&lt;/h2&gt;

&lt;p&gt;Telling a computer to do a specific task is called programming. Yes, that's exactly what it is. It can be anything from asking a computer to "tell an answer to a complex arithmetic operation" or "send some data or a set of files to another computer over the internet, also called &lt;strong&gt;web development&lt;/strong&gt;" or "analyze past data and predict the future, also called &lt;strong&gt;machine learning&lt;/strong&gt;". The task can be anything that is done by the computer, but one thing is common, being a programmer it is our task to tell the computer how to do that task, and that is called &lt;strong&gt;programming&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since the computer is a machine that runs on electricity, all that is understood by a computer is a 0 or a 1 (&lt;strong&gt;binary&lt;/strong&gt; language) under the hood, we need to program in such a way that the computer understands what we are programming. So, do we need to program in binary? Fortunately &lt;strong&gt;no&lt;/strong&gt;. There are two things called &lt;strong&gt;compiler/interpreter&lt;/strong&gt; (converts programming language code into machine level code) and &lt;strong&gt;operating system&lt;/strong&gt; (converts machine level code into binary) which we will talk about later which do all the hard work to make the computer understand our program written in a language that we can understand by translating it into binary.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Code vs Program vs Software.
&lt;/h2&gt;

&lt;p&gt;These are the words that people often use interchangeably even though they are not the same. The difference between these 3 is not something straightforward. It depends on the person's perspective on these words. Here, I will try to convey my perspective on all three of these.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converting a word from one language to another is what actually coding is. A &lt;strong&gt;code&lt;/strong&gt; in terms of computers is something we write in a language we understand and can be converted into something a computer can also understand. A code can exist without a purpose. This can be understood by the following example. Telling a computer to ask the user to tell their name is a code. Just taking the name from the user is not at all useful from any angle. But still, it is a code because we can tell the computer to do that even though it is not useful.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;program&lt;/strong&gt; is something that is written to serve a purpose. All programs consist of code but do meaningful things. Considering the previous example again. If addition to taking the name from the user, if we are greeting the user with their name, then it is something meaningful. Here the purpose is to greet the user and not just do something that is useless. All programs are coded, but not all that are coded are programs.&lt;/li&gt;
&lt;li&gt;Finally, the &lt;strong&gt;software&lt;/strong&gt; is a collection of programs that aims to solve a problem. Let us consider an example here too. The program to greet a user is meaningful but not solving any problem. Nobody uses something just to greet them. If we have a set of programs that are helping the users pay their bills, of which the greeting program is also a part, then this set of programs can be called software as it is solving a problem. An operating system that we saw before is also software that solves the problem of dealing with the low-level implementation of other software executions to make computers understand them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. The basic architecture of computers.
&lt;/h2&gt;

&lt;p&gt;The idea here is to show how the most important parts of a computer are connected together. The parts here don't mean devices like keyboards, mice, etc. A computer can still work without them and do the task it is supposed to do (alexa, smart homes, etc). Here the intention is to show the things which are absolutely necessary for every computer to function.&lt;br&gt;
The two most important parts of a computer are &lt;strong&gt;CPU&lt;/strong&gt; and &lt;strong&gt;memory&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;CPU&lt;/strong&gt; is a chip that actually performs the execution of code/program/software on it. It is usually referred to as the brain of the computer.&lt;/li&gt;
&lt;li&gt;Though the CPU is what performs the execution, there should be programs existing somewhere to make it available to the CPU to run and &lt;strong&gt;memory&lt;/strong&gt; is what does that for the CPU. The memory stores the programs and passes them to the CPU whenever required to be run.&lt;/li&gt;
&lt;/ul&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%2Fgtaykso1ngxb7w5lfrqa.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%2Fgtaykso1ngxb7w5lfrqa.png" alt="CPU and memory" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. How does program execution work?
&lt;/h2&gt;

&lt;p&gt;All that is there above this point just provides context for this section. This is the most important part of this article at least from my perspective.&lt;br&gt;
As seen in the previous section, memory is one of the most important parts of a computer. There are 2 types of memory. They are &lt;strong&gt;primary&lt;/strong&gt; memory and &lt;strong&gt;secondary&lt;/strong&gt; memory. Primary memory devices are designed in such a way that the exchange of data between primary memory devices and CPU is much faster when compared to secondary memory devices.&lt;br&gt;
But the primary memory is volatile in nature which means that once the power supply is off to the computer, the data in primary memory is gone and becomes empty whereas the data in secondary memory is non-volatile and stays even if the power supply is off.&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%2Fvmuynaahr91b9cpwusrm.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%2Fvmuynaahr91b9cpwusrm.png" alt="Memory types" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the reason why the programs and software are stored in the secondary memory initially. When these programs are executed by the CPU, the code for the program is loaded into the primary memory and executed line by line. &lt;/p&gt;

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

&lt;p&gt;What I have mentioned here is just the outline of what actually happens. There are lot more low-level things like PC, MAR, and MDR involved in this process and maybe they are topics for some other day. But, this is the bare minimum that we need before starting to learn to program. This feels very basic and intuitive if you already know it. But, most people don't know these basics and jump right into coding and struggle to understand.&lt;/p&gt;

&lt;p&gt;That's it for this one. If you find this helpful, do share it with others. Please like and comment if you have any suggestions.&lt;/p&gt;

&lt;p&gt;Make sure you follow me on Youtube and other social media platforms for more such content.&lt;br&gt;
&lt;a href="https://www.youtube.com/channel/UCFAeWQJ4JD3QpdenG2VgoIA" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/vchiranjeeviak/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Go - Defer, Panic and Recover</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Wed, 28 Sep 2022 17:48:41 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/go-defer-panic-and-recover-58km</link>
      <guid>https://dev.to/vchiranjeeviak/go-defer-panic-and-recover-58km</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;With the previous part (6), we finished basics in &lt;strong&gt;Go&lt;/strong&gt;. Most of the topics covered in those parts are common in most programming languages. By hearing the topics covered in previous parts, we can relate them with other programming languages if we know any. The topics we are going to discuss in this part are &lt;strong&gt;Defer&lt;/strong&gt;, &lt;strong&gt;Panic&lt;/strong&gt; and &lt;strong&gt;Recover&lt;/strong&gt;. I am not sure about the people reading this, but I haven't heard them before learning &lt;strong&gt;Go&lt;/strong&gt;. But panic and recover are available in other programming languages as well but with different names. We will talk more about this later. Let's get into this one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defer
&lt;/h2&gt;

&lt;p&gt;The functionality of &lt;strong&gt;defer&lt;/strong&gt; is simple. It just delays the execution of a line for some time. Okay....How much time? It delays until all other lines in the function in which the defer line is residing are finished executing.&lt;/p&gt;

&lt;p&gt;The syntax to use &lt;strong&gt;defer&lt;/strong&gt; is &lt;code&gt;defer &amp;lt;any valid line&amp;gt;&lt;/code&gt;.'&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic example to illustrate defer functionality
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;defer&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;"First line code, but comes last in output"&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;"Second line in code"&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;"Last line in code"&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;Output&lt;/strong&gt;:&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%2Ft7xvs07ehzhnc1k8dkbz.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%2Ft7xvs07ehzhnc1k8dkbz.png" alt="Output1" width="672" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see the output, the line having &lt;strong&gt;defer&lt;/strong&gt; printed last even though it is the first line in the function. That is what defer does. It delays the specified line of code until all other lines are executed in that function scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to illustrate multiple defer statements in same function
&lt;/h3&gt;

&lt;p&gt;We saw how &lt;strong&gt;defer&lt;/strong&gt; works. What if there are more than one defer statements in a function? Which line executes first and which one executes last?&lt;/p&gt;

&lt;p&gt;To answer this, we need to understand what actually is happening when the defer line is being delayed. So, when we use &lt;strong&gt;defer&lt;/strong&gt; before a line, when the control of program comes there, it pushes the defer line into a stack to store it temporarily. When the function execution comes to end, before it finishes, the lines pushed into the stack are popped out and executed one by one.&lt;/p&gt;

&lt;p&gt;If you are aware how a stack works, it is a Last-In-First-Out data structure. Meaning, whichever item is pushed last into it, comes out first. So, in our context, the line which gets pushed into stack last, will get popped out first and executed first. See below code and output to see it practically.&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;defer&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;"First line"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// goes first into defer stack&lt;/span&gt;
    &lt;span class="k"&gt;defer&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;"Second line"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// goes last into defer stack&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;"Third line"&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;"Last line"&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;Output&lt;/strong&gt;:&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%2F6ayil6q7l8wuz67ixc25.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%2F6ayil6q7l8wuz67ixc25.png" alt="Output2" width="677" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above program, "First line" goes first into defer stack and "Second line" goes last. But in the output, after "Third line" and "Last line" got executed, "Second line" is executed first because it is the last line pushed into the stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to illustrate the variable values evaluation at defer line
&lt;/h3&gt;

&lt;p&gt;I know the heading of this section is confusing. Even I don't understand what I have written. But just bear with me for this section.&lt;/p&gt;

&lt;p&gt;Consider this situation where there is a variable in a function and that is being printed in a line. But there is a &lt;strong&gt;defer&lt;/strong&gt; keyword before that line. After that line, we are changing the value of that variable. So, what value does the defer line prints? Initial value or Changed value? The line is using variable before changing the value. But also the line is being executed after changing the value. So, what value does it print?&lt;/p&gt;

&lt;p&gt;The simple answer is when a defer line is pushed into the defer stack, it pushed along with the values of variables it is using but not the actual variable itself. So, no matter what happens to that variable after the line, the initial value is already pushed into defer stack along with defer line. Hence, it prints initial value.&lt;/p&gt;

&lt;p&gt;I hope you understand the heading for this section a bit now.&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="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;testVar&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
    &lt;span class="k"&gt;defer&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 value is: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;testVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// should print 10 because the value is already evaluated&lt;/span&gt;
    &lt;span class="n"&gt;testVar&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&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%2Fwwgxqr860gi3tk175zt0.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%2Fwwgxqr860gi3tk175zt0.png" alt="Output3" width="624" height="38"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Panic
&lt;/h2&gt;

&lt;p&gt;This word is something unique to this &lt;strong&gt;Go&lt;/strong&gt; language but the concept is not. We call these as &lt;strong&gt;exceptions&lt;/strong&gt; in other languages. Yeah so, basically if any runtime error happens while executing a code, it creates &lt;strong&gt;panic&lt;/strong&gt; in the execution and the executions terminates abruptly.&lt;/p&gt;

&lt;p&gt;If you are already familiar with runtime errors, then fine. If not, just consider them as unexpected errors those may occur while program is running. A simple such error can be occurred during dividing two numbers. Consider a situation where we are taking input from user for two number and divide one with the other. If the user gives denominator as 0 and we divide with 0, it throws a runtime error. This creates panic in the execution and execution stops no matter if there is code after it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to create panic by dividing with 0
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&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;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// input 1&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;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// input 2&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;   &lt;span class="c"&gt;// division&lt;/span&gt;

    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&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;Output&lt;/strong&gt;:&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%2F05d1awkgxq807nkywy6w.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%2F05d1awkgxq807nkywy6w.png" alt="Output4" width="764" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, since I have give second input as 0 and it divides with 0, the execution panics. Even though there is a print statement after that line, it won't execute it.&lt;/p&gt;

&lt;p&gt;So, can a runtime error only create panic? No, we can create panic on our own using &lt;strong&gt;panic&lt;/strong&gt; keyword. This is just like throwing errors in other languages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to illustrate panic keyword
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;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="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;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;"This line prints fine since it is before panic"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Panicking just for fun"&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;"This line won't get printed since it is after panic"&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;Output&lt;/strong&gt;:&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%2F5siyjk1dnqy3qbfuq1uv.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%2F5siyjk1dnqy3qbfuq1uv.png" alt="Output5" width="773" height="88"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why are we creating panic for fun? We definitely don't do it for fun. But, I am not getting any idea on top of my head to justify that we don't do it for fun. I will write another article if I find one. Or you can comment down if you have ever thrown an error in code in a serious use-case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recover
&lt;/h2&gt;

&lt;p&gt;In real world code bases, there are multiple functions running one after the other. If we some panic is being created in one function stops entire code execution, the next functions won't even run. We don't want it in most situations. For that we need a way to avoid stoppage of code execution even if panic happens.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;recover&lt;/strong&gt; comes into picture. Just for comparison, this can be mapped to &lt;strong&gt;catch&lt;/strong&gt; in other languages, but not exactly same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;recover&lt;/strong&gt; is a function which returns why the panic is created if there is one or returns &lt;strong&gt;nil&lt;/strong&gt; if no panic is there. We have to handle the situation depending on the return value of &lt;strong&gt;return&lt;/strong&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to illustrate how not to use recover
&lt;/h3&gt;

&lt;p&gt;The motive of this program is to show how beginners may use recover in a wrong way. The recover in this program is totally  useless.&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="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;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;"This line prints fine since it is before panic"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You thought recover under me can help you? Hahaha!!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;recover&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;a&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After recovery"&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;Output&lt;/strong&gt;&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%2F7qa9kxkh3xqebhyu57pg.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%2F7qa9kxkh3xqebhyu57pg.png" alt="Output6" width="800" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since, the nothing after panic can run and execution stops there, recover is useless in the above case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to illustrate correct usage of recover
&lt;/h3&gt;

&lt;p&gt;Okay fine, if we can't use recover after panic, there is only chance to use it before panic. But before panic, recover returns &lt;strong&gt;nil&lt;/strong&gt; since there is won't be any panic at that time which is also of no use. Then how do we actually handle panic? This is where &lt;strong&gt;defer&lt;/strong&gt; comes in handy.&lt;/p&gt;

&lt;p&gt;Write the recover and other panic handling code inside a function and call the function before panic line with defer keyword. So, technically it is before panic, so executes and also executes after panic so that &lt;strong&gt;recover&lt;/strong&gt; does not return &lt;strong&gt;nil&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;recover&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;a&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"There is a panic, no worries, we handled it."&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;panicking&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I am afraid, I might get caught!"&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;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Just there to show that program still continues"&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;panicking&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;random&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;Output&lt;/strong&gt;:&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%2Fmuojmfvakrea68c4lglo.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%2Fmuojmfvakrea68c4lglo.png" alt="Output7" width="786" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;I hope you liked it. Next part will be on &lt;strong&gt;interfaces&lt;/strong&gt; in go.&lt;/p&gt;

&lt;p&gt;Do follow me to see them. Make sure you like and share if you felt this good.&lt;/p&gt;

&lt;p&gt;Let's connect on: &lt;a href="https://twitter.com/VChiranjeeviAK" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;a href="https://www.linkedin.com/in/chiranjeevi-tirunagari/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;a href="https://www.showwcase.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Showwcase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>go</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Go - Use-cases of non-primitive datatypes</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Wed, 21 Sep 2022 17:43:32 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/go-use-cases-of-non-primitive-datatypes-1m3h</link>
      <guid>https://dev.to/vchiranjeeviak/go-use-cases-of-non-primitive-datatypes-1m3h</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome to the part 7 of &lt;strong&gt;Go&lt;/strong&gt; series. In the previous part (part 6): &lt;a href="https://dev.to/vchiranjeeviak/go-maps-structs-485c"&gt;https://dev.to/vchiranjeeviak/go-maps-structs-485c&lt;/a&gt;, we saw what &lt;strong&gt;Maps&lt;/strong&gt; and &lt;strong&gt;Structs&lt;/strong&gt; are and how different operations like creating, adding, deleting etc can be done on them. In this one, lets see how we can iterate through them and the use-cases of these non-primitive datatypes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterating through non-primitive datatypes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt;, &lt;strong&gt;Slices&lt;/strong&gt; and &lt;strong&gt;Maps&lt;/strong&gt; are some sort of collection of some similar type of values. There might be many situation where we want to go through each of these collections, do some computation or update the values. No matter what we do, going through the entire collection is the point to note here. We use loops to do this. Revisit this &lt;a href="https://dev.to/vchiranjeeviak/go-conditionals-and-loops-5f1g"&gt;https://dev.to/vchiranjeeviak/go-conditionals-and-loops-5f1g&lt;/a&gt; loops article if haven't already for more context on loops.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using normal for loop to iterate
&lt;/h3&gt;

&lt;p&gt;Run a "for" loop from 0 until length of collection and do any operation on each item inside collection using index operator.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to print all items in a slice in new line
&lt;/h4&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="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;arr&lt;/span&gt; &lt;span class="o"&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="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"three"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"four"&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;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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&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%2Fswlj6mqabokujaggo05p.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%2Fswlj6mqabokujaggo05p.png" alt="Output1" width="364" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can iterate through an array in the similar way. But in case of maps, this way of iteration is possible only if the keys in the map are of the &lt;code&gt;int&lt;/code&gt; type. If it is not of &lt;code&gt;int&lt;/code&gt; type, we can't use integer indices inside index operator.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to add all items in an array
&lt;/h4&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="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;arr&lt;/span&gt; &lt;span class="o"&gt;:=&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="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="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="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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="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;"Sum = "&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&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%2F6foffuvpbnekbpxeaxb1.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%2F6foffuvpbnekbpxeaxb1.png" alt="Output2" width="166" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using for-each loop to iterate
&lt;/h3&gt;

&lt;p&gt;We now know why iterating using normal "for" loop is not always possible in case of maps. It is because maps don't guarantee that they always have &lt;code&gt;int&lt;/code&gt; keys and while iterating using a normal "for" loop give us only integer value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;for-each&lt;/strong&gt; loop lets us overcome this issue. If we use for-each loop with array or slice, it gives us 2 values, which are &lt;strong&gt;position and value&lt;/strong&gt;. If we use for-each loop with maps, it gives us 2 values again, which are &lt;strong&gt;key and value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The syntax to use it is &lt;code&gt;for &amp;lt;pos/key&amp;gt;, &amp;lt;value&amp;gt; := range collection { }&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to print key and value combined as a string in a string-string map
&lt;/h4&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="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;mapp&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;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mapp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;
    &lt;span class="n"&gt;mapp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"2"&lt;/span&gt;
    &lt;span class="n"&gt;mapp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"three"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"3"&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;mapp&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;value&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;Output&lt;/strong&gt;:&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%2Ffqos3tf5sodoz9cyuflk.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%2Ffqos3tf5sodoz9cyuflk.png" alt="Output3" width="370" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to print position and value in a slice side by side
&lt;/h4&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="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;arr&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;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;52&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;745&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;13&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;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&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;arr&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;"%v -&amp;gt; %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;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&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;Output&lt;/strong&gt;:&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%2Fxnr1rq2ing63nofdiyzp.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%2Fxnr1rq2ing63nofdiyzp.png" alt="Output4" width="364" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how we can iterate through any collection like arrays, slices and maps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real world scenarios where non-primitive datatypes are useful
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When we want to keep track of some items as a collection and we know the upper limit of the number of these items or want to limit the items to a particular number, then &lt;strong&gt;arrays&lt;/strong&gt; are useful. I know it almost feels like a definition. Let's say we are keeping track of students joining in a class. Obviously every class is limited and let's consider the limit as 60. To store names/numbers of these students, we will need an array of max size 60.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, consider this situation where we need to keep track of voters in a country like we did for students in above example. Number of voters is not limited and not constant. So, we can't keep a cap on the value. This is a situation where we need a &lt;strong&gt;slice&lt;/strong&gt;. When there is no limit required or known, to store items as a collection, slice is the way to go.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can use &lt;strong&gt;maps&lt;/strong&gt; wherever we are connecting two entities as a whole item. But the condition is that at least one of the two entities should be unique among all items. The unique entity is called as key. An item can be uniquely identified by these keys. A real world case can be keeping track of credit card number along with user name. Here, the relation we chose is the credit card and it's owner. So, credit card number is the key and owner name is the value. We can't represent a relation of a person and his credit cards. It is because, a person name can't be unique for all items. But in first case, a credit card number will always be unique in any pair.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A complex example of non-primitive datatypes and iteration
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// defining a struct&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;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;register&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;map&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;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// map of int key and person value type&lt;/span&gt;
    &lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;5463715&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Ram"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Ayodhya"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2385928&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Sita"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mithila"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1432545&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"chiranjeevi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Bangalore"&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;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;person&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;register&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;"%v having id %v lives in %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;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;city&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;Output&lt;/strong&gt;:&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%2Fj7hnds7dk1nclyzi33i4.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%2Fj7hnds7dk1nclyzi33i4.png" alt="Output5" width="790" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;That's it for this one. In the next one, we will see what &lt;strong&gt;defer&lt;/strong&gt;, &lt;strong&gt;panic&lt;/strong&gt;, &lt;strong&gt;recover&lt;/strong&gt; are. Let's meet there.&lt;/p&gt;

&lt;p&gt;More on Go lang will be coming soon and do follow me to see them. Make sure you like and share if you felt this good.&lt;/p&gt;

&lt;p&gt;Let's connect on: &lt;a href="https://twitter.com/VChiranjeeviAK" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;a href="https://www.linkedin.com/in/chiranjeevi-tirunagari/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;a href="https://www.showwcase.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Showwcase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
      <category>devops</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Go - Maps and Structs</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Wed, 07 Sep 2022 14:43:55 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/go-maps-structs-485c</link>
      <guid>https://dev.to/vchiranjeeviak/go-maps-structs-485c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome back to the Part - 6 of the Go series. In the last one, we saw two of the non-primitive datatypes (1. Arrays, 2. Slices) in Go. Do check it out if you missed it: &lt;a href="https://dev.to/vchiranjeeviak/go-arrays-and-slices-52pi"&gt;https://dev.to/vchiranjeeviak/go-arrays-and-slices-52pi&lt;/a&gt;. In this one, let's see about &lt;strong&gt;3. Maps&lt;/strong&gt; and &lt;strong&gt;4. Structs&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Maps
&lt;/h2&gt;

&lt;p&gt;We saw how arrays and slices are a collection of other primitive datatype items. Maps are also a collection of items, but here the items are not just of primitive types, they are &lt;code&gt;(key, value)&lt;/code&gt; pairs. And a key can be any primitive datatype item and same with value. But, the types should be consistent for all &lt;code&gt;(key, value)&lt;/code&gt; pairs in the map.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: A &lt;strong&gt;key&lt;/strong&gt; should be unique for each pair. In other words, no two pairs should have same &lt;strong&gt;key&lt;/strong&gt;. By this assumption, we can uniquely identify a pair using its &lt;strong&gt;key&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declare and Initialising a map
&lt;/h3&gt;

&lt;p&gt;We use &lt;code&gt;make&lt;/code&gt; function to create a map. &lt;code&gt;map[primitive]primitive&lt;/code&gt; is a datatype. We are basically trying to say is that "make a map of this type".&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to declare and initialise a map
&lt;/h4&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="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;// Declaring a map&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;map1&lt;/span&gt; &lt;span class="k"&gt;map&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;string&lt;/span&gt; &lt;span class="c"&gt;// A map in which key is of int type, value is of string type&lt;/span&gt;
        &lt;span class="c"&gt;// Initialising an empty map&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;map2&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;map&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;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// An empty map in which key is of string type, value is of int type&lt;/span&gt;
        &lt;span class="c"&gt;// Initialising a non-empty map&lt;/span&gt;
    &lt;span class="n"&gt;map3&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// A map in which key is of float type, value is of int type&lt;/span&gt;
        &lt;span class="m"&gt;0.5&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="m"&gt;1.5&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="m"&gt;2.5&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map3&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;Output&lt;/strong&gt;:&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%2F96t1df2sy7ot5emtrpld.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%2F96t1df2sy7ot5emtrpld.png" alt="Output 1" width="378" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check the type of the map on your own using &lt;code&gt;%T&lt;/code&gt; in &lt;code&gt;Printf&lt;/code&gt; and you will realise why we are using that particular notation to create a map.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding and deleting items in a map
&lt;/h3&gt;

&lt;p&gt;Adding a &lt;code&gt;(key, value)&lt;/code&gt; pair into a map is straight forward. Using index operator, we give the new &lt;code&gt;key&lt;/code&gt; into index and assign new &lt;code&gt;value&lt;/code&gt; to it. It automatically create a new pair.&lt;/p&gt;

&lt;p&gt;For deleting a pair, we use &lt;code&gt;delete&lt;/code&gt; function which takes the &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;key&lt;/code&gt; as input and deletes the pair with that particular key.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to add and delete a pair in map
&lt;/h4&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="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;// Initialising a map&lt;/span&gt;
    &lt;span class="n"&gt;map1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&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;string&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="s"&gt;"string1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"string2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Map initially = "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;map1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// Adding a new pair&lt;/span&gt;
    &lt;span class="n"&gt;map1&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="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"string3"&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;"Map after adding (3, string3) = "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;map1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// Deleting a pair with key 1&lt;/span&gt;
    &lt;span class="nb"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map1&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="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;"Map after deleting pair with key 1 = "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;map1&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;Output&lt;/strong&gt;:&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%2Fgy3ziin15a4192slhht3.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%2Fgy3ziin15a4192slhht3.png" alt="Output 2" width="800" height="96"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Structs
&lt;/h2&gt;

&lt;p&gt;Struct is a bit different from other non-primitive datatypes. It is not a collection of something like arrays, slices or maps. A &lt;strong&gt;struct&lt;/strong&gt; is a complex datatype which is a fusion of other datatypes. It is a user-defined datatype which is a combination of other datatypes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining a struct type
&lt;/h3&gt;

&lt;p&gt;As I said earlier, it is a user-defined datatype. So, we need to define this type using the syntax &lt;code&gt;type &amp;lt;name&amp;gt; struct { }&lt;/code&gt;. Inside this, we give other datatypes with their names using syntax &lt;code&gt;&amp;lt;name&amp;gt; &amp;lt;datatype&amp;gt;&lt;/code&gt;. This is how we create the fusion.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to declare and initialise a struct type and create a variable of that type and print it
&lt;/h4&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;// Creating a struct1 type struct&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;struct1&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;nestedVar1&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;nestedVar2&lt;/span&gt; &lt;span class="kt"&gt;int&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="c"&gt;// Declaring a variable of type struct1&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;structVar1&lt;/span&gt; &lt;span class="n"&gt;struct1&lt;/span&gt;
    &lt;span class="c"&gt;// Initialising a variable of type struct2&lt;/span&gt;
    &lt;span class="n"&gt;structVar2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;struct1&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"string1"&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="c"&gt;// Assigning value to structVar1&lt;/span&gt;
    &lt;span class="n"&gt;structVar1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct1&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"string2"&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="c"&gt;// Printing both&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;structVar1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;structVar2&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;Output&lt;/strong&gt;:&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%2F46bq3xfapylrqjiyo3c1.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%2F46bq3xfapylrqjiyo3c1.png" alt="Output 3" width="213" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessing nested variable in a struct
&lt;/h3&gt;

&lt;p&gt;In the above case, how can we access the string part of the struct1? We can do that using &lt;code&gt;.&lt;/code&gt; . &lt;code&gt;structname.nestedVar&lt;/code&gt; gives that particular value.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to create a struct variable and accessing a nested variable
&lt;/h4&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;// Creating a struct1 type struct&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;struct1&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;nestedVar1&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;nestedVar2&lt;/span&gt; &lt;span class="kt"&gt;int&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="c"&gt;//Initialising a struct1 type variable&lt;/span&gt;
    &lt;span class="n"&gt;structVar1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;struct1&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"string2"&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="c"&gt;// Printing nested variable&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;structVar1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nestedVar1&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;Output&lt;/strong&gt;:&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%2Fna3bmhtlfhgmeis598pj.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%2Fna3bmhtlfhgmeis598pj.png" alt="Output 4" width="169" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;That's it for this one. In the next part, lets see the use-cases of each of these non-primitive datatypes. Also, lets cover iterating through each of these datatypes which is very important.&lt;/p&gt;

&lt;p&gt;More on Go lang will be coming soon and do follow me to see them. Make sure you like and share if you felt this good.&lt;/p&gt;

&lt;p&gt;Let's connect on: &lt;a href="https://twitter.com/VChiranjeeviAK" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;a href="https://www.linkedin.com/in/chiranjeevi-tirunagari/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;a href="https://www.showwcase.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Showwcase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
      <category>devops</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Go - Arrays and Slices</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Sat, 03 Sep 2022 09:47:37 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/go-arrays-and-slices-52pi</link>
      <guid>https://dev.to/vchiranjeeviak/go-arrays-and-slices-52pi</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome back to the &lt;strong&gt;Part 5&lt;/strong&gt; of Go lang series. In &lt;strong&gt;Part 2&lt;/strong&gt;: &lt;a href="https://dev.to/vchiranjeeviak/go-variables-constants-and-datatypes-502j"&gt;https://dev.to/vchiranjeeviak/go-variables-constants-and-datatypes-502j&lt;/a&gt;, we saw what datatypes are and how many types are there in Go. Those are primitive datatypes meaning that they are basic. In this one, let's see about &lt;strong&gt;arrays&lt;/strong&gt; and &lt;strong&gt;slices&lt;/strong&gt; which are &lt;strong&gt;non-primitive datatypes&lt;/strong&gt; in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Primitive datatypes
&lt;/h2&gt;

&lt;p&gt;A datatype which is a group of variables of any &lt;strong&gt;primitive&lt;/strong&gt; or &lt;strong&gt;non-primitive&lt;/strong&gt; datatypes is called &lt;strong&gt;non-primitive&lt;/strong&gt; datatype. Revisit this line after reading below &lt;strong&gt;non-primitive&lt;/strong&gt; datatypes. You will get a clear picture of this line then.&lt;/p&gt;

&lt;p&gt;There are &lt;strong&gt;4&lt;/strong&gt; non-primitive datatypes in Go. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Arrays.&lt;/li&gt;
&lt;li&gt;Slices.&lt;/li&gt;
&lt;li&gt;Maps.&lt;/li&gt;
&lt;li&gt;Structs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see &lt;strong&gt;maps&lt;/strong&gt; and &lt;strong&gt;structs&lt;/strong&gt; in the next article.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Arrays
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;array&lt;/strong&gt; is a collection or group of values where each value belongs to the &lt;strong&gt;same&lt;/strong&gt; type. In other words, all variables or items present in an array should be of same datatype.&lt;/p&gt;

&lt;p&gt;An array in Go has a &lt;strong&gt;fixed size&lt;/strong&gt; which is specified during the time of declaration or initialisation. And, its size is fixed can't be modified during program execution.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to illustrate declaring and initialising arrays
&lt;/h4&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;arr1&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="kt"&gt;int&lt;/span&gt; &lt;span class="c"&gt;// Declaring an array of size 10&lt;/span&gt;
    &lt;span class="n"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;:=&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="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;// Initialising an array of size 10 with {1, 2, 3, 4, 5} values.&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;"This is array 1: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr1&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;"This is array 2: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr2&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;Output&lt;/strong&gt;:&lt;br&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%2Fjf6qod1gyfodg7764gxq.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%2Fjf6qod1gyfodg7764gxq.png" alt="Output" width="800" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since, we haven't given any values in arr1, it is taking all values as default values, which is 0 in case of int. In arr2, the first 5 values are given by us and remaining 5 are default values.&lt;/p&gt;
&lt;h4&gt;
  
  
  Program to add an item at 6th position in an array of size 10
&lt;/h4&gt;

&lt;p&gt;We can specify a particular position of an array using &lt;strong&gt;index&lt;/strong&gt; (&lt;code&gt;[]&lt;/code&gt;) operator. Inside these &lt;code&gt;[]&lt;/code&gt;, we give the index number. &lt;code&gt;Index number = Position - 1&lt;/code&gt;. So, to specify 6th position of an array, we use &lt;code&gt;[5]&lt;/code&gt; according to above expression. We use this index operator right beside the array name.&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="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;arr&lt;/span&gt; &lt;span class="o"&gt;:=&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="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;// Initialising array&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="n"&gt;arr&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="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt; &lt;span class="c"&gt;// Adding an item at position 6, a.k.a index 5&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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;Output&lt;/strong&gt;:&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%2Fhxltfec1u2kzxghs13o4.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%2Fhxltfec1u2kzxghs13o4.png" alt="Output" width="423" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A negative point of an array is that its size is fixed and cannot grow or shrink automatically depending on the situation. For example, in the above array of size 10, you can't add a 11th item into it. It throws an error. So, we need to know the maximum length of the array that we require in that program.&lt;/p&gt;

&lt;p&gt;What if it is not possible to know the maximum length of array in prior? What if we want our array to grow or shrink according to the needs? That is where &lt;strong&gt;slices&lt;/strong&gt; come in handy.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Slices
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;slice&lt;/strong&gt; is very similar to an array. The only difference between a slice and an array is that there is &lt;strong&gt;no fixed size&lt;/strong&gt; for a slice. Its size can grow or shrink depending on the situation.&lt;/p&gt;

&lt;p&gt;Declaration or initialisation of a slice is similar to an array except that we don't specify any size like we do in the case of an array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to declare and initialise a slice
&lt;/h4&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;slice1&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="c"&gt;// Declaring a slice of type string&lt;/span&gt;
    &lt;span class="n"&gt;slice2&lt;/span&gt; &lt;span class="o"&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="s"&gt;"chiranjeevi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"golang"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice2&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;Output&lt;/strong&gt;:&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%2Fn896q4g76t7y7oh7g0v6.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%2Fn896q4g76t7y7oh7g0v6.png" alt="Output" width="426" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to add a new string at the end of a slice
&lt;/h4&gt;

&lt;p&gt;We can't add a new item at a specific position of a slice like we did in array. We can only add a new item at the end of the slice. We do that using a function called &lt;code&gt;append&lt;/code&gt;. This is a default function. It takes 2 arguments. One is the slice, and other one is the item we want to add. This function returns a new slice with new item added to the old slice.&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="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;slice1&lt;/span&gt; &lt;span class="o"&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="s"&gt;"chiranjeevi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"golang"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;slice1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"devops"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Assigning it to the old slice again&lt;/span&gt;
    &lt;span class="c"&gt;// Bcoz the append function only return new slice&lt;/span&gt;
    &lt;span class="c"&gt;// It doesn't change the old slice.&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice1&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;Output&lt;/strong&gt;:&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%2Fb1jk1uz6uxk5rlj5nl0t.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%2Fb1jk1uz6uxk5rlj5nl0t.png" alt="Output" width="568" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating value at a position in array and slice
&lt;/h3&gt;

&lt;p&gt;We can update an already present value in an array and a slice. Specify that position using index operator and assign a value to it. We did this while adding item to an array. There we assigned item to a position where there is nothing assigned already. So, it is adding an element. If we try to do the same when there is an item present there already, it is updating an item.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to update value at a position in array
&lt;/h4&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="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;arr&lt;/span&gt; &lt;span class="o"&gt;:=&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="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="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;arr&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="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;400&lt;/span&gt; &lt;span class="c"&gt;// Updating value of 4 (index 3) to 400&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c"&gt;// arr[5] = 500 //Trying to assign value to position not present&lt;/span&gt;
    &lt;span class="c"&gt;// This throws an error if uncommented above line.&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;Output&lt;/strong&gt;&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%2Fj4ws2yv6un5ihbxgvtb2.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%2Fj4ws2yv6un5ihbxgvtb2.png" alt=" " width="280" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output if uncommented the error line&lt;/strong&gt;&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%2Fx6icndmhv3sw0wbjcvu6.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%2Fx6icndmhv3sw0wbjcvu6.png" alt=" " width="800" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We didn't use index operator while adding an item to a slice. It is because, we can't access a position which is not part of an array or slice. In case of an array, we know the size of it while declaring, so we can add any item as far as we are adding to a position which is present in the array. But, when it comes to slice, we don't know the size that a slice currently possessing. If we try to update or access a position which is not already present in the array or slice, it throws an error. We can get size or current length of an array or a slice using &lt;strong&gt;len&lt;/strong&gt; function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to update a value in slice
&lt;/h4&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;updateSlice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&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;pos&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;val&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Slice index out of bound"&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;slc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val&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;slc&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;slc&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="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;slc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updateSlice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&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;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;slc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updateSlice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&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="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slc&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;Output&lt;/strong&gt;:&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%2Fews7a17uylug1tjtdbrg.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%2Fews7a17uylug1tjtdbrg.png" alt=" " width="494" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Function - arguments and return type
&lt;/h3&gt;

&lt;p&gt;We discussed about &lt;strong&gt;functions&lt;/strong&gt; in the first article of this series, check it out if you missed it: &lt;a href="https://dev.to/vchiranjeeviak/go-hello-world-3ino"&gt;https://dev.to/vchiranjeeviak/go-hello-world-3ino&lt;/a&gt;. In that one, we saw what function header, arguments and body are. Now, let's see how we actually declare arguments from the above code example.&lt;/p&gt;

&lt;p&gt;We declare arguments inside a pair of parenthesis. For each argument, we have to give a name with which we want to call it inside the function succeeded by its datatype.&lt;/p&gt;

&lt;p&gt;In above example, we need the slice, the position at which we are updating and the value with which we want to update as arguments. So, we need to declare 3 arguments in total. First one is of type slice with integers, so we declare it as &lt;code&gt;[]int&lt;/code&gt; preceded by the name. Second is the position which is an integer, so &lt;code&gt;int&lt;/code&gt; preceded by its name. Third is the value which is also an integer, so &lt;code&gt;int&lt;/code&gt; preceded by its name. Hence, it boiled down to &lt;code&gt;(slc []int, pos int, val int)&lt;/code&gt;. That is why when we are calling the function, we used &lt;code&gt;updateSlice(slc, 1, 500)&lt;/code&gt;. The number of arguments and order should be maintained as it is in the declaration.&lt;/p&gt;

&lt;p&gt;Also, there is something after these arguments outside parenthesis. That is called the &lt;strong&gt;return type&lt;/strong&gt; of the function. It means that the value which is returned by the function has that type. In our example, we are returning a int slice, so we gave &lt;code&gt;[]int&lt;/code&gt; as return type. Where does it return to? The value is returned to the place where the function is called. We are calling it inside main function, so it returns the value there. That is the reason why we are re-assigning it to the original slice there.&lt;/p&gt;

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

&lt;p&gt;That's it for this one. In the next article, lets cover maps and structs.&lt;/p&gt;

&lt;p&gt;More on Go lang will be coming soon and do follow me to see them. Make sure you like and share if you felt this good.&lt;/p&gt;

&lt;p&gt;Let's connect on: &lt;a href="https://twitter.com/VChiranjeeviAK" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;a href="https://www.linkedin.com/in/chiranjeevi-tirunagari/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;a href="https://www.showwcase.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Showwcase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>devops</category>
      <category>cloud</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Go - Conditionals and Loops</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Wed, 24 Aug 2022 16:43:00 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/go-conditionals-and-loops-5f1g</link>
      <guid>https://dev.to/vchiranjeeviak/go-conditionals-and-loops-5f1g</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Welcome back to this &lt;strong&gt;part 4&lt;/strong&gt; of the &lt;strong&gt;Go lang&lt;/strong&gt; series. In &lt;strong&gt;part 3&lt;/strong&gt;, we covered pointers and user input. Check it out: &lt;a href="https://dev.to/vchiranjeeviak/go-pointers-user-input-4mh9"&gt;https://dev.to/vchiranjeeviak/go-pointers-user-input-4mh9&lt;/a&gt; if you missed it. In this one let's see conditionals and loops in go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditionals
&lt;/h2&gt;

&lt;p&gt;Generally, programming is all about writing some logic in the form of code so that machine can understand. One of the most vital part of writing logic is to deal with some conditions. We may want our machine to react differently in different situations. In those cases, we specify those situations as conditions and write how machine needs to work in those situations. We do this using conditionals.&lt;/p&gt;

&lt;p&gt;We can write conditions in &lt;strong&gt;2&lt;/strong&gt; ways. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;if-else&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;switch-case&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1. if-else
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We write the code which we want to be executed if a specific condition is met is written inside &lt;strong&gt;if&lt;/strong&gt; block along with a condition.&lt;/li&gt;
&lt;li&gt;The code which we want to get executed when that condition is not met is written inside &lt;strong&gt;else&lt;/strong&gt; block.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The syntax to use &lt;strong&gt;if-else&lt;/strong&gt; is:&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;if&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// code which needs to be executed if above condition is met&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="c"&gt;// code which needs to be executed when above condition is not met&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Condition
&lt;/h4&gt;

&lt;p&gt;A condition in an &lt;strong&gt;if-else&lt;/strong&gt; syntax can be any combination of &lt;strong&gt;logical&lt;/strong&gt; operation and &lt;strong&gt;relational&lt;/strong&gt; operation.&lt;/p&gt;

&lt;p&gt;There are many types of operators in Go lang and &lt;strong&gt;logical&lt;/strong&gt;, &lt;strong&gt;relational&lt;/strong&gt; operators are two of them.&lt;/p&gt;

&lt;p&gt;Before going into it, there is something we need to know about operands. An &lt;strong&gt;operand&lt;/strong&gt; is a variable or value on which the operator performs the operation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logical operators
&lt;/h4&gt;

&lt;p&gt;An operation done using logical operators is called logical operation. There are &lt;strong&gt;3&lt;/strong&gt; logical operators. They are &lt;strong&gt;AND (&amp;amp;&amp;amp;)&lt;/strong&gt;, &lt;strong&gt;OR (||)&lt;/strong&gt; and &lt;strong&gt;NOT (!)&lt;/strong&gt;. These logical operators are applied on operands which can be evaluated to a boolean value (true or false). Both &lt;strong&gt;AND&lt;/strong&gt; and &lt;strong&gt;OR&lt;/strong&gt; are binary operators (refer: &lt;strong&gt;Part 3&lt;/strong&gt; for this) take two operands on either sides of the operator. &lt;strong&gt;AND&lt;/strong&gt; evaluates &lt;strong&gt;true&lt;/strong&gt; only when both the operands are true or &lt;strong&gt;false&lt;/strong&gt; in any other case. &lt;strong&gt;OR&lt;/strong&gt; evaluates &lt;strong&gt;true&lt;/strong&gt; when at least one of them is true or &lt;strong&gt;false&lt;/strong&gt; in any other case.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;operand 1&lt;/th&gt;
&lt;th&gt;operator&lt;/th&gt;
&lt;th&gt;operand 2&lt;/th&gt;
&lt;th&gt;result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;AND&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;AND&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;AND&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;AND&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;OR&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;OR&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;OR&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;OR&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;NOT&lt;/strong&gt; takes only one of the boolean values and gives back the other one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Operand&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;NOT&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NOT&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Okay.... This is fine, but where do we get these &lt;strong&gt;True&lt;/strong&gt; or &lt;strong&gt;False&lt;/strong&gt; from?&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;relational&lt;/strong&gt; operators come into picture. We usually give an expression which contains relational operators and which is evaluated to one of the boolean values.&lt;/p&gt;

&lt;h4&gt;
  
  
  Relational operators
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Relational&lt;/strong&gt; operators are operators which check if the given relationship between two operands is true or not. Let's say, if there is an expression which says "5 is greater than 6", it is giving relationship between two operands and it is obviously false in this case.&lt;/p&gt;

&lt;p&gt;There are &lt;strong&gt;6&lt;/strong&gt; relational operators. They are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operand 1&lt;/th&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Operand 2&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;== (&lt;strong&gt;equals to&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;!= (&lt;strong&gt;not equals to&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&amp;gt; (&lt;strong&gt;greater than&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&amp;gt;= (&lt;strong&gt;greater than or equals to&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&amp;lt; (&lt;strong&gt;less than&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&amp;lt;= (&lt;strong&gt;less than or equals to&lt;/strong&gt;)&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  Program to illustrate if-else
&lt;/h4&gt;

&lt;p&gt;We are writing a program to check if the user given integer is in the range &lt;strong&gt;[0, 100)&lt;/strong&gt;. Depending on the input, we will print a unique message accordingly. This representation says that the number should be between 0 and 100, and can be equal to 0 but not equal to 100. One of our relational expression should be &lt;code&gt;our number &amp;gt;= 0&lt;/code&gt; and other one should be &lt;code&gt;our number &amp;lt; 100&lt;/code&gt;. And, we need to combine both of them with an &lt;code&gt;AND (&amp;amp;&amp;amp;)&lt;/code&gt; because both of them are necessary checks.&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;someNumber&lt;/span&gt; &lt;span class="kt"&gt;int&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;"Enter an integer:"&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;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;someNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;someNumber&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="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someNumber&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// Condition&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;"In our range"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Comes here only when the condition is true&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;"Not in our range"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Comes here only when the condition is false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. switch-case
&lt;/h3&gt;

&lt;p&gt;I don't want to complicate &lt;strong&gt;switch-case&lt;/strong&gt; because it is as simple as a real switch board. We can design a digital switch board just like how we have a unique switch for fan, light, bulb etc..&lt;/p&gt;

&lt;p&gt;Syntax for &lt;strong&gt;switch-case&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;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;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;case&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;:&lt;/span&gt;
        &lt;span class="c"&gt;// code&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;:&lt;/span&gt;
        &lt;span class="c"&gt;// code&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="c"&gt;// code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Program to illustrate switch-case
&lt;/h4&gt;

&lt;p&gt;This example is enough to understand &lt;strong&gt;switch-case&lt;/strong&gt;. We take a string input from user. If it is a "fan" given as input, we print "switched fan on". If the input is "light", we print "switched light on". If it is anything else, we print "Press the correct switch".&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt; &lt;span class="kt"&gt;string&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;"Give input:"&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;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;userInput&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="n"&gt;userInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"fan"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"switched fan on"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"light"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"switched light on"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;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;"Press the correct switch"&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;Note: I am not giving outputs in this article. Please run by yourself and comment if you find any errors in code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can write the same code using &lt;strong&gt;if-else&lt;/strong&gt; also. Try doing that yourself and comment if you faced any difficulty. I know this is basic stuff if you are coming from other programming language background, but this can be challenging for someone who started coding.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Loops
&lt;/h2&gt;

&lt;p&gt;There might be some situations where we want to run some block of code multiple times. And, the number of times can be decided by a condition or an event. An example situation can be "Taking a string input from user until it is &lt;code&gt;special&lt;/code&gt;" or "Printing numbers from &lt;code&gt;1 to 100&lt;/code&gt;, etc. Wherever we see repetition of task, we can apply loops there.&lt;/p&gt;

&lt;p&gt;Unlike many famous programming languages, Go has only one type of loop, which is &lt;strong&gt;for&lt;/strong&gt; loop. The syntax of &lt;strong&gt;for&lt;/strong&gt; loop is:&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;for&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// repetitive task&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until the condition is satisfied (evaluates to True), the code written inside keeps running again and again.&lt;/p&gt;

&lt;p&gt;Whenever we think of conditions, we should remember logical and relational operators. Because, most of the times, conditions contain only them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to take user input until we get &lt;code&gt;special&lt;/code&gt; as input
&lt;/h3&gt;

&lt;p&gt;Let's declare a variable to store input, and write a Scan statement inside for loop. But, we have to think of condition that satisfies our situation. We need to stop this loop only when we receive &lt;code&gt;special&lt;/code&gt;. So, we need to make it run until the input is not &lt;code&gt;special&lt;/code&gt;. And let us also print a line after loop to show that the program control came out of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"special"&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;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;userInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Outside loop"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will keep running until you give &lt;code&gt;special&lt;/code&gt; as input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Program to print numbers in [1, 100) range
&lt;/h3&gt;

&lt;p&gt;We can have a variable initialised to &lt;code&gt;1&lt;/code&gt;. When gone inside loop, print the variable, update it by increasing its value by 1 every time. We can stop when our variable value is greater than 99 or equal to 100.&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="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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&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;99&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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="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;++&lt;/code&gt; is a unary operator which increases the value of the variable by &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every time we go inside a loop, we call it an &lt;strong&gt;iteration&lt;/strong&gt;. This is an important terminology to know for next topics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are initialising a variable outside loop, writing a condition on it, and updating its value inside loop just print its value in the loop. It can become tough if the code in file is huge to keep track of this particular variable. So, it is better handle this kind of variables inside &lt;code&gt;for&lt;/code&gt; parameters. The syntax is:&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;initialisation&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;updation&lt;/span&gt;&lt;span class="o"&gt;&amp;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;//code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets see how above code can be converted into this method.&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&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;1&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;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;99&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be much more readable and cleaner.&lt;/p&gt;

&lt;h3&gt;
  
  
  Infinite loops
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;infinite loop&lt;/strong&gt; is a loop which doesn't stop running and runs forever either until the memory is full or power is disconnected. Most modern programming languages and their run time environments or compilers handle this carefully without letting your system get hanged. Yeah, that's what happens when your RAM is full of apps. How can we invoke an infinite loop? It is very simple to do. We just need to give a condition (check syntax of loop) in loop which is never going to false. One such condition can be &lt;code&gt;0 &amp;gt; 10&lt;/code&gt;. This condition will always be false no matter how the variables in the program change. Or we can just give &lt;code&gt;true&lt;/code&gt; (boolean) as condition as anyway any condition we give at last is evaluated to either true or false.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to print 1 infinite times
&lt;/h4&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="no"&gt;true&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="m"&gt;1&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;This might give a warning that &lt;code&gt;true&lt;/code&gt; is not required to provide as if we don't give anything, it considers as &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
So, a corrected perfect program can be:&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&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="m"&gt;1&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;I encourage you to run this and see how an infinite loop prints if you are a beginner. If you are not a beginner, I am sure that you must have seen this a lot of times.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we had given &lt;code&gt;false&lt;/code&gt; instead of &lt;code&gt;true&lt;/code&gt;, the loop doesn't even run once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Can we run finite loops without giving a condition? The answer is partially yes. Because, we still give a condition but not at the usual place we give. We provide a condition when to stop running inside the loop but not outside loop. We do this using &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Break and Continue
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;break&lt;/strong&gt; and &lt;strong&gt;continue&lt;/strong&gt; are two keywords in most programming languages and so in Go. When a loop is running, if we want to stop running that loop at a particular iteration and not run further, we use &lt;code&gt;break&lt;/code&gt;. When a loop is running, if we want to skip one particular iteration and run remaining iterations normally, we use continue.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to print [1, 100) using break
&lt;/h4&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="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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program also prints 1, 99. But the difference is in how we wrote the loop and base condition.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to print [1, 100) but don't print 10
&lt;/h4&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="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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&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;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;continue&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If you think this will print 1 to 99, then you are wrong. Why? Because, when the number is &lt;code&gt;10&lt;/code&gt;, it goes into &lt;code&gt;if&lt;/code&gt; block and executes &lt;code&gt;continue&lt;/code&gt;, which will take it directly out of loop and enters back into the loop from top. It will never execute &lt;code&gt;number++&lt;/code&gt; line, which means the number value is not going to move from 10 and will keep rotating for loop which makes it an infinite loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To make it work as we intend to, we need to make a small change:&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="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="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&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;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;continue&lt;/span&gt;
            &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="c"&gt;// new added line&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This newly added line makes sure that the number value is increasing even when it hits &lt;code&gt;continue&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;That's it for this one... Like and share this if you loved it. Follow me for the future content.&lt;/p&gt;

&lt;p&gt;Let's connect on: &lt;a href="https://twitter.com/VChiranjeeviAK" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;a href="https://www.linkedin.com/in/chiranjeevi-tirunagari/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;a href="https://www.showwcase.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Showwcase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>devops</category>
      <category>cloud</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Go - Pointers and User input</title>
      <dc:creator>Chiranjeevi Tirunagari</dc:creator>
      <pubDate>Sun, 21 Aug 2022 05:53:00 +0000</pubDate>
      <link>https://dev.to/vchiranjeeviak/go-pointers-user-input-4mh9</link>
      <guid>https://dev.to/vchiranjeeviak/go-pointers-user-input-4mh9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the &lt;strong&gt;part 3&lt;/strong&gt; of the &lt;strong&gt;Go lang&lt;/strong&gt; series. In the previous part, we saw what variables, constants and datatypes are. Check &lt;strong&gt;&lt;a href="https://dev.to/vchiranjeeviak/go-variables-constants-and-datatypes-502j"&gt;Part 2&lt;/a&gt;&lt;/strong&gt; if you missed it. In this one lets see what pointers are and how we can take input from the user who is running the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before going into pointers (very important to understand pointers)
&lt;/h2&gt;

&lt;p&gt;Before understanding pointers, we need to understand how the program execution works. It is different from language to language and it will be similar for some languages. Some programming languages interact with computer memory and some don't. So, "What memory are we talking about?". It is &lt;strong&gt;Random Access Memory&lt;/strong&gt; (RAM).&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory
&lt;/h3&gt;

&lt;p&gt;Any high-level program or software is basically stored in hard disk to use it whenever needed. The programs written in these type of languages are loaded into RAM when we want to run and then each line of code starts running there. Any memory needed to run that like storing variables, constants etc is also taken from RAM. Sometimes, some data used by a running program can be stored in cache or in secondary memory which is hard drive depending on few factors.&lt;/p&gt;

&lt;p&gt;Some languages like Java, Javascript, Python etc have their own runtime environments. These runtime environments act as an interface between programs and actual memory. They still use primary memory to run but not directly. They actually use something called &lt;strong&gt;virtual memory&lt;/strong&gt; which maps to actual main memory of system. The addresses will be different for actual and virtual and can be mapped only by these runtime environments which is a very low level implementation. This is bit out of the way to our intention of this article. So, let's continue with RAM for now since Go doesn't use any runtime environment and directly interacts with system memory.&lt;/p&gt;

&lt;h4&gt;
  
  
  Memory animation
&lt;/h4&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%2Fa2b4e1e6cypgiw10t5gq.gif" 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%2Fa2b4e1e6cypgiw10t5gq.gif" alt="Memory animation" width="2048" height="2048"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Addresses
&lt;/h3&gt;

&lt;p&gt;So, now that we know that programs especially of C, C++, Go run in RAM and these use some memory to store data required to run. The unit of memory is &lt;strong&gt;bytes&lt;/strong&gt;. And &lt;code&gt;1 byte = 8 bits&lt;/code&gt;. A &lt;strong&gt;bit&lt;/strong&gt; can contain either 0 or 1. Each byte in a memory device can be uniquely identified by its &lt;strong&gt;address&lt;/strong&gt;. The address will be of the form &lt;strong&gt;Hexadecimal&lt;/strong&gt;. Something like &lt;code&gt;0x324XF432&lt;/code&gt;. And there is an order for these addresses because the memory is continuous.&lt;/p&gt;

&lt;p&gt;Can we see the address of a variable programatically? Yes, we can do by using the &lt;code&gt;&amp;amp;&lt;/code&gt; operator. On a side note, an operator is a symbol which performs a special operation on some data &lt;strong&gt;(operands)&lt;/strong&gt;, and the number of operands is dependent on the operator. &lt;code&gt;&amp;amp;&lt;/code&gt; operator is applied on only one operand, hence, it belongs to &lt;strong&gt;unary operators&lt;/strong&gt; group. There are other groups like &lt;strong&gt;binary operators&lt;/strong&gt; (2 operands) and &lt;strong&gt;ternary operators&lt;/strong&gt; (3 operators). So, the only operand that &lt;code&gt;&amp;amp;&lt;/code&gt; (ampersand / address) operator takes is the variable name.&lt;/p&gt;

&lt;h4&gt;
  
  
  Program to find address of a variable
&lt;/h4&gt;

&lt;p&gt;An example program to get the address of a variable:&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="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;someVariable&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;

    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someVariable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// This prints value of someVariable&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="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;someVariable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// This prints address of someVariable&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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%2Fbus4vsn87zzetaltl8hc.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%2Fbus4vsn87zzetaltl8hc.png" alt="Address output" width="800" height="159"&gt;&lt;/a&gt;&lt;br&gt;
This output of address will be different for system to system. Even we get different address value every time we run. &lt;/p&gt;
&lt;h2&gt;
  
  
  Pointers
&lt;/h2&gt;

&lt;p&gt;So, now we know how and where data (variables, constants) is being stored. This is where pointers come into picture.&lt;/p&gt;
&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pointer&lt;/strong&gt; is just another datatype like int, string, boolean. But, what a variable of type pointer stores is &lt;strong&gt;address&lt;/strong&gt;. We saw how addresses look like in above output. A pointer stores those kind of address. Whose address they store? We can store addresses of other variables and constants.&lt;/p&gt;
&lt;h3&gt;
  
  
  Questions to keep in mind for future
&lt;/h3&gt;

&lt;p&gt;What is the point of storing addresses? How are pointers useful? Are pointers good or bad? These are valid questions. To know the answers, we need to go deeper into learning this language. We will find out answers to each of these questions in future articles. These questions are highlighted as topics in future articles. So do wait / check if they are already published by me.&lt;/p&gt;
&lt;h3&gt;
  
  
  Pointer of pointer
&lt;/h3&gt;

&lt;p&gt;We saw that a pointer variable stores addresses. So, it is just like any other variable, which means that it is also stored in memory, which means that a pointer variable also has an address, which means that this address value can be stored in other pointer, which is called &lt;strong&gt;pointer of pointer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I hope you got an idea on pointers. You will be more confident when you get to know the answers to the above questions. So, just keep this concept in mind.&lt;/p&gt;
&lt;h2&gt;
  
  
  User input
&lt;/h2&gt;

&lt;p&gt;Now that we are comfortable with the concept of addresses and how variables are stored, it is a perfect time to know how to take input from user while program is running (run time).&lt;/p&gt;

&lt;p&gt;As we discussed in previous articles, even though we are not having a value while writing program and taking that value from user in run time, we need to reserve some space in our program memory to store it when program receives it. And we do that by declaring (not initialising, check &lt;strong&gt;&lt;a href="https://dev.to/vchiranjeeviak/go-variables-constants-and-datatypes-502j"&gt;Part 2&lt;/a&gt;&lt;/strong&gt;) a variable for this value.&lt;/p&gt;
&lt;h3&gt;
  
  
  Scan
&lt;/h3&gt;

&lt;p&gt;The function we use to take user input is &lt;strong&gt;Scan&lt;/strong&gt;. It belongs to the same &lt;strong&gt;"fmt"&lt;/strong&gt; package to which &lt;strong&gt;Print&lt;/strong&gt; also belongs. If I forgot to convey that &lt;strong&gt;fmt&lt;/strong&gt; is short for &lt;strong&gt;format&lt;/strong&gt;, now you know.&lt;/p&gt;

&lt;p&gt;So, we pass the address (I hope you know how to get address of a variable from above part of article) of the variable in which we want to store the input value to the Scan function. This function pauses the program execution when it reaches that line and waits for user to give the input. Once the user gives input and hits &lt;code&gt;Enter&lt;/code&gt;, it resumes from there.&lt;/p&gt;
&lt;h4&gt;
  
  
  Program to illustrate user input
&lt;/h4&gt;

&lt;p&gt;An example program which takes a number as input from user and prints the next number to the screen:&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="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="c"&gt;// declaring integer variable&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;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// we pass address using &amp;amp;&lt;/span&gt;
    &lt;span class="c"&gt;// The program execution pauses here until user provides input and hits enter&lt;/span&gt;

    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Printing next number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will wait like this until you give some number to it and hit &lt;code&gt;Enter&lt;/code&gt;. If you give other than a (int) number, it throws an error.&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%2F1fphf7gj27kqvbcgyumj.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%2F1fphf7gj27kqvbcgyumj.png" alt="Waiting for input" width="800" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I gave &lt;code&gt;122&lt;/code&gt; as input and hit &lt;code&gt;Enter&lt;/code&gt;. Here is the output.&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%2F3rhpm8c0a72iomoi2ux6.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%2F3rhpm8c0a72iomoi2ux6.png" alt="Input output" width="800" height="49"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  User input in Go/C/C++ vs Python/Java/Javascript
&lt;/h3&gt;

&lt;p&gt;This is how we take user input in run time. We can learn this concept in the introductory class / video / article of languages like python or java. It is because that there is no concept of interacting with memory in them. So, there are no addresses in them. But, Go has this like C/C++ and so we had to learn about addresses first.&lt;/p&gt;

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

&lt;p&gt;That's it for this one. Next article is going to be on conditionals and loops in Go.&lt;/p&gt;

&lt;p&gt;Do follow me to see them. Make sure you like and share if you felt this good.&lt;/p&gt;

&lt;p&gt;Let's connect on: &lt;a href="https://twitter.com/VChiranjeeviAK" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; &lt;a href="https://www.linkedin.com/in/chiranjeevi-tirunagari/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;a href="https://www.showwcase.com/vchiranjeeviak" rel="noopener noreferrer"&gt;Showwcase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>devops</category>
      <category>beginners</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
