WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go language function method


There are functions and methods in the Go language. One method is a function that contains the recipient. The recipient can be a value of a named type or a struct type or a pointer. All methods of a given type belong to a set of methods of that type. The syntax is as follows:

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

The following defines a structure type and a method for that type:

package main

Import (
    "fmt"
)

/* Define structure */
Type Circle struct {
   Radius float64
}

Func main() {
   Var c1 Circle
   C1.radius = 10.00
   fmt.Println("area of the circle = ", c1.getArea())
}

//The method belongs to a method in a Circle type object
Func (c Circle) getArea() float64 {
   //c.radius is an attribute in a Circle type object
   Return 3.14 * c.radius * c.radius
}

The above code execution results are:

 area = 314