<?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: iyiola-dev</title>
    <description>The latest articles on DEV Community by iyiola-dev (@iyioladev).</description>
    <link>https://dev.to/iyioladev</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%2F621874%2F9d63d0e7-1d46-4e90-a954-021ee4b45b86.jpg</url>
      <title>DEV Community: iyiola-dev</title>
      <link>https://dev.to/iyioladev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iyioladev"/>
    <language>en</language>
    <item>
      <title>Strings in GO</title>
      <dc:creator>iyiola-dev</dc:creator>
      <pubDate>Thu, 08 Jul 2021 16:34:37 +0000</pubDate>
      <link>https://dev.to/iyioladev/strings-in-go-3d5a</link>
      <guid>https://dev.to/iyioladev/strings-in-go-3d5a</guid>
      <description>&lt;p&gt;The Strings and Strconv library&lt;/p&gt;

&lt;p&gt;Unlike other programming languages like Dart, Java, or Javascript, you can easily add a method to a String or convert a string to int without a package/library. In golang that is not very easy without a library. &lt;/p&gt;

&lt;h3&gt;
  
  
  THE STRCONV LIBRARY
&lt;/h3&gt;

&lt;p&gt;The first one we will be talking about will be the strconv library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://golang.org/pkg/strconv/"&gt;link to the docs&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The most common use of the strconv library/package is converting &lt;code&gt;string to int&lt;/code&gt; or &lt;code&gt;int to String&lt;/code&gt; and few other things will start by importing the package like this:&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;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"strconv"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So for converting int to string or vice versa there are 2 commands :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Itoa: used to convert int to string.&lt;/li&gt;
&lt;li&gt;Atoi: used to convert string to int.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first one we will use is &lt;code&gt;Itoa&lt;/code&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="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;64&lt;/span&gt;
    &lt;span class="n"&gt;letter&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strconv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Itoa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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;"letter is a %T"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this  you should see something like &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;letter is a string&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The next thing is &lt;code&gt;Atoi&lt;/code&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="n"&gt;letter&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"4"&lt;/span&gt;
    &lt;span class="n"&gt;num&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;strconv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Atoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;letter&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;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;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="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;"%T is a number"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this you should see something like &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;int is a number&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Of course, there are a lot more other things you can do with this package. you can check the docs for more functions.&lt;/p&gt;

&lt;p&gt;### THE STRINGS PACKAGE &lt;/p&gt;

&lt;p&gt;&lt;a href="https://golang.org/pkg/strings/"&gt;Link to the official documentation&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You can do many things with this package, but we will only be treating a few for this article.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Replace&lt;/code&gt; function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Split&lt;/code&gt; function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;ToLower&lt;/code&gt; function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;ToUpper&lt;/code&gt; function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Contains&lt;/code&gt; function&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  The &lt;code&gt;Replace&lt;/code&gt; function
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;Replace&lt;/code&gt; function accepts 4 arguments: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The string &lt;/li&gt;
&lt;li&gt;The string to be replaced&lt;/li&gt;
&lt;li&gt;What should the string  it should be replaced with&lt;/li&gt;
&lt;li&gt;The number of replacement occurrences &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;NB: for the number of occurrences, the string should be replaced if the number is a negative integer. It will replace all occurrences, but then instead of passing a negative integer, one can use the &lt;code&gt;ReplaceAll&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So I am going to show 2 examples with different cases of how this is used below.&lt;/p&gt;

&lt;p&gt;1.&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="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"The boy is stupid stupid"&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;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"stupid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"wise"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This replaces the first occurrence of &lt;code&gt;stupid&lt;/code&gt; with &lt;code&gt;wise&lt;/code&gt; because we specified that it should be done only once with the integer &lt;code&gt;1&lt;/code&gt;. If we replace it with &lt;code&gt;2&lt;/code&gt; it will change both stupid to wise.&lt;/p&gt;

&lt;p&gt;2.&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="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"The boy is stupid stupid"&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;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"stupid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"wise"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should show &lt;code&gt;The boy is wise wise&lt;/code&gt; as a result when you run it.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Split Function
&lt;/h4&gt;

&lt;p&gt;The split function takes in 2 arguments the strings to be separated and what should be used as the string to separate them then it returns a slice containing the strings separated. If there is no result it will return the slice with only one element which is the string. An example will be shown below:&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="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"season 03,season02,season01"&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;"%q&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;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result when this runs will be &lt;code&gt;["season 03" "season02" "season01"]&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The ToLower Function
&lt;/h4&gt;

&lt;p&gt;The ToLower function as the name suggests turns a string to lower case. It accepts only one argument, the string to be turned. An example will be shown below&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="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"The Boy is Foolish"&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;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToLower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when this runs the result will be &lt;code&gt;the boy is foolish&lt;/code&gt; which means the case has been changed to lowercase.&lt;/p&gt;

&lt;h4&gt;
  
  
  The ToUpper Function
&lt;/h4&gt;

&lt;p&gt;The ToUpper function as the name suggests turns a string to uppercase. It accepts only one argument, the string to be turned. An example will be shown below&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="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"the boy is foolish"&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;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToUpper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when this runs the result will be &lt;code&gt;THE BOY IS FOOLISH&lt;/code&gt; which means the case has been changed to uppercase.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Contains Function
&lt;/h4&gt;

&lt;p&gt;The contains functions check if a substring is part of a string provided, so it takes 2 arguments, the string to be checked and the substring, then it returns a bool. An example is provided below&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="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"trinkets"&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"rin"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns a boolean of true because the word rin can be found in trinkets otherwise it would have returned false.&lt;/p&gt;

&lt;p&gt;A whole lot more can be done with the &lt;code&gt;Strconv&lt;/code&gt; and the &lt;code&gt;Strings&lt;/code&gt; package check out the docs for more&lt;/p&gt;

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