The section "Go Programming Language Specification" on Comparison Operators makes me think that a structure containing only comparable fields should be comparable:
Structure values ββare comparable if all their fields are comparable. Two structure values ββare equal if their respective non-empty fields are equal.
Thus, I would expect the following code to compile, since all fields in the Student structure are comparable:
package main type Student struct { Name string // "String values are comparable and ordered, lexically byte-wise." Score uint8 // "Integer values are comparable and ordered, in the usual way." } func main() { alice := Student{"Alice", 98} carol := Student{"Carol", 72} if alice >= carol { println("Alice >= Carol") } else { println("Alice < Carol") } }
However, the message failed to compile :
invalid operation: alice> = carol (operator> = not defined in the structure)
What am I missing?
comparison struct go
maerics
source share