Categories
Uncategorized

Go: Creating new types based on built-in types

Ever needed a custom string type to add your own methods or tweak certain behavior? In languages like Ruby or Javascript, you can directly modify built in types. In other languages, we usually create custom objects to wrap around a default type and then extend that object with necessary methods or attributes.

A sample example in Python would look like:

In Go, we would define a new type based on an existing type. Then define methods on the newly defined type. It allows us to easily create new types based on existing types and then customize to suit our needs. The same example in Go would look like:

Simple, isn’t it?