From the course: Go for Developers: Practical Techniques for Effective Coding
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
The empty interface - Go Tutorial
From the course: Go for Developers: Practical Techniques for Effective Coding
The empty interface
- All the interfaces we've seen so far have declared one or more methods. In GO, there is no minimum method count for an interface. This means that it is possible to have what is called an empty interface. If you declare an interface with zero methods, then every type in the system will be considered to have implemented it. In GO, we use the empty interface to represent anything. In GO 118, generics were added to the language. As part of this, a new keyword "any" was added to the language. This keyword is an alias for the empty interface. So, for example, here the variables I and A are both identical types. Using the "any" keyword is now considered idiomatic GO and the use of the empty interface discouraged. It is considered bad practice in GO to overuse the empty interface. You should always try to accept either a concrete or a non-empty interface. While there are valid reasons for using an empty interface, the downside should be considered. First, there is no type information…