What kind of operation is x%y in golang?


I’m going through some golang tutorials, and I came across this for loop:

for n := 0; n <= 5; n++ {
    if n%2 == 0 {

I’m confused by the n%2 statement.

The output of this is:


It looks like these are not multiples of 2, but I’m not understanding the == 0 part of the statement if that’s the case? Is there a resource that talks about this operation, or something I should look up?


This is called the remainder operator, it returns the remainder of a division operation. Hence X % Y == 0 will be true when X can be evenly divided by Y.

This operator and % to represent it is common in many languages.

See related question: Understanding The Modulus Operator %

Answered By – Cameron Roberts

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.