Go Language: Methods and Interfaces
A few weeks ago I started to learn Go and I wrote an introductory post. I’m going to continue where I left and explain how you can extend structures with methods and later how to use interfaces as arguments.
Methods
Go doesn’t have classes or objects as we know them. It uses structs instead to create object-like structures:
1
2
3
4
type Animal struct {
color string
size float64
}
This looks very similar to an object but something very important is missing. You can declare properties like this, but not methods. How will our animal do stuff without methods?. Golang actually does have methods, but you have to attach them to the struct after it is created: