Yesterday we started on comparison operators. Today we’ll continue, but there are a lot of types to cover, so I’ll break this down into a two or three parts, to keep each day’s email easily digestible.
Recall that we had just been introduced to the concepts of ordered and comparable, and this is where we pick up…
Comparison operators
… These terms and the result of the comparisons are defined as follows:
- Boolean types are comparable. Two boolean values are equal if they are either both
true
or bothfalse
.
Recall from yesterday that in contrast to many other languages, Boolean values are not ordered in Go.
- Integer types are comparable and ordered. Two integer values are compared in the usual way.
LOL. The “usual way”. I love it when the spec occasionally “breaks character” and uses normal language like this.
Floating-point types are comparable and ordered. Two floating-point values are compared as defined by the IEEE-754 standard.
Consult IEEE-754 if you really care where this differs from “the usual way”. You probably don’t.
- Complex types are comparable. Two complex values
u
andv
are equal if bothreal(u) == real(v)
andimag(u) == imag(v)
.
I’d be surprised if you ever use a complex number in Go, so you probably won’t care about how they compare, either.
- String types are comparable and ordered. Two string values are compared lexically byte-wise.
This is probably intuitive. abc
and abd
are not equal, and the former is ordered before the latter. One thing to note: Sorting strings this way (bytewise, that is) isn’t always the preferred sorting approach. Many languages, or even different locales with the same language, have different ordering rules (i.e. does uppercase or lowercase come first, or are they intermingled?) If you’re sorting human text (say names, or titles of books), particularly in non-English languages, you’d do well to look into one of the i18n-related packages for smarter sorting.
- Pointer types are comparable. Two pointer values are equal if they point to the same variable or if both have value
nil
. Pointers to distinct zero-size variables may or may not be equal.
Two things to note here:
- Pointers in Go, unlike in languages like C, are not ordered.
- Some types in Go have zero size. Two common examples:
struct{}
and a zero-length array (i.e.[0]int
). These types are handy in that they take up no memory, but this also means that the implementation is free to have all zero-size variables consume the same (zero-length) memory address. This means you cannot reliably compare different instances of zero-lenght values:
var x = struct{}{}
var y = struct{}{}
if x == y { // Technically, it's undefined whether this will be true or false
fmt.Println("same")
}
Quotes from [_The Go Programming Language Specification_](https://go.dev/ref/spec) Version of August 2, 2023