<?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: Luka Giorgadze</title>
    <description>The latest articles on DEV Community by Luka Giorgadze (@lukagiorgadze).</description>
    <link>https://dev.to/lukagiorgadze</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%2F499928%2Fb6e9ac11-4686-4c2e-8a82-f4448a00c392.jpeg</url>
      <title>DEV Community: Luka Giorgadze</title>
      <link>https://dev.to/lukagiorgadze</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lukagiorgadze"/>
    <language>en</language>
    <item>
      <title>Gonull: A Go Package for Handling Nullable Values with Ease</title>
      <dc:creator>Luka Giorgadze</dc:creator>
      <pubDate>Tue, 22 Aug 2023 09:06:28 +0000</pubDate>
      <link>https://dev.to/lukagiorgadze/gonull-a-go-package-for-handling-nullable-values-with-ease-1mgc</link>
      <guid>https://dev.to/lukagiorgadze/gonull-a-go-package-for-handling-nullable-values-with-ease-1mgc</guid>
      <description>&lt;p&gt;When working with databases and JSON, developers often come across the challenge of managing nullable values. Nulls can be pesky. They can introduce unexpected errors if not handled correctly, and often require additional checks in the code. To address this, the open-source package &lt;a href="https://github.com/lomsa-dev/gonull"&gt;gonull&lt;/a&gt; steps in as a solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is gonull?
&lt;/h3&gt;

&lt;p&gt;gonull provides a generic Nullable type for Go applications. Its core purpose is to simplify the process of handling nullable values, especially when dealing with databases and JSON.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Features:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Generic Nullable Type: At the heart of &lt;code&gt;gonull&lt;/code&gt; is the &lt;code&gt;Nullable&lt;/code&gt; type, which can hold a nullable value of any specified type. This type has two fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Val&lt;/code&gt;: Holds the actual value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Valid&lt;/code&gt;: A flag indicating whether the value has been set or not.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ease of Creation: You can quickly create a new &lt;code&gt;Nullable&lt;/code&gt; with an initial value using the NewNullable function. This also sets the &lt;code&gt;Valid&lt;/code&gt; flag to true.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database Integration: &lt;code&gt;gonull&lt;/code&gt; makes it seamless to integrate nullable values with &lt;code&gt;database/sql&lt;/code&gt;. The Scan and Value methods enable the Nullable type to act as a nullable field in database operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSON Operations: The package provides built-in methods (&lt;code&gt;UnmarshalJSON&lt;/code&gt; and &lt;code&gt;MarshalJSON&lt;/code&gt;) to handle the serialization and deserialization of nullable values when working with JSON data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Does it Work?
&lt;/h3&gt;

&lt;p&gt;Consider a scenario where you want to represent a &lt;code&gt;Person&lt;/code&gt; with optional fields, such as &lt;code&gt;Age&lt;/code&gt;, &lt;code&gt;Address&lt;/code&gt;, and &lt;code&gt;Height&lt;/code&gt;. These fields might or might not have values, and when serialized into JSON, they should be represented correctly (either with their value or with null).&lt;/p&gt;

&lt;p&gt;Here's a simple example using the &lt;code&gt;gonull&lt;/code&gt; package:&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;"encoding/json"&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;

    &lt;span class="s"&gt;"github.com/lomsa-dev/gonull"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;MyCustomInt&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;MyCustomFloat32&lt;/span&gt; &lt;span class="kt"&gt;float32&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="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;     &lt;span class="n"&gt;gonull&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Nullable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;MyCustomInt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="n"&gt;gonull&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Nullable&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;Height&lt;/span&gt;  &lt;span class="n"&gt;gonull&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Nullable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;MyCustomFloat32&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;jsonData&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;`{"Name":"Alice","Age":15,"Address":null,"Height":null}`&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;person&lt;/span&gt; &lt;span class="n"&gt;Person&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;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonData&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;person&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="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="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;"Unmarshalled Person: %+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="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;marshalledData&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;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&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="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="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="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;"Marshalled JSON: %s&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="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marshalledData&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the &lt;code&gt;Age&lt;/code&gt;, &lt;code&gt;Address&lt;/code&gt;, and &lt;code&gt;Height&lt;/code&gt; fields of the Person struct are of the &lt;code&gt;Nullable&lt;/code&gt; type. When we unmarshal the JSON data, these fields correctly interpret the JSON values, including recognizing null values. When marshaled back into JSON, the fields that are unset (or not valid) are represented as null.&lt;/p&gt;

</description>
      <category>go</category>
      <category>json</category>
      <category>nullable</category>
    </item>
  </channel>
</rss>
