30秒学会 Golang 片段 · 2020年2月28日

30秒学会 Golang 片段 – IsOdd

Returns true if the given number is odd, false otherwise.

Checks whether a number is odd or even using the modulo (%) operator.
Returns true if the number is odd, false if the number is even.

代码实现

func IsOdd(n int) bool {
    return n % 2 == 1
}

使用样例

IsOdd(3) // true