Every developer knows generics support is the most wanted feature in any major programming language. A few months ago Rob Pike had already approved several Golang proposals, one of them included the new generics feature. This decision is made not only for engaging more developers to the community, but also for boosting existing projects and scaling their codebases.
https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md
According to it the generics in Golang are based on the contracts and have a pretty familiar syntax. Here are some examples:
contract Addable(t T) { // "addable" contract definition
t + t
}
func Sum(type T Addable)(points []T) T {
var total T
for _, point := range points {
total += point // we can easily use the "+" operator since, the T type is Addable
}
return total
}
Some of proposals will be implemented in the newer Golang versions. Every developer can publish his own proposal by the link:
The coolest thing about it is Github highlights the new Golang syntax with the generics,
go doc
andgo lint
also work as expected.