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.


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

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 https://go.dev/tour/basics[Packages, variables, and functions] and https://go.dev/tour/flowcontrol[Flow control statements: for, if, else, switch and defer] in the Basics section of https://go.dev/tour/list[The Go Tour].

The things that stood out most for me were that Go, technically, only has one looping construct, https://gobyexample.com/for[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 https://gobyexample.com/switch[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:

[source,php]

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 https://go.dev/blog/defer-panic-and-recover[the Go Blog], for an introduction to them.

See you link:/learning-golang/day-3/[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