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.
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?
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.
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!
Here I am at day 4. Today, I learned about the range function and started the slices exercise…but got stuck.
Today, on day 14, I created a custom method to remove code duplication creeping into the weather station codebase. Come read the fun story behind getting that done.
Here we are on day 13. Today, I continued learning Golang by working on the Golang version of my PHP/Python weather station, adding a function to render static pages. Let me share my learnings with you.
Here we are on day 12. I didn’t solve anything. I’m feeling that these exercises are becoming arbitrary and pointless.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus