What is the use of golang's interface?

I can accomplish all kinds of business requirements without this thing. What"s the use of this?

Mar.02,2021

this is not the problem of golang, this is why programming for interfaces
it is recommended to take a look at the design pattern


golang interface {} one can represent the interface in the design pattern, and the other can be seen as a void* similar to C #.


strongly typed languages are added to maintain the advantages of dynamic languages. Similar to Object in java, it is very useful. The object-oriented design of golang is based on interface {}. If you don't use it, you should be a programming


master to use this to implement type OOP development. Interface-oriented programming.


does not mean that if you can complete all kinds of business, the features of the language you do not use are useless. Instead, you should reflect on why you do not use this important feature.
take the most simple and commonly used sorting, for example, without interface {}, you have to write similar code n times for different types of parameters, similar to the idea of the CPP template.


needs to be clear about the difference between interface and interface {}. One is the syntax mechanism of go for data abstraction and decoupling, and the other is the concrete implementation of this mechanism. The former should be encouraged, while the latter should be used with caution.
large projects or infrastructure projects are often built without interface to implement data abstractions and interfaces. Of course, it also depends on the specific requirements, go is not a strong paradigm, go/channel/interface/reflect these features do not say certain use, as long as you think the code architecture can perfectly match the requirements. Of course, it doesn't mean that what you don't use is useless. I suggest you take a look at different types of open source projects and experience it.
with regard to interface {}, you should be careful at all times. There is a risk of abuse and poor maintainability. But in line with go's pragmatic philosophy, it can be used if it can perfectly match the needs.

Menu