<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0' 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">
  <channel>
    <title>David Pointer</title>
    <description>Pragmatic programmer. Consilient alchemist. Serial hobbyist. Pro geek. Miniature tabletop gamer.</description>
    <link>https://dpointer80906.silvrback.com/feed</link>
    <atom:link href="https://dpointer80906.silvrback.com/feed" rel="self" type="application/rss+xml"/>
    <category domain="dpointer80906.silvrback.com">Content Management/Blog</category>
    <language>en-us</language>
      <pubDate>Wed, 20 Feb 2019 10:30:51 -0700</pubDate>
    <managingEditor>david.b.pointer@gmail.com (David Pointer)</managingEditor>
      <item>
        <guid>https://dpointer80906.silvrback.com/casting-go-interface-to-struct#46583</guid>
          <pubDate>Wed, 20 Feb 2019 10:30:51 -0700</pubDate>
        <link>https://dpointer80906.silvrback.com/casting-go-interface-to-struct</link>
        <title>Casting Go interface to struct</title>
        <description>Learning to think in Go</description>
        <content:encoded><![CDATA[<p>I was using an excellent go <a href="https://godoc.org/golang.org/x/net/icmp">icmp library</a> recently. I got hung up on dealing with a received <a href="https://godoc.org/golang.org/x/net/icmp#MessageBody">MessageBody</a> type that I knew was really an <a href="https://godoc.org/golang.org/x/net/icmp#Echo">Echo</a> type, but I could not refer to the fields in the <code>Echo</code> struct. I think my C/C++/Python background got in my way here.</p>

<p>After a bit of head scratching, I noticed the <code>MessageBody</code> type was defined an an interface and the <code>Echo</code> type was defined as a struct. Ah! A clue! I bet I can <a href="https://stackoverflow.com/questions/49448302/golang-convert-interface-to-struct">cast an interface type to a struct type</a>.</p>

<p>Thusly:  </p>
<div class="highlight"><pre><span></span>switch rxMsg.Type {
case ipv4.ICMPTypeEchoReply:
    body, _ := rxMsg.Body.(*icmp.Echo)
</pre></div>
<p>where <code>rxMsg.Body</code> is the received <code>icmp.MessageBody</code> interface and <code>body</code> is the desired <code>icmp.Echo</code> struct.</p>

<p>Insert blinding flash o&#39; light here. Using an interface definition, this Go icmp library author allows the library user to easily convert an received message body into its appropriate struct equivalent. Whoa.  That sort of power was not so obvious to me when I first read about the Go interface.</p>
]]></content:encoded>
      </item>
      <item>
        <guid>https://dpointer80906.silvrback.com/simple-defer-d-error-handling-in-go#46548</guid>
          <pubDate>Fri, 15 Feb 2019 10:47:00 -0700</pubDate>
        <link>https://dpointer80906.silvrback.com/simple-defer-d-error-handling-in-go</link>
        <title>Simple defer&#39;d Error Handling in Go</title>
        <description></description>
        <content:encoded><![CDATA[<p>I recently wrote a Go func that starts off by opening a network listening connection and defers closing that connection.</p>
<div class="highlight"><pre><span></span>connection, err := icmp.ListenPacket(...)
if err != nil {
    panic(err)
}
defer connection.Close()
</pre></div>
<p>The defer&#39;d <code>connection.Close()</code>returns an error, but how to capture and do something with that error if it occurs?</p>

<p>A solution was not so obvious to me. Thankfully I stumbled upon a very <a href="https://pocketgophers.com/handling-errors-in-defer/">comprehensive and detailed answer</a>.A bit of overkill (for my purposes) that led me to this simple implementation.</p>
<div class="highlight"><pre><span></span>    connection, err := icmp.ListenPacket(...)
    if err != nil {
        panic(err)
    }
    defer func() {
        err = connection.Close()
        if err != nil {
            fmt.Printf(&quot;%v\n&quot;, err)
        }
    }()
</pre></div>
<p>Thank you most kindly <a href="https://pocketgophers.com">pocketgophers</a>. Life is good.</p>
]]></content:encoded>
      </item>
  </channel>
</rss>