I think it’s worth trying to write just another post on Golang interface, in attempt to serve as a quick introduction to interface and reflection of Golang in simple English.
According to Effective Go:
Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here..
This is pretty much half the story of the interface, and normal every day use of it.
Let’s say you have a
generic interface, which has one method, called
Then you write a struct which implements the generic interface.
Then you could just use instances of specification anywhere as if you are using generic.
This is pretty much the ducking type part of Golang, and personally I think this is similar to
abstract in Java.
Notice that the “implementation” is implicit, not explicit. But the compiler still checks for the “type safety”, and will warn you if something goes wrong.
And another part of the story is how an interface value is stored.
In Go, the interface has two parts, the interface value and the “actual“ value. With this two parts understanding, a lot of common pitfalls could be avoided.
And the Golang does packing and unpacking for you automatically when necessary.
For example, the following codes demonstrate it.
With this understanding, we could explore reflection in Golang now.
According to the official Golang blog post Laws of Reflection,
At the basic level, reflection is just a mechanism to examine the type and value pair stored inside an interface variable.
And that is it.
I believe this is pretty much everything one needs to know at an beginning level.
After this, you may also want to read the following posts.
- Laws of Reflection
- Go Data Structures: Interfaces
- Learning Go - Interfaces & Reflections
- How to use interfaces in Go
- Structs and Interfaces
Till next time.