<?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: art3xias23</title>
    <description>The latest articles on DEV Community by art3xias23 (@art3xias23).</description>
    <link>https://dev.to/art3xias23</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%2F921095%2F2fc2b5b0-90ac-471d-a481-36db1d1cbd3f.png</url>
      <title>DEV Community: art3xias23</title>
      <link>https://dev.to/art3xias23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/art3xias23"/>
    <language>en</language>
    <item>
      <title>Go value/reference types</title>
      <dc:creator>art3xias23</dc:creator>
      <pubDate>Sun, 03 Dec 2023 18:09:53 +0000</pubDate>
      <link>https://dev.to/art3xias23/go-reference-by-value-types-3am</link>
      <guid>https://dev.to/art3xias23/go-reference-by-value-types-3am</guid>
      <description>&lt;p&gt;In go we have several types, most common are: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Numeric Types:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;int (platform-dependent signed integer)&lt;/li&gt;
&lt;li&gt;int8, int16, int32, int64 (signed integers of specific sizes)&lt;/li&gt;
&lt;li&gt;uint (platform-dependent unsigned integer)&lt;/li&gt;
&lt;li&gt;uint8, uint16, uint32, uint64 (unsigned integers of specific sizes)&lt;/li&gt;
&lt;li&gt;float32, float64 (floating-point numbers)&lt;/li&gt;
&lt;li&gt;complex64, complex128 (complex numbers)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Boolean Type:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bool (true/false values)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;String Type:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;string (immutable sequence of bytes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Derived Types:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;array (fixed-size sequence of elements of the same type)&lt;/li&gt;
&lt;li&gt;slice (variable-size sequence dynamically built on top of an array)&lt;/li&gt;
&lt;li&gt;map (unordered collection of key-value pairs)&lt;/li&gt;
&lt;li&gt;struct (composite data type that groups together variables of different types)&lt;/li&gt;
&lt;li&gt;channel (communication mechanism between goroutines)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pointer Types:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;*T (a pointer to a value of type T)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Function Types:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;func (function types)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interface Type:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;interface (a set of method signatures)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All basic types (Numeric, Boolean, String) along with structs and arrays from the derived types, functions and interfaces are passed by value.&lt;/p&gt;

&lt;p&gt;Maps, arrays, slices and channels are passed by reference.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Assigning variables is passing by value
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 10
var b = a

fmt.Printf("a:%p\nb:%p", &amp;amp;a, &amp;amp;b)`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;a:0xc0000a4000&lt;br&gt;
b:0xc0000a4008   &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Providing arguments to functions is passing by value
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func printFunc() {
    var a = 10
    fmt.Printf("a: %p", &amp;amp;a)
    //Passing in a function creates a new value
    PassByValue(a)
}

func PassByValue(a int) {
    fmt.Printf("f :%p", &amp;amp;a)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;a :0xc0000120d0a &lt;br&gt;
f :0xc0000120f0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Reassining reference types creates a new value for the variable holding it, but keeps the underlying structure
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a := []float64{
        97.5,
        105,
        63.5}

    b := a

    fmt.Printf("The variables have different memory allocations")
    fmt.Print("\n")
    fmt.Printf("a: %p", &amp;amp;a)
    fmt.Print("\n")
    fmt.Printf("b: %p", &amp;amp;b)
    fmt.Print("\n")
    fmt.Print("------------------")
    fmt.Print("\n")
    fmt.Print("\n")
    fmt.Printf("The underlying structure keeps it's reference")
    fmt.Print("\n")
    fmt.Printf("a[0]: %p", &amp;amp;a[0])
    fmt.Print("\n")
    fmt.Printf("b[0]: %p", &amp;amp;b[0])
    fmt.Print("\n")
    fmt.Print("------------------")
    fmt.Print("\n")
    fmt.Print("\n")
    fmt.Printf("Confirming they hold the same values")
    fmt.Print("\n")
    fmt.Printf("a: %v", a)
    fmt.Print("\n")
    fmt.Printf("b: %v", b)

&amp;gt; The variables have different memory allocations
&amp;gt; a: 0xc0000a4000
&amp;gt; b: 0xc0000a4018
&amp;gt; ------------------
&amp;gt; The underlying structure keeps it's reference
&amp;gt; a[0]: 0xc0000a6000
&amp;gt; b[0]: 0xc0000a6000
&amp;gt; ------------------
&amp;gt; Confirming they hold the same values
&amp;gt; a: [97.5 105 63.5]
&amp;gt; b: [97.5 105 63.5] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Passing by * and &amp;amp;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;'*' - Dereference operator. When it's used as a prefix to a variable it's pulling the value that pointer is pointing to&lt;/p&gt;

&lt;p&gt;'&amp;amp;' - Address of operator. When it's used as a prefix to a variable it retrieves the memory address where the variable is stored&lt;br&gt;
&lt;/p&gt;

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

fmt.Printf("a: %p", &amp;amp;a)
fmt.Print("\n")

passByReferenceFunc(&amp;amp;a)

func passByReferenceFunc(a *int) {
    fmt.Printf("f: %p", a)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;a: 0xc0000a4000&lt;br&gt;
f: 0xc0000a4000    &lt;/p&gt;
&lt;/blockquote&gt;

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