Go - Method



Go programming language supports special types of functions called methods. In method declaration syntax, a "receiver" is present to represent the container of the function. This receiver can be used to call a function using "." operator. For example −

Syntax of a Method

func (variable_name variable_data_type) function_name() [return_type]{
   /* function body*/
}

Example

package main

import (
   "fmt" 
   "math" 
)

/* define a circle */
type Circle struct {
   x,y,radius float64
}

/* define a method for circle */
func(circle Circle) area() float64 {
   return math.Pi * circle.radius * circle.radius
}

func main(){
   circle := Circle{x:0, y:0, radius:5}
   fmt.Printf("Circle area: %f", circle.area())
}

When the above code is compiled and executed, it produces the following result −

Circle area: 78.539816

Methods with Struct Type Receiver

You can create a method with a struct type receiver that operates on a copy of the struct, so whatever changes will be done in the method, they do not affect the original struct.

Example

package main
import "fmt"

// Struct
type Rectangle struct {
    width, height float64
}

// Define a method  with struct type receiver
func (rect Rectangle) Area() float64 {
    return rect.width * rect.height
}

func main() {
    rectObj := Rectangle{width: 2.4, height: 4.5}
    fmt.Println("Area of Rectangle:", rectObj.Area())
}

When the above code is compiled and executed, it produces the following result −

Area of Rectangle: 10.799999999999999

Methods with Non-Struct Type Receiver

You can also create a method withnon-struct type receivers of such type whose definition is present in the same package.

Example

package main
import "fmt"

// Creating a custom type based on int
type value int

// Defining a method with a non-struct receiver
func (v value) cube() value {
    return v * v * v
}

func main() {
    x := value(3)
    y := x.cube()

    fmt.Println("Cube of", x, "is", y) 
}
Cube of 3 is 27

Methods with Pointer Receiver

You can create a method that can havepointer receivers. This approach reflects the changes done in the method in the caller.

Example

package main
import "fmt"

type student struct {
    grade string
}

// Method with pointer receiver to modify data
func (s *student) updateGrade(newGrade string) {
    s.grade = newGrade
}

func main() {
    s := student{grade: "B"}
    
    fmt.Println("Before:", s.grade)
    
    // Calling the method to update the grade
    s.updateGrade("A")
    
    fmt.Println("After:", s.grade)
}
Before: B
After: A
Advertisements