<?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: Adrian DY</title>
    <description>The latest articles on DEV Community by Adrian DY (@adriandy89).</description>
    <link>https://dev.to/adriandy89</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%2F1078636%2F74a47fe0-f24b-4ce2-bf55-9b332efd603f.jpg</url>
      <title>DEV Community: Adrian DY</title>
      <link>https://dev.to/adriandy89</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adriandy89"/>
    <language>en</language>
    <item>
      <title>Blockchain and Smart Contracts integraton, 3D experience</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Sun, 31 Mar 2024 15:33:52 +0000</pubDate>
      <link>https://dev.to/adriandy89/blockchain-and-smart-contracts-integraton-3d-experience-2l40</link>
      <guid>https://dev.to/adriandy89/blockchain-and-smart-contracts-integraton-3d-experience-2l40</guid>
      <description>&lt;p&gt;Revolutionize your digital landscape with our cutting-edge integration of Blockchain and Smart Contracts into your 3D experience! Imagine a world where transactions are secure, transparent, and seamlessly executed within your immersive environment. With our innovative solution, you can unlock new levels of efficiency, trust, and creativity in your 3D projects. Say goodbye to traditional limitations and embrace the future of decentralized, automated transactions. Elevate your digital experiences today with Blockchain and Smart Contracts integration!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.nftspeed.space/" rel="noopener noreferrer"&gt;https://marketplace.nftspeed.space/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>smartcontract</category>
      <category>react</category>
      <category>marketplace</category>
    </item>
    <item>
      <title>Building a Golang API with JWT authentication</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Sat, 10 Jun 2023 03:29:31 +0000</pubDate>
      <link>https://dev.to/adriandy89/building-a-golang-api-with-jwt-authentication-19k</link>
      <guid>https://dev.to/adriandy89/building-a-golang-api-with-jwt-authentication-19k</guid>
      <description>&lt;p&gt;First, let's start with some background information. JWT stands for JSON Web Tokens, and it is a standard for securely transmitting information between parties as a JSON object. JWTs are commonly used for authentication and authorization purposes, and they are composed of three parts: a header, a payload, and a signature.&lt;/p&gt;

&lt;p&gt;When a user logs in to your application, you can generate a JWT and send it back to the client. The client can then include the JWT in subsequent requests to your API, and your API can use the JWT to authenticate the user and authorize access to protected resources.&lt;/p&gt;

&lt;p&gt;Now, let's dive into the code. We'll start by creating a basic Golang API using the Gin framework. Make sure you have Gin installed before proceeding:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/gin-gonic/gin"&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;router&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="o"&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="p"&gt;})&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code creates a simple API that listens on port 8080 and responds with a "Hello, world!" message when you make a GET request to the root endpoint.&lt;/p&gt;

&lt;p&gt;Next, we'll add JWT authentication to our API. We'll be using the github.com/dgrijalva/jwt-go package to generate and validate JWTs.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/dgrijalva/jwt-go"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/gin-gonic/gin"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;jwtKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"my_secret_key"&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;router&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/login"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/auth"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;authMiddleware&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":8080"&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;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;loginData&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;Username&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"username" binding:"required"`&lt;/span&gt;
        &lt;span class="n"&gt;Password&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"password" binding:"required"`&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShouldBindJSON&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;loginData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusBadRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;()})&lt;/span&gt;
        &lt;span class="k"&gt;return&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;loginData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"myuser"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;loginData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Password&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"mypassword"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Invalid login credentials"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;expirationTime&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Minute&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;claims&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StandardClaims&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ExpiresAt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expirationTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unix&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewWithClaims&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SigningMethodHS256&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;claims&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;tokenString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SignedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jwtKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusInternalServerError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Could not generate token"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"token"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;tokenString&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;authMiddleware&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerFunc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tokenString&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Authorization"&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;tokenString&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Authorization header missing"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParseWithClaims&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tokenString&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;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StandardClaims&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&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="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SigningMethodHMAC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ErrInvalidSigningMethod&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;jwtKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ErrSignatureInvalid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Invalid token signature"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Invalid token"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Valid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusUnauthorized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Invalid token"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Next&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;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Hello, authenticated user!"&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;Let's go through this code step by step. First, we define a &lt;code&gt;jwtKey&lt;/code&gt; variable that will be used to sign and verify JWTs. This key should be kept secret and not shared with anyone.&lt;/p&gt;

&lt;p&gt;Next, we define two endpoints: &lt;code&gt;/login&lt;/code&gt; and &lt;code&gt;/auth/hello&lt;/code&gt;. The &lt;code&gt;/login&lt;/code&gt; endpoint accepts a username and password in JSON format, checks if they are valid, and generates a JWT if they are. The &lt;code&gt;/auth/hello&lt;/code&gt; endpoint requires a valid JWT to be included in the &lt;code&gt;Authorization&lt;/code&gt; header of the request.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;login&lt;/code&gt; function first checks if the username and password are valid. If they are, it creates a new JWT with an expiration time of 5 minutes and returnsit to the client in JSON format. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;authMiddleware&lt;/code&gt; function is a middleware that checks if a valid JWT is included in the &lt;code&gt;Authorization&lt;/code&gt; header of the request. If a valid JWT is present, it calls &lt;code&gt;c.Next()&lt;/code&gt; to allow the request to proceed. If a valid JWT is not present or is invalid, it returns an error response.&lt;/p&gt;

&lt;p&gt;Finally, the &lt;code&gt;hello&lt;/code&gt; function is a protected endpoint that requires a valid JWT to be accessed. If a valid JWT is present, it returns a "Hello, authenticated user!" message in JSON format.&lt;/p&gt;

&lt;p&gt;Let's start by discussing the &lt;code&gt;jwt-go&lt;/code&gt; package that we used to implement JWT authentication in our Golang API. This package provides functions for creating, parsing, and validating JWTs.&lt;/p&gt;

&lt;p&gt;When creating a JWT, we first create a &lt;code&gt;StandardClaims&lt;/code&gt; struct that includes any claims that we want to include in the JWT payload, such as an expiration time. We then create a new JWT using the &lt;code&gt;jwt.NewWithClaims&lt;/code&gt; function and sign it using our &lt;code&gt;jwtKey&lt;/code&gt; variable. The resulting signed JWT is then included in the response to the client.&lt;/p&gt;

&lt;p&gt;When validating a JWT, we first extract the JWT from the &lt;code&gt;Authorization&lt;/code&gt; header of the request and parse it using the &lt;code&gt;jwt.ParseWithClaims&lt;/code&gt; function. We pass in our &lt;code&gt;jwtKey&lt;/code&gt; variable as the second argument to this function to verify the signature of the JWT. If the JWT is valid and has not expired, we call &lt;code&gt;c.Next()&lt;/code&gt; to allow the request to proceed. If the JWT is invalid or has expired, we return an error response.&lt;/p&gt;

&lt;p&gt;It's important to note that JWTs should be used in conjunction with HTTPS to ensure that they are transmitted securely. JWTs should also be kept secret and not shared with anyone else.&lt;/p&gt;

&lt;p&gt;In addition to the &lt;code&gt;jwt-go&lt;/code&gt; package, we used the &lt;code&gt;Gin&lt;/code&gt; framework to create our Golang API. Gin is a lightweight web framework that provides features such as routing, middleware, and error handling. We defined two endpoints in our API: &lt;code&gt;/login&lt;/code&gt;, which generates a JWT if the user provides valid credentials, and &lt;code&gt;/auth/hello&lt;/code&gt;, which requires a valid JWT to be accessed.&lt;/p&gt;

&lt;p&gt;That's it! &lt;em&gt;With this code, you have created a Golang API with JWT authentication. Of course, you can customize this code to fit your specific needs, but this should give you a good starting point.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>learning</category>
      <category>jwt</category>
      <category>programming</category>
    </item>
    <item>
      <title>Concurrency in Go: Goroutines, Mutexes and Channels</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Mon, 22 May 2023 03:32:02 +0000</pubDate>
      <link>https://dev.to/adriandy89/concurrency-in-go-goroutines-mutexes-and-channels-40f4</link>
      <guid>https://dev.to/adriandy89/concurrency-in-go-goroutines-mutexes-and-channels-40f4</guid>
      <description>&lt;p&gt;Go (Golang) is a popular programming language that has been designed to support concurrency by making it easier for developers to write concurrent programs. &lt;/p&gt;

&lt;p&gt;Goroutines are a key feature of the Go programming language (Golang) that make it easy to write concurrent code. Goroutines enable developers to execute multiple functions or methods at the same time, without blocking other parts of the program. In this article, we’ll explore how goroutines work, and how they can be used to write efficient and robust concurrent code. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Goroutine?
&lt;/h3&gt;

&lt;p&gt;In Golang, a goroutine is a lightweight thread of execution that is managed by the Go runtime. Goroutines enable developers to run multiple functions or methods concurrently, without the need for complex locking or synchronization mechanisms. &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Goroutine
&lt;/h3&gt;

&lt;p&gt;To create a new goroutine in Golang, use the  go  keyword, followed by the function or method that you want to execute concurrently. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    go myFunction()
    // do other work
}
func myFunction() {
    // do something
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the  &lt;code&gt;myFunction()&lt;/code&gt;  method is executed concurrently, in a new goroutine. The main program can continue to execute other code, without waiting for  &lt;code&gt;myFunction()&lt;/code&gt;  to complete. &lt;/p&gt;

&lt;h3&gt;
  
  
  Synchronization and Sharing Data
&lt;/h3&gt;

&lt;p&gt;Goroutines in Golang share the same memory space, which means they can share data between them. However, this can create synchronization issues, as multiple goroutines may attempt to modify the same data at the same time. &lt;/p&gt;

&lt;p&gt;To prevent this, Golang provides synchronization primitives such as mutexes and channels. Mutexes are used to prevent multiple goroutines from accessing shared data simultaneously, while channels enable safe communication between multiple goroutines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mutexes
&lt;/h3&gt;

&lt;p&gt;In Golang, mutexes are used to allow exclusive access to shared resources. A mutex is a mechanism that is used to prevent two goroutines from accessing a shared resource at the same time. When a goroutine acquires a mutex, all other goroutines attempting to acquire the same mutex will block until the mutex is released. &lt;/p&gt;

&lt;p&gt;Here's an example of how to use a mutex in Golang:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "sync"
    "fmt"
)
 var count int
var mutex sync.Mutex
 func increment() {
    mutex.Lock()
    count++
    mutex.Unlock()
}
func main() {
    var wg sync.WaitGroup
     for i := 0; i &amp;lt; 1000; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            increment()
        }()
    }
    wg.Wait()
    fmt.Println(count)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using a mutex to protect the  &lt;code&gt;count&lt;/code&gt;  variable which is being used by multiple goroutines. The  &lt;code&gt;increment()&lt;/code&gt;  function increments the  &lt;code&gt;count&lt;/code&gt;  variable and uses the  &lt;code&gt;mutex.Lock()&lt;/code&gt;  and  &lt;code&gt;mutex.Unlock()&lt;/code&gt;  methods to ensure that the variable is being accessed by only one goroutine at a time. The end result is that the  &lt;code&gt;count&lt;/code&gt;  variable is incremented by all the goroutines, resulting in a value of 1000. &lt;/p&gt;

&lt;h3&gt;
  
  
  Channels
&lt;/h3&gt;

&lt;p&gt;Channels are a way for goroutines to communicate with each other. A channel allows one goroutine to send data to another goroutine. This is useful for sharing data between goroutines that might otherwise need to be protected with a mutex. &lt;/p&gt;

&lt;p&gt;Here's an example of using a channel in Golang:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "fmt"
)
func producer(ch chan&amp;lt;- int) {
    for i := 0; i &amp;lt; 10; i++ {
        ch &amp;lt;- i
    }
    close(ch)
}
func consumer(ch &amp;lt;-chan int, done chan&amp;lt;- bool) {
    for value := range ch {
        fmt.Println("Received:", value)
    }
    done &amp;lt;- true
}
func main() {
    ch := make(chan int)
    done := make(chan bool)
    go producer(ch)
    go consumer(ch, done)
    &amp;lt;-done
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've created a  &lt;code&gt;producer&lt;/code&gt;  function that sends ten integers to a channel and then closes the channel. We've also created a  &lt;code&gt;consumer&lt;/code&gt;  function that reads values from the channel and prints them. The  &lt;code&gt;main&lt;/code&gt;  function creates the channel and a  &lt;code&gt;done&lt;/code&gt;  channel to signal when the  &lt;code&gt;consumer&lt;/code&gt;  has finished. When the  &lt;code&gt;consumer&lt;/code&gt;  has finished reading from the channel, it sends a value to the  &lt;code&gt;done&lt;/code&gt;  channel. The main function waits for the &lt;code&gt;done&lt;/code&gt; channel to receive a value. &lt;/p&gt;

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

&lt;p&gt;Goroutines are a powerful feature of the Go programming language that enable efficient and robust concurrent programming. By using goroutines, developers can write code that can execute multiple tasks simultaneously, without the need for complex locking or synchronization mechanisms. However, it’s important to be aware of synchronization issues when sharing data between goroutines, and to use synchronization primitives such as mutexes and channels to prevent them.&lt;/p&gt;

&lt;p&gt;Mutexes and channels are powerful tools for managing concurrency in Golang programming. Mutexes can be used to ensure that shared resources are accessed in an exclusive manner, while channels enable goroutines to communicate with each other in a safe and efficient manner. By using mutexes and channels correctly, Golang developers can write efficient, scalable, and robust concurrent programs.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding Golang Object Oriented Programming (OOP) with Examples</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Thu, 18 May 2023 03:45:37 +0000</pubDate>
      <link>https://dev.to/adriandy89/understanding-golang-object-oriented-programming-oop-with-examples-15l6</link>
      <guid>https://dev.to/adriandy89/understanding-golang-object-oriented-programming-oop-with-examples-15l6</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Object-oriented programming (OOP) is a popular programming paradigm that emphasizes the use of objects and their interactions to design and build applications. Go is not a pure object-oriented language like Java or Python, but it does support many OOP concepts like encapsulation, abstraction, inheritance, and polymorphism. In this article, we will explore how to write object-oriented programs in Go. &lt;/p&gt;

&lt;h3&gt;
  
  
  Defining a Class
&lt;/h3&gt;

&lt;p&gt;In Go, we define classes as structures. A class is a collection of attributes (fields) and behaviors (methods). Here's an example of a class representing a  Person :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Person struct {
    Name   string
    Age    int
    Gender string
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This defines a class  Person  with the three fields  Name ,  Age , and  Gender . &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an Object
&lt;/h3&gt;

&lt;p&gt;In Go, we create an object by instantiating a structure. We can create an object of class  Person  like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;person := Person{Name: "John Doe", Age: 30, Gender: "Male"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an object  person  with the values  "John Doe" ,  30 , and  "Male" . &lt;/p&gt;

&lt;h3&gt;
  
  
  Encapsulation
&lt;/h3&gt;

&lt;p&gt;Encapsulation is the concept of wrapping data and methods inside a single unit. In Go, encapsulation is achieved by using a naming convention for fields and methods: Private fields and methods start with a lowercase letter, while public fields and methods start with an uppercase letter. &lt;br&gt;
 Here's an example of a  Person  class with private fields and public methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Person struct {
    name   string
    age    int
    gender string
}
func (p *Person) SetName(name string) {
    p.name = name
}
func (p *Person) GetName() string {
    return p.name
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've made the fields  name ,  age , and  gender  private by using lowercase letters. We've also defined two public methods  SetName()  and  GetName()  to access and modify the value of the  name  field. &lt;/p&gt;

&lt;h3&gt;
  
  
  Abstraction
&lt;/h3&gt;

&lt;p&gt;Abstraction is the process of hiding implementation details while showing only the necessary information to the user. In Go, we can achieve abstraction by using interfaces. An interface specifies a set of methods that a struct must implement to satisfy the interface. &lt;br&gt;
 Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Animal interface {
    Speak() string
}
type Dog struct {
    Name string
}
func (d Dog) Speak() string {
    return "Woof!"
}
type Cat struct {
    Name string
}
func (c Cat) Speak() string {
    return "Meow!"
}
func main() {
    animals := []Animal{Dog{Name: "Fido"}, Cat{Name: "Fluffy"}}
    for _, animal := range animals {
        fmt.Println(animal.Speak())
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've defined an interface  Animal  with a single method  Speak() . We've also defined two implementations of this interface:  Dog  and  Cat . We can now create a list of  Animal  objects and call the  Speak()  method on each of them to get their respective sounds. &lt;/p&gt;

&lt;h3&gt;
  
  
  Inheritance
&lt;/h3&gt;

&lt;p&gt;Inheritance is the concept of creating a new class from an existing class. In Go, we achieve inheritance using composition. Composition is the process of combining different structures to create a new one. &lt;br&gt;
 Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Animal struct {
    Name   string
    Origin string
}
type Bird struct {
    Animal
    SpeedKPH float32
    CanFly   bool
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've defined a class  Animal  with two fields  Name  and  Origin . We've also defined a class  Bird  that "inherits" all of the fields and methods from  Animal  using composition. In addition,  Bird  has two additional fields  SpeedKPH  and  CanFly . &lt;/p&gt;

&lt;h3&gt;
  
  
  Polymorphism
&lt;/h3&gt;

&lt;p&gt;Polymorphism is the concept of using a single interface to represent multiple classes. In Go, we achieve polymorphism using interfaces and method overriding. &lt;br&gt;
 Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Shape interface {
    Area() float64
}
type Rectangle struct {
    Width  float64
    Height float64
}
func (r Rectangle) Area() float64 {
    return r.Width * r.Height
}
type Circle struct {
    Radius float64
}
func (c Circle) Area() float64 {
    return math.Pi * c.Radius * c.Radius
}
func main() {
    shapes := []Shape{Rectangle{2, 3}, Circle{4}}
    for _, shape := range shapes {
        fmt.Println("Area =", shape.Area())
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've defined an interface  Shape  with a single method  Area() . We've also defined two implementations of this interface:  Rectangle  and  Circle . We can now create a list of  Shape  objects and call the  Area()  method on each of them to get the area of the shape, regardless of whether it's a rectangle or a circle. &lt;/p&gt;

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

&lt;p&gt;In this article, we've explored how to write object-oriented programs in Go. We've covered defining a class, creating an object, encapsulation, abstraction, inheritance, and polymorphism. By using these concepts, you can write well-organized, modular code that is easy to maintain and extend.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Error Handling in Golang: How to Prevent Application Stop</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Wed, 17 May 2023 13:47:52 +0000</pubDate>
      <link>https://dev.to/adriandy89/error-handling-in-golang-how-to-prevent-application-stop-4pgn</link>
      <guid>https://dev.to/adriandy89/error-handling-in-golang-how-to-prevent-application-stop-4pgn</guid>
      <description>&lt;p&gt;Golang is a programming language that has gained immense popularity among developers due to its robust performance and simplicity. However, like any other programming language, Golang handles errors in its own way. &lt;/p&gt;

&lt;p&gt;In this article, we’ll dive deep into how Golang handles errors and explore some effective ways to prevent your application from stopping. &lt;/p&gt;

&lt;h3&gt;
  
  
  Error Handling in Golang
&lt;/h3&gt;

&lt;p&gt;In Golang, errors are represented as values of the built-in  error  type. Golang provides a simple, yet effective, way of handling errors using the  err  syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if err != nil {
    // handle the error
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This syntax checks if the value of the error is  nil , which means there was no error. If the error is not  nil , the code inside the  if  block is executed, which handles the error. &lt;/p&gt;

&lt;p&gt;However, one of the biggest mistakes that developers make while handling errors in Golang is to ignore them. Ignoring errors can lead to unexpected application behavior, and in some cases, even result in application stop. &lt;/p&gt;

&lt;h3&gt;
  
  
  Preventing Application Stop
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Logging Errors
&lt;/h4&gt;

&lt;p&gt;One of the most common ways to prevent application stop due to errors is by logging errors. You can use the  log  package provided by Golang to log errors to the console or a file. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "log"
    "os"
)
 func main() {
    file, err := os.Open("file.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()
     // your code here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’re using the  log.Fatal()  method to log the error and stop the application. &lt;/p&gt;

&lt;h4&gt;
  
  
  2. Recovering from Panics
&lt;/h4&gt;

&lt;p&gt;Another way to prevent application stop is by recovering from panics. A panic is a built-in function in Golang that stops the current goroutine and starts the panic sequence. To recover from a panic sequence, you can use the  recover()  function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    defer func() {
        if r := recover(); r != nil {
            log.Println("Recovered from panic:", r)
        }
    }()
     panic("Something went wrong")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’re using the  defer  statement to register a function that will be called when the panic sequence starts. This function uses the  recover()  function to stop the panic sequence and log the error. &lt;/p&gt;

&lt;h4&gt;
  
  
  3. Graceful Shutdown
&lt;/h4&gt;

&lt;p&gt;The third way to prevent application stop is by using graceful shutdown. Graceful shutdown is a technique used to stop an application gracefully without losing any data. Here’s an example of how to implement graceful shutdown in Golang:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "context"
    "os"
    "os/signal"
    "syscall"
)
 func main() {
    ctx, cancel := context.WithCancel(context.Background())
     c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
     go func() {
        &amp;lt;-c
        cancel()
    }()
     // your code here
     &amp;lt;-ctx.Done()
    log.Println("Application stopped gracefully")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’re creating a  context  object and a  cancel  function. We’re also creating a channel to listen for signals that may cause the application to stop, like  SIGTERM  or  os.Interrupt . When one of these signals is received, the  cancel  function is called to stop the application gracefully. &lt;/p&gt;

&lt;h3&gt;
  
  
  Some best practices for error handling in Golang
&lt;/h3&gt;

&lt;p&gt;1 - &lt;strong&gt;Always handle errors&lt;/strong&gt;: In Golang, it's essential to handle errors correctly. Ignoring errors can lead to unexpected behavior and even cause your application to crash. When you encounter an error, you should handle it appropriately. You can log the error, return an error value to the caller, or use panic and recover to handle errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;file, err := os.Open("filename.txt")
if err != nil {
   log.Fatal(err)
}
defer file.Close()
// your code here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using the  os.Open()  function to open a file. If an error occurs, we log the error and stop the program. If there's no error, we defer closing the file and continue with our code.&lt;/p&gt;

&lt;p&gt;2 - &lt;strong&gt;Use descriptive error messages&lt;/strong&gt;: When you encounter an error, make sure the error message is descriptive and helpful. The error message should contain information about what went wrong and where the error occurred. This information can help you debug the error and fix it quickly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_, err := http.Get("http://example.com")
if err != nil {
   log.Printf("Error making HTTP request: %s", err)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're making an HTTP request using the  http.Get()&amp;gt;  function. If an error occurs, we log the error with a descriptive message. &lt;/p&gt;

&lt;p&gt;3 - &lt;strong&gt;Wrap errors&lt;/strong&gt;: When you call a function that returns an error, it's a good practice to wrap the error with additional information. Wrapping errors can help you understand the context in which the error occurred and help you debug the error more easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func doSomething() error {
   _, err := someFunction()
   if err != nil {
      return fmt.Errorf("Error doing something: %w", err)
   }
   // your code here
   return nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're wrapping the error returned by the  someFunction()  function with additional context information using the  fmt.Errorf()  function. &lt;/p&gt;

&lt;p&gt;4 - &lt;strong&gt;Use error types&lt;/strong&gt;: In Golang, you can define custom error types. Using error types can make your error handling more robust and help you distinguish between different types of errors. For example, you can define an error type for network errors, database errors, or file system errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type MyError struct {
   Msg string
   Code int
}
func (e *MyError) Error() string {
   return e.Msg
}
func doSomething() error {
   _, err := someFunction()
   if err != nil {
      return &amp;amp;MyError{
         Msg: "Error doing something",
         Code: 500,
      }
   }
   // your code here
   return nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're defining a custom error type  MyError  and using it to return a specific error when an error occurs in the  doSomething()  function. &lt;/p&gt;

&lt;p&gt;5 - &lt;strong&gt;Use defer&lt;/strong&gt;: Defer is a built-in function in Golang that allows you to schedule a function call to execute after the current function returns. Using defer can help you avoid forgetting to handle errors by ensuring that the error handling code is always executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;file, err := os.Open("filename.txt")
if err != nil {
   log.Fatal(err)
}
defer file.Close()
// your code here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using  defer  to ensure that the  file.Close()  function is always called, even if an error occurs.&lt;/p&gt;

&lt;p&gt;6 - &lt;strong&gt;Use context&lt;/strong&gt;: Context is a powerful concept in Golang that allows you to propagate deadlines and other request-scoped values across API boundaries and between processes. Using context can help you manage errors more effectively by providing a way to cancel an operation or request if it takes too long to complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func doSomething(ctx context.Context) error {
   // set a deadline of 10 seconds
   ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second))
   defer cancel() // cancel the context when the function returns
   // your code here
   return nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using  context  to set a deadline of 10 seconds for an operation. If the operation takes longer than 10 seconds, the context is canceled and an error is returned.&lt;/p&gt;

&lt;p&gt;By following these best practices, you can ensure that your Golang code is more robust and that your error handling is effective.&lt;/p&gt;

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

&lt;p&gt;Error handling is an essential part of any programming language, and Golang is no exception. By following the methods outlined in this article, you can effectively handle errors in your Golang applications and prevent them from stopping unexpectedly.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Golang Pointers and Functions: A Guide with Examples</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Wed, 17 May 2023 02:52:56 +0000</pubDate>
      <link>https://dev.to/adriandy89/golang-pointers-and-functions-a-guide-with-examples-1paj</link>
      <guid>https://dev.to/adriandy89/golang-pointers-and-functions-a-guide-with-examples-1paj</guid>
      <description>&lt;p&gt;In this article, we'll explore how Golang pointers work in functions, and we'll provide examples of using them with and without memory references.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Pointers in Golang?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A pointer is a variable that contains the memory address of another variable. In Golang, a pointer is represented by an asterisk (*) followed by the data type of the variable it points to. For example, the following code declares a pointer to an integer variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var ptr *int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This declares a pointer named "ptr" that points to an integer variable. To assign the address of a variable to a pointer, use the "&amp;amp;" operator. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num int = 42
ptr = &amp;amp;num
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This assigns the address of the "num" variable to the "ptr" pointer. Now, the "ptr" pointer points to the "num" variable in memory.&lt;/p&gt;

&lt;p&gt;Using Pointers in Functions&lt;/p&gt;

&lt;p&gt;When a pointer is passed to a function, the function can modify the value of the variable it points to directly in memory. This is much more efficient than passing a copy of the variable to the function, which requires creating a new variable in memory.&lt;/p&gt;

&lt;p&gt;Let's take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func updateValueWithRef(chain *string) {
    fmt.Printf("%p\n", &amp;amp;*chain)
    *chain = "Hello from function"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function takes a pointer to a string variable as its argument. Inside the function, we use the "*" operator to dereference the pointer and access the value of the variable it points to. Then, we can modify the value of the variable directly in memory by using the "=" assignment operator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With Memory References&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's take a look at an example of using a pointer with a memory reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chain := "Hello world"
fmt.Printf("%p\n", &amp;amp;chain)
fmt.Printf("Before function: %s\n", chain)
updateValueWithRef(&amp;amp;chain) // send a reference to the original
fmt.Printf("After function with pointer: %s\n", chain)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we declare a string variable named "chain" and assign it the value "Hello world". We then print the memory address of the "chain" variable using the "&amp;amp;" operator.&lt;/p&gt;

&lt;p&gt;Next, we call the "updateValueWithRef" function and pass it a reference to the "chain" variable using the "&amp;amp;" operator. Inside the function, we modify the value of the variable directly in memory using the "*" operator.&lt;/p&gt;

&lt;p&gt;After the function call, we print the value of the "chain" variable again, and we can see that it has been modified by the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without Memory References&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To understand the difference between using a pointer with and without memory references, let's take a look at an example of using a pointer without a memory reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func updateValue(chain string) {
    fmt.Printf("%p\n", &amp;amp;chain)
    chain = "Hello from function"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, we take a string variable as its argument. Inside the function, we modify the value of the variable using the "=" assignment operator.&lt;/p&gt;

&lt;p&gt;Now, let's call this function instead of the "updateValueWithRef" function in our previous example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chain := "hello world"
fmt.Printf("%p\n", &amp;amp;chain)
fmt.Printf("Before function: %s\n", chain)
updateValue(chain) // send a copy, not reference
fmt.Printf("After function: %s\n", chain)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we call the "updateValue" function and pass it a copy of the "chain" variable. Inside the function, we modify the value of the copy using the "=" assignment operator.&lt;/p&gt;

&lt;p&gt;After the function call, we print the value of the "chain" variable again, and we can see that it has not been modified by the function.&lt;/p&gt;

&lt;p&gt;Full code example:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
    chain := "Hello world"
    fmt.Printf("%p\n", &amp;amp;chain)
    fmt.Printf("Before function: %s\n", chain)
    updateValue(chain) // send a copy, not reference
    fmt.Printf("After function: %s\n", chain)
    updateValueWithRef(&amp;amp;chain) // send a reference
    fmt.Printf("After function with pinter: %s\n", chain)
}

func updateValue(chain string) {
    fmt.Printf("%p\n", &amp;amp;chain)
    chain = "Hello from function"
}

// TODO - pointers *
func updateValueWithRef(chain *string) {
    fmt.Printf("%p\n", &amp;amp;*chain)
    *chain = "Hello from function with ref"
}

/*
    Output:
    0xc00009e230
    Before function: Hello world
    0xc00009e250
    After function: Hello world
    0xc00009e230
    After function with pinter: Hello from function with ref
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In conclusion, pointers are a powerful feature of Golang that enable developers to write more efficient code by manipulating data directly in memory. When using pointers in functions, it's important to understand the difference between using them with and without memory references, as this can affect how the function modifies the value of the variable it points to.&lt;/p&gt;

&lt;p&gt;By understanding how pointers and functions work together in Golang, developers can write more efficient and scalable code that takes advantage of the language's unique features.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Slicing in Golang: Understanding the Basics</title>
      <dc:creator>Adrian DY</dc:creator>
      <pubDate>Tue, 16 May 2023 03:19:24 +0000</pubDate>
      <link>https://dev.to/adriandy89/slicing-in-golang-understanding-the-basics-2d8l</link>
      <guid>https://dev.to/adriandy89/slicing-in-golang-understanding-the-basics-2d8l</guid>
      <description>&lt;p&gt;Slicing is an essential concept in Golang that allows you to take a portion of a slice without actually copying the underlying data. In this article, we'll explore what slicing is, how it works, and how it can be used to manipulate data. We’ll also dive into an important facet of slicing: how changing the capacity of a slice can affect its sub-slices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Slicing in Golang?&lt;/strong&gt;&lt;br&gt;
 A slice in Golang is a dynamically-sized array. It's created using the make() built-in function and has a type that includes the element type and the size of the array. However, unlike an array, a slice is dynamic; its size can be changed at run-time.&lt;br&gt;
 &lt;strong&gt;Slicing is the process of taking a portion of a slice and creating a new slice from it&lt;/strong&gt;. The new slice refers to the same underlying array as the original slice, which means that any changes made to the new slice will be reflected in the original slice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Taking a Sub-Slice in Golang&lt;/strong&gt;&lt;br&gt;
 To take a sub-slice of a slice, you can use the slice operator. This operator has two parameters: the start index and the end index of the sub-slice. The start index is inclusive, while the end index is exclusive. &lt;/p&gt;

&lt;p&gt;Let's take a look at an example:&lt;br&gt;
slicen1 := []int{1, 2, 3, 4, 5}&lt;br&gt;
subSlicen := slicen1[1:3]&lt;/p&gt;

&lt;p&gt;In this example, we take a sub-slice of  &lt;code&gt;slicen1&lt;/code&gt;  from the 1st index to the 3rd index (exclusive). The resulting sub-slice is  &lt;code&gt;{2, 3}&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sub-Slices and Changing Capacity&lt;/strong&gt;&lt;br&gt;
 When you create a sub-slice in Golang, it refers to a portion of the underlying array of the original slice. This means that any changes made to the sub-slice will be reflected in the original slice.&lt;br&gt;
 However, there is an important caveat to keep in mind: &lt;strong&gt;&lt;em&gt;if you append a new value to the original slice that causes its capacity to increase, it will no longer refer to the same underlying array, and the sub-slice will lose its reference&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
 Let's look at the code example:&lt;br&gt;
&lt;/p&gt;

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

import "fmt"

func main() {
    numbers := []int{1, 2, 3}
    subNumbers := numbers[:2]
    fmt.Println("numbers: ", numbers)
    fmt.Printf("Len: %d, Capac: %v, Memory Ref: %p \n", len(numbers), cap(numbers), numbers)
    fmt.Println("subNumbers: ", subNumbers)
    fmt.Printf("Sub - Len: %d, Capac: %v, Memory Ref: %p \n", len(subNumbers), cap(subNumbers), subNumbers)
    numbers[1] = 10
    fmt.Println("numbers[1] = 10")
    fmt.Println("numbers: ", numbers)
    fmt.Println("subNumbers: ", subNumbers)
    numbers = append(numbers, 4)
    fmt.Println("append(numbers, 4)")
    fmt.Println("numbers: ", numbers)
    fmt.Printf("Len: %d, Capac: %v, Memory Ref: %p \n", len(numbers), cap(numbers), numbers)
    fmt.Println("subNumbers: ", subNumbers)
    fmt.Printf("Sub - Len: %d, Capac: %v, Memory Ref: %p \n", len(subNumbers), cap(subNumbers), subNumbers)
    numbers[1] = 20
    fmt.Println("numbers[1] = 20")
    fmt.Println("numbers: ", numbers)
    fmt.Printf("Len: %d, Capac: %v, Memory Ref: %p \n", len(numbers), cap(numbers), numbers)
    fmt.Println("subNumbers: ", subNumbers)
    fmt.Printf("Sub - Len: %d, Capac: %v, Memory Ref: %p \n", len(subNumbers), cap(subNumbers), subNumbers)
    numbers = append(numbers, 5)
    fmt.Printf("Len: %d, Capac: %v, Memory Ref: %p \n", len(numbers), cap(numbers), numbers)
    numbers = append(numbers, 6)
    fmt.Printf("Len: %d, Capac: %v, Memory Ref: %p \n", len(numbers), cap(numbers), numbers)
    numbers = append(numbers, 7)
    fmt.Printf("Len: %d, Capac: %v, Memory Ref: %p \n", len(numbers), cap(numbers), numbers)

    /*
       Output:
        numbers:  [1 2 3]
        Len: 3, Capac: 3, Memory Ref: 0xc00001c1c8 
        subNumbers:  [1 2]
        Sub - Len: 2, Capac: 3, Memory Ref: 0xc00001c1c8 
        numbers[1] = 10
        numbers:  [1 10 3]
        subNumbers:  [1 10]
        append(numbers, 4)
        numbers:  [1 10 3 4]
        Len: 4, Capac: 6, Memory Ref: 0xc000022210 
        subNumbers:  [1 10]
        Sub - Len: 2, Capac: 3, Memory Ref: 0xc00001c1c8 
        numbers[1] = 20
        numbers:  [1 20 3 4]
        Len: 4, Capac: 6, Memory Ref: 0xc000022210 
        subNumbers:  [1 10]
        Sub - Len: 2, Capac: 3, Memory Ref: 0xc00001c1c8 
        Len: 5, Capac: 6, Memory Ref: 0xc000022210 
        Len: 6, Capac: 6, Memory Ref: 0xc000022210 
        Len: 7, Capac: 12, Memory Ref: 0xc00001a120
    */
}

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

&lt;/div&gt;



&lt;p&gt;In this example, we create a slice called  &lt;code&gt;numbers&lt;/code&gt;  with three elements. We create a sub-slice called  &lt;code&gt;subNumbers&lt;/code&gt;  that refers to the first two elements of  &lt;code&gt;numbers&lt;/code&gt; .&lt;br&gt;
 Next, we append an element to  &lt;code&gt;numbers&lt;/code&gt;  and modify its second element's value. As you can see from the output, these changes are reflected in both  &lt;code&gt;numbers&lt;/code&gt;  and  &lt;code&gt;subNumbers&lt;/code&gt; .&lt;br&gt;
 However, when we append another element to  &lt;code&gt;numbers&lt;/code&gt; , its capacity increases to six. Since the sub-slice  &lt;code&gt;subNumbers&lt;/code&gt;  had a capacity of three, it loses its reference to the underlying array and refers to a new array that's created when the  &lt;code&gt;numbers&lt;/code&gt;  slice is appended to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
 In summary, slicing is an essential concept in Golang that allows you to take a portion of a slice without copying the underlying data. When you take a sub-slice of a slice, any changes made to the sub-slice are reflected in the original slice. However, if you append to the original slice and its capacity increases, it will lose its reference to its underlying array, and any sub-slices created from it will no longer reflect changes made to the original slice.&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
