<?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: k leaf</title>
    <description>The latest articles on DEV Community by k leaf (@kleaf).</description>
    <link>https://dev.to/kleaf</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%2F1173888%2Fd532ad46-a213-44e3-a555-0b154a137a47.png</url>
      <title>DEV Community: k leaf</title>
      <link>https://dev.to/kleaf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kleaf"/>
    <language>en</language>
    <item>
      <title>hi 24</title>
      <dc:creator>k leaf</dc:creator>
      <pubDate>Thu, 12 Oct 2023 22:40:00 +0000</pubDate>
      <link>https://dev.to/kleaf/hi-24-26bd</link>
      <guid>https://dev.to/kleaf/hi-24-26bd</guid>
      <description>&lt;p&gt;hs media&lt;/p&gt;

</description>
    </item>
    <item>
      <title>hi 2</title>
      <dc:creator>k leaf</dc:creator>
      <pubDate>Thu, 12 Oct 2023 22:40:00 +0000</pubDate>
      <link>https://dev.to/kleaf/hi-2-2o3h</link>
      <guid>https://dev.to/kleaf/hi-2-2o3h</guid>
      <description>&lt;p&gt;hs media&lt;/p&gt;

</description>
    </item>
    <item>
      <title>assigning the result of this type assertion to a variable (switch value := value.(type))</title>
      <dc:creator>k leaf</dc:creator>
      <pubDate>Wed, 04 Oct 2023 04:44:18 +0000</pubDate>
      <link>https://dev.to/kleaf/assigning-the-result-of-this-type-assertion-to-a-variable-switch-value-valuetype-25lb</link>
      <guid>https://dev.to/kleaf/assigning-the-result-of-this-type-assertion-to-a-variable-switch-value-valuetype-25lb</guid>
      <description>&lt;p&gt;assigning the result of this type assertion to a variable (switch value := value.(type)) could eliminate type assertions in switch cases&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fg679odd9xz6cwvfq4j.png" class="article-body-image-wrapper"&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-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fg679odd9xz6cwvfq4j.png" alt="warning message code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package main

import "fmt"

func main() {
    values := []interface{}{1, "two", 3.0}

    for _, value := range values {
        switch value.(type) {
        case int:
            v := value.(int)
            fmt.Printf("%d is an integer\n", v)
        case string:
            v := value.(string)
            fmt.Printf("%s is a string\n", v)
        case float64:
            v := value.(float64)
            fmt.Printf("%f is a float\n", v)
        default:
            fmt.Println("Unknown type")
        }
    }
}



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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Change to this&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy5zhv35kadfn4n0lqnnd.png" class="article-body-image-wrapper"&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-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy5zhv35kadfn4n0lqnnd.png" alt="correct version of code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package main

import "fmt"

func main() {
    values := []interface{}{1, "two", 3.0}

    for _, value := range values {
        switch v := value.(type) {
        case int:
            fmt.Printf("%d is an integer\n", v)
        case string:
            fmt.Printf("%s is a string\n", v)
        case float64:
            fmt.Printf("%f is a float\n", v)
        default:
            fmt.Println("Unknown type")
        }
    }
}


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

&lt;/div&gt;

&lt;p&gt;In this example, we have a slice of empty interfaces ([]interface{}) containing values of different types. We use a for loop to iterate through the values, and within the loop, we use a type assertion in the switch statement to determine the actual type of each value and perform different actions based on the type.&lt;/p&gt;

&lt;p&gt;By using the type assertion (value.(type)), we can access the underlying type of value without the need for separate type assertions for each case. This makes the code more concise and eliminates the need for repetitive type checks.&lt;/p&gt;

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