Saturday 4 September 2021

What is Function in Go Language?

 In Go, functions are the basic building blocks. A function is used to break a large problem into smaller tasks. We can invoke a function several times, hence functions promote code reusability. There are 3 types of functions in Go:

  • Normal functions with an identifier
  • Anonymous or lambda functions
  • Method (A function with a receiver)

Function parameters, return values, together with types, is called function signature.

Function cannot be declared inside another function. If we want to achieve this, we can do this by anonymous function.

Create a Function

To create (often referred to as declare) a function, do the following:

  • Use the func keyword.
  • Specify a name for the function, followed by parentheses ().
  • Finally, add code that defines what the function should do, inside curly braces {}.

Syntax

func FunctionName() {
  // code to be executed
}

Go Function Example

  1. package main  
  2. import "fmt"  
  3. type Employee struct {  
  4.    fname string  
  5.    lname string  
  6. }  
  7. func (emp Employee) fullname(){  
  8.    fmt.Println(emp.fname+" "+emp.lname)  
  9. }  
  10. func main() {  
  11.    e1 := Employee{ "Santosh Kumar","Singh"}  
  12.    e1.fullname()  
  13. }  

Output:

Santosh Kumar Singh

Go Function with Return

Let's see an example of function with return value.

  1. package main  
  2. import (  
  3.    "fmt"  
  4. )  
  5. func fun() int {  
  6.    return 3033 
  7. }  
  8. func main() {  
  9.    x := fun()  
  10.    fmt.Println(x)  
  11. }  

Output:

3033

Go Function with Multiple Return

Let's see an example of a function which takes n number of type int as argument and returns two int values. The return values are filled in the calling function in a parallel assignment.

Go function multiple return example

  1. package main  
  2. import (  
  3.    "fmt"  
  4. )  
  5. func main() {  
  6.    fmt.Println(addAll(10,15,20,25,30))  
  7. }  
  8. func addAll(args ... int)(int,int)  {  
  9.    finalAddValue:=0  
  10.    finalSubValue:=0  
  11.    for _,value  := range args{  
  12.       finalAddValue += value  
  13.       finalSubValue -= value  
  14.    }  
  15.    return finalAddValue,finalSubValue  
  16. }  

Output:

100 -100

Naming Rules for Go Functions

  • A function name must start with a letter
  • A function name can only contain alpha-numeric characters and underscores (A-z0-9, and _ )
  • Function names are case-sensitive
  • A function name cannot contain spaces
  • If the function name consists of multiple words, techniques introduced for multi-word variable naming can be used

Tip: Give the function a name that reflects what the function does!

Call a Function

Functions are not executed immediately. They are "saved for later use", and will be executed when they are called.

In the example below, we create a function named "myMessage()". The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs "I just got executed!". To call the function, just write its name followed by two parentheses ():

Example

package main
import ("fmt")

func myMessage() {
  fmt.Println("I just got executed!")
}

func main() {
  myMessage() // call the function
}

Result:

I just got executed!


No comments:

Post a Comment