Grokking Advanced Go
0% completed
The cmp, slices, and maps Packages
The two functions below compare orders by the same three rules: customer name in ascending order, then total in descending order, then ID in ascending order. They return the same result for any two orders, and only the first writes every branch.
func compareByHand(a, b Order) int { if a.Customer != b.Customer { if a.Customer < b.Customer { return -1 } return 1 } if a.Cents != b.Cents { if a.Cents > b.Cents { return -1 } return 1 } if a.ID < b.ID { return -1 } if a.ID > b.ID { return 1 } return 0 } func compareOrders(a, b Order) int { return cmp.Or(
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%