Method with struct and interface

  • Methods enhance types with additional behavior.

  • Methods of the type are called Method 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 method is a function that takes receiver as it’s first argument.

See also