dongzhun6952 2017-08-28 18:37
浏览 16
已采纳

x%y在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 {
        continue
    }
    fmt.Println(n)
}

I'm confused by the n%2 statement.

The output of this is:

1
3
5

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?

  • 写回答

2条回答 默认 最新

  • dpc46827 2017-08-28 18:39
    关注

    This is called a modulus operator, it returns the remainder of a division operation. Hence == 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 %

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?