Let's Go - an introduction to Golang
Tags: go, googlelanguage, opensource, languages
Published: 2020-10-09 08:22:03

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! 

##Let's declare some variables
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. 

![Go declaration of variables](https://dev-to-uploads.s3.amazonaws.com/i/8az96mnhf41shfn54aae.jpg)

Wait, what? Wow, I just used := to assign a value to a variable? Cool! Now, _i_ and _s_ know that they are a _int_ and a _string_. 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! 

![Declaration of global variables](https://dev-to-uploads.s3.amazonaws.com/i/mpohevvx3qurnjkpqrfm.jpg)

Now I have all my global variables collected really neat in a _var_ block. I really love the look of that. And it’s the same way you handle your imports.

![Import variables](https://dev-to-uploads.s3.amazonaws.com/i/dmh2bd9l7845bn5jsw49.jpg)

So, is that it? Go is just pretty and neat? No, no. There is more! Let's dive into pointers.

##Let's point with some pointers
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. 

![Pointer](https://dev-to-uploads.s3.amazonaws.com/i/5c248lhu111r5ihmar1b.jpg)

To point to the value hold by *P, the operator & is used. Here I declare a value _i_, and then I point to it with _p_. 

![Point to variable](https://dev-to-uploads.s3.amazonaws.com/i/nmx3qb6zmn14sco5zwmy.PNG)

So, know _p_ is a pointer to _i_. What does this mean? This means that I can read _i_ through p, and that I can write to _i_ through p. 

![Pointer operations](https://dev-to-uploads.s3.amazonaws.com/i/5479gs9i8y3kyrydm8qn.jpg)

And that's pointers! 

##Let's send some data
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 _struct_. A struct is a block of variables which you use as an object for example to send. 

![Struct](https://dev-to-uploads.s3.amazonaws.com/i/uclc0dr55aoifdj7d99d.jpg)

This _messageToSend_ struct holds four ints, a slice of connections and a string. Hold up! A slice? What is that? A _slice_ is just Go's unlimited arrays. This means, that while an array, in Go, needs to have a certain length, slices don't. _Connections_ is just a dynamix-sized array: a slice. 

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 _TCP_ connection. 

![Dial](https://dev-to-uploads.s3.amazonaws.com/i/qikvry82qvmd5q800td3.jpg)

So, as you can see, _dial_ returns two variables: _conn_ and _err_, 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 _msg_ and send it to you connection _conn_.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ynil4nbgubu41q9raeuc.jpg)

And now your message is sent! 
But, how do we receive it then? Also easy - let's look at that. 

##Let's receive some data
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. 

![Listener](https://dev-to-uploads.s3.amazonaws.com/i/c366cfqqgkf6jl1u7ns7.jpg)

To pick up a call, we need to accept the incoming call. This is done by creating a _listener_, 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! 

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 _msg_.

![Received data](https://dev-to-uploads.s3.amazonaws.com/i/jx0c59vqec3zy8ezdnqb.jpg)

Then you just decode the received messages - let Go handle that! It's easy with the GOB library provided by Go. 

![The GOB library](https://dev-to-uploads.s3.amazonaws.com/i/34gi9jux06bmosqce1z9.jpg)

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!  

This was my short introduction to the language of Go. If you have any questions, feel free to comment below!