Methods attach functions to a type.

Methods

methods_intro.go
package main

import "fmt"

type Rectangle struct {
	Width  int
	Height int
}

func (r Rectangle) Area() int {
	return r.Width * r.Height
}

func main() {
	var width = 
	shape := Rectangle{Width: width, Height: 3}
	area := shape.Area()

	fmt.Println("width=", shape.Width)
	fmt.Println("height=", shape.Height)
	fmt.Println("area=", area)
}
package main

import "fmt"

type Rectangle struct {
	Width  int
	Height int
}

func (r Rectangle) Area() int {
	return r.Width * r.Height
}

func main() {
	var width = 
	shape := Rectangle{Width: width, Height: 3}
	area := shape.Area()

	fmt.Println("width=", shape.Width)
	fmt.Println("height=", shape.Height)
	fmt.Println("area=", area)
}
package main

import "fmt"

type Rectangle struct {
	Width  int
	Height int
}

func (r Rectangle) Area() int {
	return r.Width * r.Height
}

func main() {
	var width = 
	shape := Rectangle{Width: width, Height: 3}
	area := shape.Area()

	fmt.Println("width=", shape.Width)
	fmt.Println("height=", shape.Height)
	fmt.Println("area=", area)
}
method receiver A method receiver appears before the method name and tells Go which type the method belongs to.