I dont get the whole types + Interfaces model(that replaces classes in other languages). If theres a simple way you could explain what they are about, it will be really appreciated.
Asked
Active
Viewed 344 times
8
-
Can you explain Go Interfaces? Decouple code. See https://stackoverflow.com/a/62297796/12817546. Call a method “dynamically”. See https://stackoverflow.com/a/62336440/12817546. Access a Go package. See https://stackoverflow.com/a/62278078/12817546. Assign any value to a variable. See https://stackoverflow.com/a/62337836/12817546. – Jul 10 '20 at 09:54
2 Answers
5
This is a pretty good overview:
http://research.swtch.com/2009/12/go-data-structures-interfaces.html

Chris Cherry
- 28,118
- 6
- 68
- 71
4
Go interfaces are statically checked duck typing.
The difference between pure virtual classes in C++ or interfaces in java is that you don't declare the interface on the class that implements the interface, but on the method that receives the interface.
For example, I can create an interface with a Read
and a Write
method and call it ThingsDustinReadsAndWrites
and have a function called doReadsAndWrites(rr ThingsDustinReadsAndWrites
. That can, in turn, receive a built-in http.ClientConn
which has never heard of my interface, but implements it because it happens to have those methods.

Dustin
- 89,080
- 21
- 111
- 133