Comparison operators

February 12, 2024

I’ll be livestreaming again today! I hope you can join me as I continue where I left off last week, adding some new features to my open-source library, Kivik!

Let’s talk about a mundane detail… that actually has some interesting nuances: Comparisons!

Comparison operators

Comparison operators compare two operands and yield an untyped boolean value.

==    equal
!=    not equal
<     less
<=    less or equal
>     greater
>=    greater or equal

In any comparison, the first operand must be assignable to the type of the second operand, or vice versa.

This is a subtle, but important point: One of the operands must be assignable to the other, but which doesn’t matter.

Wait… how is that possible? Given, say x == y, how could x be assignable to y, but not the other way around? Perhaps the easiest example is comparing against an untyped boolean constant.


type B bool
var x B


if (x == true) {


if (true == x) {

are valid. In the first case, the right hand operand (the untyped constant true) is assignable to the left hand operand (the variable x of type B), but not the other way around (you can’t assign anything to a constant). And in the second example, the reverse is true.

The equality operators == and != apply to operands of comparable types. The ordering operators <, <=, >, and >= apply to operands of ordered types.

The fact that Go has two concepts at play here, ordered and comparable differs from some languages. And sometimes in surprising ways. Perhaps the most surprising is that boolean values are not ordered (more on that tomorrow). In many languages, you can easily convert/typecast between boolean and integer values, such that true translates to 1 and false to 0. In such langauges, it’s usually (if not always) possible to order or sort by a boolean flag, such that false sorts before true. Not so in Go! (Although there have been proposals to change this, but so far they haven’t gotten any serious traction.)

Quotes from The Go Programming Language Specification Version of August 2, 2023

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .