Why do I Need interface

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Wrote a one-month plug-in, found interface this keyword almost no use, and then curiosity, opened the Gjson source code, found that Gjson source with a few, the most usage is equivalent to the object in Java. I began to wonder why it was necessary to interface, not an object (written here, I remembered the fear that was dominated by the C language void*).

It's a bit confusing to have a personal sense of interface in translation into an interface. As handler translated into a handle, seemingly at that time was wrong, Hook was seen wrong into a sentence, handler, hook handle, is the door knob, socket is the meaning of the socket.

The most basic impression of interface (interface) is the FMT. Println This method, at that time feel very magical, really want to implement the string method, it can be printed out. Now that the program is written more, it is found that "interface is contract".
I see IO in the implementation of Reader/writer, are used interface, I also try to write their own reader/writer, and then as a variable to pass. Perhaps my recent work has been carried out in a package, without passing, let alone the exchange of structs.

The more authoritative summary is as follows:

An interface is a kind of contract which the implementing type (s) must fulfill. Interfaces describe the behaviorof types, what's they can do. They completely separate the definition of what a object can do from how it does it, allowing distinct implementations to Be represented at different times by the same interface variable, which are what polymorphism essentially is.
Writing functions So, they accept an interface variable as a parameter makes them more general.
The ' Go '

The wild translations are as follows:

An interface is a constraint that must be fully implemented (the declared function). The interface defines the behavior of the type: "What can they do". Unlike the definition of how an object is done, he allows the same interface variable to represent different implementations at different times, which is the nature of go polymorphism. When writing a method, let them receive the interface type, just as usual to pass a parameter.

Summary interface

    • Handling Unknown data types
    • Golang generics

Writing so long Golang, I think, before java/c++ that set, constructor, overload, is not really so necessary. But I miss Java's generics and Gradle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.