<?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: Amanda Engel Thilo</title>
    <description>The latest articles on Forem by Amanda Engel Thilo (@engelthilo).</description>
    <link>https://forem.com/engelthilo</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%2F477568%2F1949711b-ed15-4834-a266-e8ec44c5e444.JPG</url>
      <title>Forem: Amanda Engel Thilo</title>
      <link>https://forem.com/engelthilo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/engelthilo"/>
    <language>en</language>
    <item>
      <title>Let's Go - an introduction to Golang</title>
      <dc:creator>Amanda Engel Thilo</dc:creator>
      <pubDate>Fri, 09 Oct 2020 08:22:03 +0000</pubDate>
      <link>https://forem.com/itminds/let-s-go-an-introduction-to-golang-41pf</link>
      <guid>https://forem.com/itminds/let-s-go-an-introduction-to-golang-41pf</guid>
      <description>&lt;p&gt;This post is a short introduction to the language of Go, also known as Golang. Go is an open source programming language created by Google. Go is a system-level language used for programming across large-scale network servers and distributed systems. And it’s really good for that! &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's declare some variables
&lt;/h2&gt;

&lt;p&gt;Let us start with the basics. Go makes declaration, creation of arrays and objects really easy and smooth. To declare a variable you don't need to tell it what its type is... You can, but you don't need to. &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%2Fi%2F8az96mnhf41shfn54aae.jpg" 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%2Fi%2F8az96mnhf41shfn54aae.jpg" alt="Go declaration of variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait, what? Wow, I just used := to assign a value to a variable? Cool! Now, &lt;em&gt;i&lt;/em&gt; and &lt;em&gt;s&lt;/em&gt; know that they are a &lt;em&gt;int&lt;/em&gt; and a &lt;em&gt;string&lt;/em&gt;. Okay, but what if I have a lot of global variables? As a C# developer, I've often experienced the unpleasant situation where I had a lot of variable declarations in my class. Which looked really ugly. Go also has a solution for this! &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%2Fi%2Fmpohevvx3qurnjkpqrfm.jpg" 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%2Fi%2Fmpohevvx3qurnjkpqrfm.jpg" alt="Declaration of global variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now I have all my global variables collected really neat in a &lt;em&gt;var&lt;/em&gt; block. I really love the look of that. And it’s the same way you handle your imports.&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%2Fi%2Fdmh2bd9l7845bn5jsw49.jpg" 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%2Fi%2Fdmh2bd9l7845bn5jsw49.jpg" alt="Import variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, is that it? Go is just pretty and neat? No, no. There is more! Let's dive into pointers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's point with some pointers
&lt;/h2&gt;

&lt;p&gt;Because Go is created based on C++, it has the ability to use pointers. The pointers in Go pretty much work like those you know from C++. The pointer *P points to a P value. Its zero value is nil. &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%2Fi%2F5c248lhu111r5ihmar1b.jpg" 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%2Fi%2F5c248lhu111r5ihmar1b.jpg" alt="Pointer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To point to the value hold by *P, the operator &amp;amp; is used. Here I declare a value &lt;em&gt;i&lt;/em&gt;, and then I point to it with &lt;em&gt;p&lt;/em&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%2Fi%2Fnmx3qb6zmn14sco5zwmy.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%2Fi%2Fnmx3qb6zmn14sco5zwmy.PNG" alt="Point to variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, know &lt;em&gt;p&lt;/em&gt; is a pointer to &lt;em&gt;i&lt;/em&gt;. What does this mean? This means that I can read &lt;em&gt;i&lt;/em&gt; through p, and that I can write to &lt;em&gt;i&lt;/em&gt; through p. &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%2Fi%2F5479gs9i8y3kyrydm8qn.jpg" 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%2Fi%2F5479gs9i8y3kyrydm8qn.jpg" alt="Pointer operations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's pointers! &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's send some data
&lt;/h2&gt;

&lt;p&gt;I started this blog post telling you, that Go is good for distributed systems. So, let's look at that. Go works with something called a &lt;em&gt;struct&lt;/em&gt;. A struct is a block of variables which you use as an object for example to send. &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%2Fi%2Fuclc0dr55aoifdj7d99d.jpg" 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%2Fi%2Fuclc0dr55aoifdj7d99d.jpg" alt="Struct"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This &lt;em&gt;messageToSend&lt;/em&gt; struct holds four ints, a slice of connections and a string. Hold up! A slice? What is that? A &lt;em&gt;slice&lt;/em&gt; is just Go's unlimited arrays. This means, that while an array, in Go, needs to have a certain length, slices don't. &lt;em&gt;Connections&lt;/em&gt; is just a dynamix-sized array: a slice. &lt;/p&gt;

&lt;p&gt;But who do we send this message to? First, we need a connection to a fellow peer. This is done by dialing to a connection, for example a &lt;em&gt;TCP&lt;/em&gt; connection. &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%2Fi%2Fqikvry82qvmd5q800td3.jpg" 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%2Fi%2Fqikvry82qvmd5q800td3.jpg" alt="Dial"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, as you can see, &lt;em&gt;dial&lt;/em&gt; returns two variables: &lt;em&gt;conn&lt;/em&gt; and &lt;em&gt;err&lt;/em&gt;, where conn is the connection we just dialed, and err is a (hopefully empty) error message. Great, now we have a connection. Then we need to send some data! This is easy with Go. You just encrypt you message &lt;em&gt;msg&lt;/em&gt; and send it to you connection &lt;em&gt;conn&lt;/em&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%2Fi%2Fynil4nbgubu41q9raeuc.jpg" 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%2Fi%2Fynil4nbgubu41q9raeuc.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now your message is sent! &lt;br&gt;
But, how do we receive it then? Also easy - let's look at that. &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's receive some data
&lt;/h2&gt;

&lt;p&gt;To receive data, we first need to have a connection. But, didn’t we just create one before? Kind of, but not really. The dial function just dials to some peer. Like when you call your mom - she needs to pick up as well. So that is what we have to do now: pick up the dial from our caller. &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%2Fi%2Fc366cfqqgkf6jl1u7ns7.jpg" 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%2Fi%2Fc366cfqqgkf6jl1u7ns7.jpg" alt="Listener"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To pick up a call, we need to accept the incoming call. This is done by creating a &lt;em&gt;listener&lt;/em&gt;, which listens for calls. When a call is incoming, the listener accepts the call, and boom! A connection is established. Now we just need to receive some data! &lt;/p&gt;

&lt;p&gt;To receive data, we need an object to receive it into. Because of this, we create an empty messageToSend object, which will really be a messageToReceive object. I called it &lt;em&gt;msg&lt;/em&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%2Fi%2Fjx0c59vqec3zy8ezdnqb.jpg" 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%2Fi%2Fjx0c59vqec3zy8ezdnqb.jpg" alt="Received data"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you just decode the received messages - let Go handle that! It's easy with the GOB library provided by Go. &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%2Fi%2F34gi9jux06bmosqce1z9.jpg" 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%2Fi%2F34gi9jux06bmosqce1z9.jpg" alt="The GOB library"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you have a msg object which holds the data received from the caller. You can get the data out by assigning the variables inside the message to new variables. Then all there is left is to play around with it!  &lt;/p&gt;

&lt;p&gt;This was my short introduction to the language of Go. If you have any questions, feel free to comment below!&lt;/p&gt;

</description>
      <category>go</category>
      <category>googlelanguage</category>
      <category>opensource</category>
      <category>languages</category>
    </item>
  </channel>
</rss>
