<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Golang on My Personal Blog | Mohammad Abbasi</title><link>https://blog.mohammad-abbasi.me/en/tags/golang/</link><description>Recent content in Golang on My Personal Blog | Mohammad Abbasi</description><language>en</language><lastBuildDate>Wed, 29 Jan 2025 03:20:36 +0330</lastBuildDate><atom:link href="https://blog.mohammad-abbasi.me/en/tags/golang/index.xml" rel="self" type="application/rss+xml"/><item><title>Generics in Go</title><link>https://blog.mohammad-abbasi.me/en/p/generics-in-go/</link><pubDate>Tue, 27 Aug 2024 04:11:34 +0330</pubDate><guid>https://blog.mohammad-abbasi.me/en/p/generics-in-go/</guid><description>&lt;p&gt;You probably know that Go is a statically typed language, which means that every variable in your program must have a predefined type. For example, when we define a function, we need to specify the types of its parameters, like in the following code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;func&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;string&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&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;&lt;/span&gt;&lt;span class="nx"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&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&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here, we have defined a parameter named &amp;ldquo;s&amp;rdquo; with the string type. We can define any number of additional parameters with any types, such as float64, int, or structs. However, the problem arises when we want to pass a number(or other types) to this function because numbers are of type int, and we can&amp;rsquo;t assign an int to a variable of type string.
Previously, we used to define the type of the &amp;ldquo;s&amp;rdquo; parameter as an interface, which partially solved this problem but still had many limitations.&lt;/p&gt;</description></item></channel></rss>