Skip to content

Instantly share code, notes, and snippets.

@sorenvonsarvort
Last active March 28, 2021 15:43
Show Gist options
  • Save sorenvonsarvort/a25f0c56ee11ba8fcf23d97986cb22f1 to your computer and use it in GitHub Desktop.
Save sorenvonsarvort/a25f0c56ee11ba8fcf23d97986cb22f1 to your computer and use it in GitHub Desktop.
golang-1.13-will-support-generics

Golang 1.13 Will Support Generics

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:

https://github.com/golang/proposal#readme

@sorenvonsarvort
Copy link
Author

The coolest thing about it is Github highlights the new Golang syntax with the generics, go doc and go lint also work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment