Easy Tutorial
❮ Go Multi Dimensional Arrays Go Operators ❯

Go Language Functions and Methods

Go Functions

Go language has both functions and methods. A method is a function that includes a receiver, which can be a value or a pointer of a named type or a struct type. Methods for a given type belong to that type's method set. The syntax is as follows:

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

Below is an example defining a struct type and a method for that type:

Example

package main

import (
   "fmt"
)

/* Define a struct */
type Circle struct {
  radius float64
}

func main() {
  var c1 Circle
  c1.radius = 10.00
  fmt.Println("Area of the circle = ", c1.getArea())
}

// This method belongs to the Circle type object
func (c Circle) getArea() float64 {
  // c.radius is the attribute of the Circle type object
  return 3.14 * c.radius * c.radius
}

The output of the above code is:

Area of the circle =  314

Go Functions

❮ Go Multi Dimensional Arrays Go Operators ❯