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!


:idseparator: - :idprefix: :experimental: :source-highlighter: rouge :rouge-style: pastie :imagesdir: /images :source-linenums-option: on :tip-caption: 💡

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

image::posts/learning-golang/goland-with-the-slices-exercise-code.png[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!

[source,go]

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 https://gist.github.com/tetsuok/2280162[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 https://en.wikipedia.org/wiki/Pseudorandomness[a pseudo-random number] between 0 & 255, but haven’t quite clicked with the https://pkg.go.dev/math/rand#Rand.Intn[rand.Intn] function, yet. To appreciate it better, I’m going to go through https://gobyexample.com/random-numbers[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.

image::posts/learning-golang/slices-exercise-image.png[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 https://www.digitalocean.com/community/tutorials/understanding-data-types-in-go[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, link:/learning-golang/day-6/[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