Learning Golang. Day 2

Learning Golang. Day 2

Today was a slightly slower day for learning Go than was yesterday. However, I learned about flow control statements, and defer, panic, and recover.


It was slower largely because I didn’t get as much sleep last night, so I was far more awake yesterday when I started learning Go. That said, today, I finished the Packages, variables, and functions and Flow control statements: for, if, else, switch and defer in the Basics section of The Go Tour.

The things that stood out most for me were that Go, technically, only has one looping construct, the for loop. However, because some of its components are optional, it can function as several, different looping constructs, such as the while loop in C, Java, & PHP.

Secondly, the break statement isn’t required in switch statements. This might take me a bit of getting used to, as I’m used to being able to write switch statements in PHP that pack one case up against another as a way of checking multiple conditions. For example:

1
2
3
4
5
6
7
8
switch($someValue)
{
  case(1):
  case(2):
  case(3):
    // ... take some action
    break;
}

That said, I don’t see this being an issue, rather just something that I need to get used to.

The language aspects that really grabbed my attention are defer, panic, and recover. Of these three, defer makes the most sense. It’s fascinating how you can set up a function to execute after the surrounding functions return. I see clear advantages in cleaning up resource handles using it.

For panic and recover, I’ll need a bit more time to fully appreciate them. Check out this article on the Go Blog, for an introduction to them.

See you next time!


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

Learning Golang. Day 1
Mon, May 9, 2022

Learning Golang. Day 1

For the longest time, I’ve been meaning to learn another software development language in-depth, in addition to my primary development language, PHP. I’ve decided that the language is Go.


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