Golang methods are cool.

Golang is a mostly procedural language, however it adopts certain OOP-like features (as well as FP-like features). Golang Methods are a perfect example.

Take these two functions

type Post struct { ... }
 
func UpdatePost(post *Post) { // Function A
  post.title = "new Title"
}
 
func (post *Post) UpdatePost() { // Function B
  post.title = "new Title"
}

Function A is a function and is called with UpdatePost(post), meanwhile Function B is a method and is called with post.UpdatePost(). Its important to note, that Function B is just syntax sugar. When compiled, both output the same binary.

This is pretty magical IMO. Especially when comparing to javascript, where to achieve the same thing, you’d need to either have a helper inside the post instance or post be a class. Now image you want an array of Posts, in js you’d have to create a new instance of the Post class/object. Meanwhile, in go, no need!

Btw, the post in func (post *Post) UpdatePost(){} is called a receiver argument.