-
Notifications
You must be signed in to change notification settings - Fork 0
GoVsGenerics
Andrew Gerrand edited this page Dec 10, 2014
·
3 revisions
This is page tries to enumerate the Use Cases that call for generics and give a Go alternative without using generics.
Who knows, you might find you really didn't need generics for that at all!
FIRST USE CASE: Generic Algorithm on data whose Type you Don't Yet Know
...but needs to follow some Properties or Behaviours...
You just use interfaces to define the properties or behaviours your data needs to be able to be used by your Generic Algorithm. As exemplified by the sort package:
http://golang.org/pkg/sort/#Interface
- map() in Python: map(lambda x:x*2, range(100)):
func Map(f interface{}, v interface{}) interface{} {
// Reflection to solve f and v types
}
The reflect solution performance is really bad
- Polymorphic functions that operate on built-in generic types.
func KeyIntersection(a map[T1]T2, b map[T1]T2) map[T1]T2 {}
- Containers that must store non-interface values. (Various motivations: control memory layout, require an immutable copy...)
This page is under construction.
It feeds from a discussion thread on golang nuts: http://groups.google.com/group/golang-nuts/browse_thread/thread/3d825ac84e742598
- Home
- Getting started with Go
- Working with Go
- Learning more about Go
- The Go Community
- Using the Go toolchain
- Additional Go Programming Wikis
- Online Services that work with Go
- Troubleshooting Go Programs in Production
- Contributing to the Go Project
- Platform Specific Information
- Release Specific Information