Deep Dive Into the Go Type System

Deep Dive Into the Go Type System

Go has a very interesting type system. It eschews classes and inheritance in favor of interfaces and composition, but on the other hand it doesn't have templates or generics. The way it handles collections is also unique. 

[…]

Interfaces are the cornerstone of the Go type system. An interface is just a collection of method signatures. Every type that implements all the methods is compatible with the interface. Here is a quick example. The Shape interface defines two methods: GetPerimeter() and GetArea(). The Square object implements the interface.

[…]

For a whole article on Go interfaces, check out: How To Define and Implement a Go Interface.

[…]

Type assertions let you convert an interface to its concrete type. If you already know the underlying type, you can just assert it. If you're not sure, you can try several type assertions until you discover the right type

[…]

The Go reflect package lets you directly check the type of an interface without type assertions. You can also extract the value of an interface and convert it to an interface if you wish (not as useful). 

[…]