<?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>Forem: Emirhan</title>
    <description>The latest articles on Forem by Emirhan (@ashkan90).</description>
    <link>https://forem.com/ashkan90</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%2F174880%2F4f26f420-a025-42ea-8d0e-ac1549661e01.jpeg</url>
      <title>Forem: Emirhan</title>
      <link>https://forem.com/ashkan90</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ashkan90"/>
    <language>en</language>
    <item>
      <title>In_array alternative for Golang. Searching value in 'map, array, slice'.</title>
      <dc:creator>Emirhan</dc:creator>
      <pubDate>Sat, 10 Aug 2019 07:34:07 +0000</pubDate>
      <link>https://forem.com/ashkan90/inarray-alternative-for-golang-searching-value-in-map-array-slice-56c7</link>
      <guid>https://forem.com/ashkan90/inarray-alternative-for-golang-searching-value-in-map-array-slice-56c7</guid>
      <description>&lt;p&gt;Hi, I started golang nowadays. I moved from PHP to Golang and I can say that Golang is too beautiful. &lt;br&gt;
I didn't know there's not search function exists until I decided to write a package.&lt;br&gt;
Basically in PHP, we might be implement our 'in_array' alternative 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;if (is_array($x)) {
   foreach ($x as $v) {
      if ($v == $searchVal) { return true; }
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Of course, This alternative can be used non-multidimensional.&lt;br&gt;
In Golang, we cannot decide type of variable easily. There's a package called 'Reflect'. &lt;br&gt;
Reflect package is not used only getting the type of any variable. Its documentation is &lt;a href="//golang.org/pkg/reflect/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If we understand what we gonna do then we can do!&lt;br&gt;
Let's write the func prototype;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func In_array(search interface{}, array interface{}) bool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;No body want to restrict the search value's type because we want to search any type, any time. Because of that, I used 'interface{}'.&lt;br&gt;
As same as search, incoming value(which is array) must be interface{} too.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "reflect"
)

func In_array(search interface{}, array interface{}) bool {

    val := reflect.ValueOf(array)
    val = val.Convert(val.Type())

    typ := reflect.TypeOf(array).Kind()

    switch typ {
    case reflect.Map:
        s := val.MapRange()

        for s.Next() {
            s.Value().Convert(s.Value().Type())
            for i := 0; i &amp;lt; s.Value().Len(); i++ {
                if deep {
                    if reflect.DeepEqual(search, s.Value().Index(i).Interface()) {
                        return true
                    }
                } else {
                    str := s.Value().Index(i).String()
                    if strings.Contains(str, search.(string)) {
                        return true
                    }
                }
            }
        }
    case reflect.Slice, reflect.Array:
        for i := 0; i &amp;lt; val.Len(); i++ {
            if reflect.DeepEqual(search, val.Index(i).Interface()) {
                return true
            }
        }
    }

    return false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We're detecting type of value with this line of code;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reflect.TypeOf(array).Kind()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This code, prepare 'reflect' package's constants they might be;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Map,
Array,
Slice,
String,
....
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We should iterate the Value by MapRange() if Value is Map&lt;br&gt;
(don't forget, we're not doing recursive stuff. This is just a idea)&lt;br&gt;
Then we're looping current index's value and checking if given search value is equal to map's value.&lt;br&gt;
It's same as nested loops.&lt;/p&gt;

&lt;p&gt;I used the DeepEqual() because, &lt;br&gt;
Docs:&lt;code&gt;&lt;br&gt;
DeepEqual reports whether x and y are “deeply equal,” defined as follows. Two values of identical type are deeply equal if one of the following cases applies. Values of distinct types are never deeply equal.&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And BUM! This is simple and efficient. We can test it with this;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rules := map[string][]string{
    "name":    {"must-be-string", "min-len-20"},
    "surname": {"must-be-string", "min-len-15"},
}
in_array("must-be-string", rules) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There're some changes made because of deep and logical problems. I updated the code so you can use it with ease. &lt;/p&gt;

&lt;p&gt;Here's the source code, &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/ashkan90" rel="noopener noreferrer"&gt;
        ashkan90
      &lt;/a&gt; / &lt;a href="https://github.com/ashkan90/golang-in_array" rel="noopener noreferrer"&gt;
        golang-in_array
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      PHP in_array alternative for golang
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;golang-in_array&lt;/h1&gt;

&lt;/div&gt;

&lt;p&gt;PHP in_array alternative for golang&lt;/p&gt;

&lt;p&gt;It's tested with&lt;/p&gt;

&lt;ul class="contains-task-list"&gt;
&lt;li class="task-list-item"&gt;
 "search any type in array of map"&lt;/li&gt;
&lt;li&gt;[] "any type in recursive map (no depth)"&lt;/li&gt;
&lt;li class="task-list-item"&gt;
 "any type in array"&lt;/li&gt;
&lt;li class="task-list-item"&gt;
 "any type in slice"&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
  &lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/ashkan90/golang-in_array" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


</description>
      <category>go</category>
      <category>array</category>
      <category>search</category>
      <category>map</category>
    </item>
  </channel>
</rss>
