Grokking Advanced Go
0% completed
Type Parameters and Constraints
func SumCents(values []int64) int64 { var total int64 for _, v := range values { total += v } return total } func SumKilograms(values []float64) float64 { var total float64 for _, v := range values { total += v } return total }
An order service needs the first function for prices and the second for shipping weights. The two bodies are identical, and only the type names differ. Before Go 1.18, a program had to keep both copies, accept interface{} and lose compile-time checking, or generate the copies with a tool.
Go 1
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%