I would like to understand the interface type with a simple example of it’s use in Go (Language).
I read the web documentation, but I don’t get it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The idea behind go interfaces is duck typing. Which simply translates into: If you look like a duck and quack like a duck then you are a duck. Meaning that if your object implements all duck’s features then there should be no problem using it as a duck. Here is an example:
Here a
Humanis aWalkerand aDogis aWalker. Why? Because they both.. well…Walk. They both implement theWalk () stringfunction. So this is why you can executeMakeWalkon them.This is very helpful when you want different types to behave in the same manner. A practical example would be file type objects (sockets, file objects) – you need a Write and a Read function on all of them. Then you can use Write and Read in the same fashion independent of their type – which is cool.