Go logo

The Function in Go Language

In Go, functions are the fundamental building blocks. A function is used to break a large problem into smaller jobs. We can invoke a function many times, hence functions are used as code reusability.

A function is a set of statements that collectively perform a job. Each Go program has at least one function, which is main(). You may split your code into separate functions.

 Three types of functions in Go Programming are:

  • Normal functions with an identifier
  • Method (A function with a receiver)
  • Anonymous or lambda functions
A few points about the Go functions:
  • Function parameters, return values, together with types, is known as function signature.
  • Function can’t be declared inside another function.
  • If we would like to declare a function inside another function, then do it with an anonymous function.
  • The best way to split your code among different functions is your choice, but logically, the target should be such that each function performs a particular task.
  • A function declaration tells the compiler about a function name, return type, and parameters.
  • A function definition provides the real body of the function.
An example of a function

The following example shows how to create a simple function in Go Lang:

package main 

import "fmt" 

type Student struct { 

   firstName string 

   lastName string 

} 

func (stu Student) fullname(){ 

   fmt.Println(stu.firstName+" "+stu.lastName) 

} 

func main() { 

   e1 := Student{ "Mike","Haynes"} 

   e1.fullname() 

}

The output:

Mike Haynes

Note: The Go standard library offers numerous built-in functions your program may call. For instance, the function len() takes arguments of different types (like an array) and returns the length of this type.

Further Reading:  The Decision-making statements in Go – If statement

If a string is passed to it, then the function returns the length of the string in bytes. When an array is passed to it, the function returns the length of the array.

From the above example, we may learn a few things:

A function definition in Go programming language is made up of function header and a body. Here are the elements of a function −

  • Func − It begins the declaration of a function.
  • Function Name − It’s the real name of this function. The function name and the parameter list collectively constitute the function signature.
  • Parameters − A parameter is just like a placeholder. When a function is invoked, you pass a value to the parameter. This value is known as the real parameter or argument. The parameter list denotes the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Return Type − A function may return a list of values. The return_types is the list of data types of the values that the function returns. Some functions perform the desired operations without returning a value. In cases like this, the return_type is not mandatory (shown in the example below).
  • Function Body − It includes a collection of statements that specify what the function does.
Further Reading:  What is a Range in Go Language?
How Calling a Function Works

Whilst making a Go function, you provide a definition of what the function must do. To use a function, you’ll need to call that function to do the defined task.

When a program calls a function, the program control is transferred to the called function. A called function performs a specified task and if a return statement is given and it is executed or if its function-ending final recourse is reached, it returns the program control back to the main program.

To call a function, you just must pass the required parameters together with its function name. If the function returns a value, then it’s possible to save the returned value.

An example of Return Value in the Go function

In this example, we will use the return statement to return a value:

package main 

import ( 

   "fmt" 

) 

func retfunc() int { 

   var x,y,z int

   x = 10

   y = 20

   z = x * y

   return z 

} 

func main() { 

   retval := retfunc() 

   fmt.Println(retval) 

}

Output:

200

Returning multiple values example

This example shows returning multiple values by the function:

package main




import "fmt"




func retMulti(x, y int) (int, int) {

   return y, x

}

func main() {

   a, b := retMulti(10, 100)

   fmt.Println(a, b)

}

 

Further Reading:  An introduction to Maps in Go (GoLang)

Output:

100 10