This is a creation in Article, where the information may have evolved or changed.
Before we start the article, we need to figure out what "generic programming" is.
In the simplest definition, generic programming was a style of computer programming in which algorithm be written in terms of types To-be-specified-later that is then instantiated if needed for specific types provided as parameters. –from Wikipedia.
In a nutshell, the code we write is not valid for a particular type (for example, for int, not for string), but for most types of arguments.
Let's look at a C + + example:
1 2 3 4 5 6 7 8 9 10
|
#include <algorithm> #include <vector>
int main() { std::vector<int> a{3,1,2,4 ,5}; std::vector<string> b{"Golang", "I", "AM"}; std:: Sort (A.begin (), A.end ()); //after This, a={1,2,3,4,5} std:: Sort (B.begin (), B.end ()); //after This, b={"I", "AM", "Golang"} return 0; }
|
The sort function here is a generic programming example. As for why {"Golang", "I", "AM"} are sorted after the {"I", "AM", "Golang"} because the comparison function of string type is the dictionary order. A careful person may notice that the vector is initialized directly using curly braces, yes, this is c++11.
Does go have generic programming?
No.
Why does go have no generic programming?
Here to apply the official website answer
Why does Go don't have generic types?
Generics is added at some point. We don ' t feel an urgency for them. Generics is convenient but they come at a cost in complexity in the type system and run-time ...
Meanwhile, Go ' s built-in maps and slices, plus the ability to use the empty interface to construct containers mean in many Cases it is possible to write code this does what generics would enable, if less smoothly.
Translation, even though the generics are good, but it will increase the complexity of our language design, so we now do not intend to support the future may be supported. In addition, although we are now very weak, but the use of interface is also possible to achieve generics (hehe).
Go's generic programming implementation
As I said before, using go interface can be used for generic programming, so let's take a look at interface.
Duck Typing
This introduces a concept, duck typing.
When I see a bird so walks like a duck and swins like a duck and quacks like a duck, I call that bird a duck. –james Whitcomb Riley
Based on the definition of Wikipedia, duck typing is a type definition method for object-oriented programming languages. We judge that a god horse is not judged by its type definition, but rather whether it satisfies some particular method or attribute definition.
Interface
So what is interface in go? Interface is a set of methods. We can think of it as a dynamic data type that defines internal methods, and any data type that implements these methods can be considered a specific data type.
Generic programming
In fact, the go language also provides the sort function, we look at the source code, Src/sort/sort.go.
1 2 3 4 5 6 7 8 9 10 11 12 13 Span class= ' line ' >14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Package sort
//A type, typically A collection, that satisfies sort. Interface can //sorted by the routines The methods require that the //Elements of the collection is enumerated by an integer index. type Interface Interface { //Len is the number of elements in the collection. Len () int //Less reports whether the element with //Index I should sort before the element with index J. Less (i, J int) bool //Swap swaps the elements with indexes I and J. Swap (i, J int) }
...
//Sort sorts data. //It makes one call to data. Len to determine N, and O (N*log (n)) calls to //Data. Less and data. Swap. The sort is isn't guaranteed to be stable. func Sort (data Interface) { //Switch to Heapsort if depth of 2*ceil (LG (N+1)) is reached. N: = data. Len () maxDepth: = 0 For I: = n; i > 0; I >>= 1 { maxdepth++ } maxDepth *= 2 quickSort (data, 0, N, maxDepth) }
|
Some of the sources are omitted, and we see a interface defined in the package that contains three methods: Len (), less (), Swap (). Interface is passed as a parameter to sort. We're going to use sort, just to implement interface's three ways to use the following is an example.
1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - + A at - - - - - in - to + - the * $
|
Package main
Import ( "FMT" "Sort" )
type person struct { Name string Age int }
func (P person) string () string { return to FMT. Sprintf ("%s:%d", P.name, P.age) }
//Byage implements sort. Interface for []person based on //The Age field. type byage []person
func (a byage) len () int { return Len(a)} func (a byage) Swap (i, J int) {a[i], a[j] = A[j], A[i]} func (a byage) less (i, J int) bool { return a[i]. Age < A[j]. AGE}
func Main () { people: = []person{ {"Bob", +}, {"John", " a", {"Michael", +}, {"Jenny", }
FMT. Println (People) sort. Sort (Byage (people)) FMT. Println (People) }
|