How to Define Methods on Types in Go

Define Go methods by adding a receiver argument in parentheses before the function name to attach behavior to a type.

You define methods on types in Go by declaring a function with a receiver argument that matches the type name. The receiver appears in parentheses between the func keyword and the method name.

type MyType struct {
    Value int
}

func (m *MyType) Double() int {
    return m.Value * 2
}

Use a pointer receiver (*MyType) if the method needs to modify the struct's fields; use a value receiver (MyType) if it only reads data.