DEV Community

Amanda Engel Thilo for IT Minds

Posted on

Let's Go - an introduction to Golang

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

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

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

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.


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

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

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.


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.


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

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.


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

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

The GOB library

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!

Top comments (0)