<?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: Ian</title>
    <description>The latest articles on Forem by Ian (@ianbush100).</description>
    <link>https://forem.com/ianbush100</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%2F299676%2Ff8d2842f-2165-44b0-8a7b-1dd717d36f15.jpg</url>
      <title>Forem: Ian</title>
      <link>https://forem.com/ianbush100</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ianbush100"/>
    <language>en</language>
    <item>
      <title>Trying to understand pointers in Go - What is a pointer anyway? </title>
      <dc:creator>Ian</dc:creator>
      <pubDate>Tue, 11 Aug 2020 00:16:53 +0000</pubDate>
      <link>https://forem.com/ianbush100/trying-understanding-pointers-in-go-what-is-a-pointer-anyway-55bp</link>
      <guid>https://forem.com/ianbush100/trying-understanding-pointers-in-go-what-is-a-pointer-anyway-55bp</guid>
      <description>&lt;p&gt;Over the past couple of months, Go has taken my interest. As I worked through different tutorials and made small projects I noticed an area that was a conceptual gap: pointers. My first language was Java. Since then I've worked in various languages like Groovy, Javascript, and Python. All of the languages I had worked in didn't have this feature or it was hidden away as was with Java (In Java the object type stores a reference to an object, which is just a pointer, more on that &lt;a href="http://math.hws.edu/eck/cs124/javanotes3/java2cpp/s2.html#:~:text=A%20POINTER%20IS%20JUST%20THE,form%20of%20references%20to%20objects.&amp;amp;text=When%20you%20use%20an%20object,the%20actual%20object%20in%20memory."&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;These are my personal notes for learning pointers in general and more specifically in Go. I've linked some of the sources I used in the bottom of the post. They do a much better job than I did, so please check them out!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a pointer?
&lt;/h2&gt;

&lt;p&gt;As Professor Dave Marshal defines it &lt;a href="https://users.cs.cf.ac.uk/Dave.Marshall/C/node10.html"&gt;here&lt;/a&gt; "A pointer is a variable which contains the address in memory of another variable". So pointer points to a memory address. When a variable is created it is stored in memory. When a pointer is declared for that variable it "points" to where that variable is in memory. &lt;/p&gt;

&lt;h2&gt;
  
  
  That's nifty, so what?
&lt;/h2&gt;

&lt;p&gt;What advantage would it be to pass a memory address as opposed to passing a value? Is this just adding more complexity? Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    j := 12
    fmt.Println(j)
    changeJ(j)
    fmt.Println(j)
}

func changeJ(j int) {
    j = 30
    fmt.Println(j)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12
30
12
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is because the function changeJ() doesn't actually change the variable j in the context of the main() function. Let's change changeJ() to return an int.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {

    j := 12
    fmt.Println(j) 
    j = changeJ(j)
    fmt.Println(j) 
}

func changeJ(j int) int{
    j = 30
    fmt.Println(j)
    return j
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12
30
30
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because we are setting j to the value returned by changeJ() now we see j has the updated value. How could we potentially do this with a pointer? &lt;/p&gt;

&lt;h2&gt;
  
  
  Pointers in Go
&lt;/h2&gt;

&lt;p&gt;Pointers in Go are denoted by &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;&amp;amp;&lt;/code&gt; followed by the variable that it is pointing to. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;*&lt;/code&gt; denotes the value of what is in the memory address that it pointer is pointing to. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;amp;&lt;/code&gt; denotes the address location of where the pointer is pointing to. This returns the location in memory. &lt;/p&gt;

&lt;p&gt;So if we have &lt;code&gt;j := 12&lt;/code&gt; and we want to point to the value of &lt;code&gt;j&lt;/code&gt; we would use the symbol &lt;code&gt;*j&lt;/code&gt;. If we want to point to the memory address of &lt;code&gt;j&lt;/code&gt; we would use &lt;code&gt;&amp;amp;j&lt;/code&gt;. Let's look at the following snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    j := 12
    fmt.Println(j) 
    k := &amp;amp;j
    changeJ(k)
    fmt.Println(j) 
}

func changeJ(j *int) {
    *j = 30
    fmt.Println(*j)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12
30
30
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By setting the value of the pointer we can directly change the value of the variable in its memory address. No return statement needed to set a variable or to hold the results of the changeJ() in the main() function. &lt;/p&gt;

&lt;h2&gt;
  
  
  When to use pointers
&lt;/h2&gt;

&lt;p&gt;When should we use pointers? In the faq's &lt;a href="https://golang.org/doc/faq#methods_on_values_or_pointers"&gt;page&lt;/a&gt; for Go it lays out some use cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;"...does the method need to modify the receiver? If it does, the receiver must be a pointer."&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"If the receiver is large, a big struct for instance, it will be much cheaper to use a pointer receiver". So it is less intensive to pass the address of the memory than to pass a large copy. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"If some of the methods of the type must have pointer receivers, the rest should too, so the method set is consistent regardless of how the type is used". &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Credit where credit is due: Things I found helpful
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=93f9_bJQdHk"&gt;Pointer Receivers - Go Lang Practical Programming Tutorial p.8&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://tour.golang.org/moretypes/1"&gt;A Tour of Go: Pointers&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://users.cs.cf.ac.uk/Dave.Marshall/C/node10.html"&gt;Pointers - DaveMarshall&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://golang.org/doc/faq#methods_on_values_or_pointers"&gt;Go FAQs&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://math.hws.edu/eck/cs124/javanotes3/java2cpp/s2.html#:~:text=A%20POINTER%20IS%20JUST%20THE,form%20of%20references%20to%20objects.&amp;amp;text=When%20you%20use%20an%20object,the%20actual%20object%20in%20memory."&gt;David J. Eck - Appendix 1, Section 2&lt;br&gt;
Pointers and Arrays in C++&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>go</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>How mindfulness has helped me solve problems</title>
      <dc:creator>Ian</dc:creator>
      <pubDate>Mon, 29 Jun 2020 00:06:58 +0000</pubDate>
      <link>https://forem.com/ianbush100/how-mindfulness-has-helped-me-solve-problems-4g07</link>
      <guid>https://forem.com/ianbush100/how-mindfulness-has-helped-me-solve-problems-4g07</guid>
      <description>&lt;p&gt;I looked away from my IDE to see the time on the top corner of my laptop screen. I’d been staring deep into my work for longer than I’d planned. Sometimes this is a good thing where all of the pieces are coming together and you enter a blissful state. When you are pleasantly reminded why you love what you do. This was not one of the times. &lt;/p&gt;

&lt;p&gt;This was a time when my body felt tight. My eyes were heavy and hurt. In the back of my mind negative self talk about missing deadlines and busted estimates ran through like the ticker on the bottom of a cable news screen.&lt;/p&gt;

&lt;p&gt;I tried to walk my code into working little by little. Frantically searching the docs of the library I was using. Skimming tutorials. Still nothing. It felt like I was trapped in a tunnel. The tunnel dug by my concentration. But I didn’t have a shovel. &lt;/p&gt;

&lt;p&gt;A lot of folks on the internet talk about taking a walk when you get stuck. Get up, make some coffee, do something else. My problem was I entered this mental state long before I realized that I need to get up and take a walk. &lt;/p&gt;

&lt;p&gt;I wanted a way to not enter that metal space in the first place. It felt like entering a tunnel and every time a line changes and I hit run I’d hope it would mean I could come up for air. It wasn’t how I wanted to approach my work, my hobbies, my side projects. I had to find a different way to point my attention when things got tough. &lt;/p&gt;

&lt;p&gt;I started and stopped meditating many times. Like many positive “habits” I would fall in and out of it. After a couple of years of this cycle, I finally set time aside first thing in the morning. I get up, I walk to the room I meditate in, and I do it. &lt;/p&gt;

&lt;p&gt;I also started looking into some things to shape my perspective. Taoism, Buddhism, Stoicism, and the like. I found that in order to get out of that feeling of being in a tunnel I had to not enter it at all. I starting trying not to let my mind wander as much. I set aside time to think of ideas but didn't let it ruminate over problems I can’t fix. &lt;/p&gt;

&lt;p&gt;I still mess up sometimes. I still fall into being hyperfocused but not actually stepping back and solving the problem at hand. But on the whole, it has been such a value-added for me.  I feel in control when I’m trying to problem-solve. I try to be kind to myself while I work through things and think about what I need to do as opposed to judging how I’m doing. What’s more, I actually recharge my mind when I’m away from my computer. &lt;/p&gt;

&lt;p&gt;When I’m mindful it feels like I can actually look for a solution. I can stop to think while I solve problems and approach things without my mind running commentary in the background. &lt;/p&gt;

</description>
      <category>programing</category>
      <category>development</category>
    </item>
  </channel>
</rss>
