Learning Golang. Day 5

Learning Golang. Day 5

Here we are on day 5 – the end of the first week. Today, I solved the Slices exercise!


As I was working through my morning jobs, I pondered how I’d go about learning Go, today, asking myself:

  • Would I attempt to finish up the Slices exercise in the Go Tour from yesterday?

  • Would I take it a little easier and play around with Goland?

  • Would I do some background reading on some concepts that I’d covered so far, ones which I wanted to dive deeper into?

Goland showing my slices exercise solution

To be honest, at first I thought of just playing around with Goland because it’s Friday; and Friday’s should be easy and relaxed. No? But then, I thought that I’d just be being slack if I did that, avoiding possibly not being able to finish up the Slices exercise from yesterday. A kind of quiet crabwalk away from something I thought might take me too much effort or time.

But, when I sat down and cracked Goland open, I was staring directly at where I left off yesterday with the Slices exercise, dove right in and finished it! Yes. I finished it!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
func Pic(dx, dy int) [][]uint8 {
	var outer [][]uint8
	for i := 0; i < dy; i++ {
		var inner []uint8
		for j := 0; j < dx; j++ {
			element := uint8((i ^ j) + rand.Intn(100))
			inner = append(inner, element)
		}
		outer = append(outer, inner)
	}

	return outer
}

Sure, it’s not the prettiest of solutions. Perhaps. But, it works! Regardless, I’m happy with it.

If I’m completely honest, I had a look at some exercise solutions and took inspiration from a number of them. As each of them seemed to more or less focus around the same ideas, I don’t feel like I’m cheating in any way.

The part that was completely mine, without inspiration from the others was using rand.Intn(100). I wanted to generate a pseudo-random number between 0 & 255, but haven’t quite clicked with the rand.Intn function, yet. To appreciate it better, I’m going to go through Random Numbers on Go by Example. Perhaps just a little more time and I’ll have it.

Here’s the image which my implementation of `Pic generates.

The image generated based on my solution to the Go Tour Slices exercise

Things of interest

Some notable things about today’s session was that I had to double-check what a uint8 is. I feel a little silly about having to do this, as I should know it (I feel).

For complete honesty, it’s an unsigned 8-bit integers (0 to 255) If you’re interested, DigitalOcean’s blog has an excellent post on Go’s data types.

Otherwise, everything went well, and I’m super happy with having passed the test with a bit of originality, right at the 30-minute mark.

See you, Monday!


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

Learning Golang. Day 4
Thu, May 12, 2022

Learning Golang. Day 4

Here I am at day 4. Today, I learned about the range function and started the slices exercise…​but got stuck.


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