Go language function method
There are both functions and methods in Go
language. One method is a function that contains the recipient, which can be a value or a pointer to a named type or struct type. All methods of a given type belong to the method set of that type. The syntax format 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:
Example
package main
import (
"fmt"
)
/* Define Structure */
type Circle struct {
radius float64
}
func main() {
var c1 Circle
c1.radius = 10.00
fmt.Println("Area of a circle = ", c1.getArea())
}
//This method belongs to a method in a Circle type object
func (c Circle) getArea() float64 {
//c.radius is an attribute in an object of type Circle
return 3.14 * c.radius * c.radius
}
The result of the above code execution is:
Area of a circle = 314