Close this search box.

Golang: Getting started with If statements

If statements in Golang are pretty simple, so this is going to be a really short article. What I would like to do, is compare the same if statements in Python and Golang to give you a feel for how the two languages differ in their constructs.

In Python, we’d have something like:

Here, we make some simple statements about the value of x. We can do the same in Golang. Here are the differences:

  1. We must assign this to package main, as it’s an executable piece of code
  2. We must import the fmt module to make printing possible
  3. The if statement must live within the function
  4. Instead of a colon after the condition, we use curly braces

And that’s it. If you are familiar with Python or another language, you’ll probably be very comfortable with this kind of syntax.

Share the Post:

Related Posts