Methodsenhancetypeswith additional behavior.Methodsof thetypeare calledMethod Set.To attach method to a
type:
// Syntax // “varName Type” is called a “receiver” func (varName Type) funcName() { // Code }
// Example // “book” is a struct here func (b book) printBook() { fmt.Println(b.title, b.price) }
- A `receiver` is nothing but method's `input parameters` written `before` a `method name`.
- A `method` belongs to a `single type`.
- `Methods` on `different types` can have the `same names`.
- `Method Expressions` allows us to call `methods` through `types`. For e.g.
```go
// "game" is a struct type
game.print(cod)
game.print(battlefield)
- Behind the scenes, a
methodis afunctionthat takesreceiveras it’sfirst argument.