Learning Golang. Day 3

Learning Golang. Day 3

Here I am at day 3. Today, I started learning about pointers, arrays, slices, and structs!

:idseparator: - :idprefix: :experimental: :source-highlighter: rouge :rouge-style: pastie :imagesdir: /images :source-linenums-option: on :tip-caption: đź’ˇ

If I could sum up today’s session in one word, it would be: fascinating. This is mainly because I started learning about https://go.dev/tour/moretypes/1[pointers] again!

== Pointers

Pointers are a topic that has always been more than just a little bit confusing for me, ever since I first started learning them (way back in 1997), when I was learning C. For example, am I accessing the pointer’s value, or it’s location in memory? And, when I update a pointer, I’m not updating it, rather the value at the location in memory which it points to.

Ah, the joys of https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean[pointer derefencing or indirection]. To come back to the topic so many years later – especially after so many years using link:/tags/php[PHP] and other languages such as Ruby, Python, and Java, which don’t have pointers – made today extra special.

I’m determined to drum the concept into my head this time, and I’m heartened by the feeling that, today, it made just that much more sense. That said, while I was playing with them using https://go.dev/tour/moretypes/2[structs], as in the example below, I was a little confused. But after a bit more time, it started to make sense.


numbers := Numbers{21, 48} pointerToFirst := &numbers pointerToFirst.Second = pointerToFirst.First + pointerToFirst.Second

== Structs

I’m pretty familiar with structs, but it still was fun to learn more about them. I’m impressed by how flexible Go makes working with them. Actually, I’m coming to appreciate so many of the small conveniences Go makes.

== Arrays & Slices

What took most of the time in today’s session was understanding https://go.dev/tour/moretypes/6[arrays] and https://go.dev/tour/moretypes/7[slices]. This is mainly because, while they seem pretty straightforward at first, they seem to break their own rules.

For example, I first read that arrays are created with a fixed size which is not able to be changed. Then, I read that slices don’t contain values, rather, they reference an underlying array, which they, in effect, provide a movable window to. What I mean by that is that they can link to a portion of, all of, or none of an underlying array.

Take the example below. It initialises an array, a, which contains 10 integers. Then, it initialises a slice, b, which is a view on the 1st, 2nd, and 3rd elements (2, 3, & 4) of a.


a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} b := a[1:4]

However, later in the Go Tour, I learned about the built-in make function which lets you create dynamically-sized arrays, and the append function which lets you append new values to a slice. To give more background to append, the Tour says:

[quote] If the backing array of the slice is too small to fit all the given values a bigger array will be allocated. The returned slice will point to the newly allocated array.

I get that this is great for convenience, which I’m fully behind. However, as someone getting into the language, it seems to contradict itself.

I could well be wrong here, or be over-thinking it. If I am, and you’re an experienced Go developer, please let me know in the comments.

Regardless, that was a quick summary of today’s session.

See you link:/learning-golang/day-4/[next time!]

You might also be interested in these tutorials too...

Live Reload Go Projects with wgo
Fri, Apr 19, 2024

Live Reload Go Projects with wgo

Building web apps in Go is extremely rewarding. However, as Go’s a compiled language, to see changes, you need to restart the app. Needless to say that’s quite tedious! With live reloading, it doesn’t need to be.

Restrict HTTP Request Methods in Go 1.22
Thu, Apr 11, 2024

Restrict HTTP Request Methods in Go 1.22

Restricting HTTP request methods, when working with net/http prior to Go 1.22 was a little complicated. However, that’s all changed. Now, it’s pretty trivial. In this short tutorial, I’ll show you the new approach.

Go mod tidy - A Quick Introduction
Fri, Apr 5, 2024

Go mod tidy - A Quick Introduction

What is go mod tidy and why you would use it? In this short tutorial you’ll get a brief introduction to what it is and see the essentials of how to use it.

Debug Go Code with Visual Studio Code
Wed, Mar 27, 2024

Debug Go Code with Visual Studio Code

Recently, I started debugging Go code using Visual Studio Code. Some minor hiccups aside, it was pretty trivial to get up and going. This is the short version of what you need to do to get started.

Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus