<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sabari Ram on Medium]]></title>
        <description><![CDATA[Stories by Sabari Ram on Medium]]></description>
        <link>https://medium.com/@sabariramc?source=rss-4b6c00000df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_ApZj5NzKV_39Cil.jpg</url>
            <title>Stories by Sabari Ram on Medium</title>
            <link>https://medium.com/@sabariramc?source=rss-4b6c00000df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 14:06:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sabariramc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[GO: Zero values initialization]]></title>
            <link>https://medium.com/@sabariramc/go-zero-values-initialization-5db1f9a1071e?source=rss-4b6c00000df------2</link>
            <guid isPermaLink="false">https://medium.com/p/5db1f9a1071e</guid>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Sabari Ram]]></dc:creator>
            <pubDate>Tue, 19 Nov 2024 15:39:13 GMT</pubDate>
            <atom:updated>2024-11-19T15:39:13.506Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Gopher" src="https://cdn-images-1.medium.com/max/325/1*IPEbBZRtL94PgiWu1OHPog.png" /></figure><p>In golang there is a zero value for variables, different type has different zero values. Lets consider the basic types only,</p><ul><li>0 for numeric types,</li><li>false for the boolean type,</li><li>&quot;&quot; (the empty string) for strings, and</li><li>nil for slice, map, chan, interface.</li></ul><p>How to declare a zero value variables of different type. In go there are two ways to create a new variable, using either of the following keyword/operator</p><ul><li>var</li><li>:=</li></ul><p>Following is the example for creating new variables using var keyword</p><pre> var intVar int   <br> var boolVar bool <br> var strVar string <br> var sliceVar []string <br> var mapVar map[string]int <br> var chVar chan int<br> var interfaceVar interface{} </pre><p>This is straight forward, lets try the same with the := operator</p><pre>intVar := 0<br>boolVar := false<br>strVar := &quot;&quot;</pre><p>The above mentioned ones are used commonly, when it comes to other type map , slice , chan and interface. The following are the practice I have used and seen mostly, (without using make function)</p><pre>sliceVar := []string{} // initalizes the variable is not zero value<br>mapVar := map[string]int{} <br>// chVar := chan int{} -- didnt find the equivalent without make</pre><p>But these are not zero value variables but rather zero length slice/maps. Following is the way to declare new variable with zero value</p><pre> sliceVar := []int(nil)<br> mapVar := map[string]int(nil)<br> chVar := chan int(nil)<br> interfaceVar := interface{}(nil)</pre><p>These declarations comes in handy in generics,</p><p>In go 1.23 a new standard library package is introduced iter and using this package as foundation few new function are introduced in slices and maps standard library packages. Following is the link for same.</p><p><a href="https://tip.golang.org/doc/go1.23#iterators">Go 1.23 Release Notes - The Go Programming Language</a></p><p>One of the function in slices package is a good example, the slices.Collect function.</p><pre>// Collect collects values from seq into a new slice and returns it.<br>func Collect[E any](seq iter.Seq[E]) []E {<br> return AppendSeq(&lt;&lt;pass a nil slice&gt;&gt;, seq) <br>}<br><br>// AppendSeq appends the values from seq to the slice and<br>// returns the extended slice.<br>func AppendSeq[Slice ~[]E, E any](s Slice, seq iter.Seq[E]) Slice {<br> for v := range seq {<br>  s = append(s, v)<br> }<br> return s<br>}</pre><p>In case of not generic function a simple nil will suffice passing a empty slice</p><pre>func Sample(a []int) {<br>  ....<br>}<br><br>func call() {<br>  Sample(nil) //will compile<br>}</pre><p>In case of the generic function the same will trigger cannot infer &lt;type&gt; error</p><pre>// Collect collects values from seq into a new slice and returns it.<br>func Collect[E any](seq iter.Seq[E]) []E {<br> return AppendSeq(nil, seq) //will not compile, error: in call to AppendSeq, cannot infer Slice <br>}</pre><p>One alternative to this could look like</p><pre>// Collect collects values from seq into a new slice and returns it.<br>func Collect[E any](seq iter.Seq[E]) []E {<br>  var x []E // extra decleration that is used only to pass<br>  return AppendSeq(x, seq) //will compile<br>}</pre><p>In the above code the declared variables only use is to pass a nil value, which is a roundabout way to send a nil slice, to say the least.</p><p>The actual implementation looks like following</p><pre>// Collect collects values from seq into a new slice and returns it.<br>func Collect[E any](seq iter.Seq[E]) []E {<br> return AppendSeq([]E(nil), seq) <br>}</pre><p>Compared to the alternative this looks straight forward.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5db1f9a1071e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>