Functions and Methods
Latest update: November 29, 2022

Functions and Methods #

⚠️ Opinionated stuff below

Go supports both plain package-level functions, and methods one can attach to any type:

package foo

// A plain package-level function
func sayHello() {
	fmt.Println("Hello, World!")
}

// A type with a method
type Dog struct {}

func (d *Dog) Bark() {
	fmt.Println("bark")
}

In my opinion, most developers with OOP experience prematurely jump into turning everything into a type, so that they can attach methods to it. My advice is to always start with package-level functions until you reach a point, in which turning it into a method would prove to be a better option.