How is the empty interface different than a generic?


Maybe I’m not fully versed on the power of generics, but how is the empty interface, interface{}, different than a generic, especially if we have the ability to use reflection or type switches? People always mention that Go doesn’t have generics, but interface{} seems like it does the job pretty comparable to something like <T> in Java.


If you come from Java, the empty interface (interface{}) is actually closer to working with Object variables in Java than with generics.

You can assign anything to an interface{} (like you can do with an Object variable in Java).

But you should then "cast" or "type assert" back if you want to use the actual type you stored there (same that you need to do with Object variables in Java).

Generics in Java are quite different, since they allow you to keep type checking at compile time. The difference is precisely that you don’t need to resort to reflection or type switches if you work with Generics.

You can read more about Java generics here:

And then follow this and the next 2 or 3 steps of the Go tour here for more on how the empty interface works:

Answered By – eugenioy

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.