Showing posts with label go functions. Show all posts
Showing posts with label go functions. Show all posts

Saturday, 4 May 2019

Go Language: return Error from a function


Go functions return multiple values. Using this feature, most of the Go functions return error value as the last value returned by the function.

For example, below function calculate sum of two integers a and b and a nil, if the numbers are >= 0, else return 0 and an error message.

func add(a int, b int) (int, error) {
         if a < 0 || b < 0 {
                  return 0, fmt.Errorf("sum pf negative numbers is not supported")
         }

         return a + b, nil
}

‘nil’ is like null in other programming languages like Java.

App.go
package main

import "fmt"

func main() {
 result1, err1 := add(10, 20)

 if err1 != nil {
  fmt.Println(err1)
 } else {
  fmt.Println("Sum of 10 and 20 is : ", result1)
 }

 result2, err2 := add(0, -1)

 if err2 != nil {
  fmt.Println(err2)
 } else {
  fmt.Println("Sum of 0 and -1 is : ", result2)
 }
}

func add(a int, b int) (int, error) {
 if a < 0 || b < 0 {
  return 0, fmt.Errorf("sum pf negative numbers is not supported")
 }

 return a + b, nil
}

Output
Sum of 10 and 20 is :  30
sum pf negative numbers is not supported



Previous                                                 Next                                                 Home

Go Language: Functions continued


When two or more consecutive parameters to a function taking same type, you can omit the type from all except the last.

func add(x, y, z int) int {
         return x + y + z
}

App.go
package main

import (
 "fmt"
)

func main() {
 result := add(10, 20, 30)

 fmt.Println("Sum of 10, 20, 30 is ", result)

}

func add(x, y, z int) int {
 return x + y + z
}

Output
Sum of 10, 20, 30 is  60



Previous                                                 Next                                                 Home

Go language: Return a value from function


Functions in Go can be written zero or more values.

Syntax
func functionName(parameters) returnType{
         statements
        
         return value
}

You can return a value from a function using ‘return’ keyword.

func add(a int, b int) int{
         return a + b
}

HelloWorld.go
package main

import "fmt"

func main() {

 result := add(10, 20)

 fmt.Println("Sum of 10 and 20 is : ", result)

}

func add(a int, b int) int{
 return a + b
}

Output
Sum of 10 and 20 is :  30

Returning multiple values from a function
You can return multiple values from a function­­.

Syntax
func functionName(parameters) (returnType1, … returnType N){
         statements
        
         return value1, … valueN
}

Example
func arithmetic(a int, b int) (int, int, int, int){
         return a + b, a - b, a *b, a / b
}


HelloWorld.go
package main

import "fmt"

func main() {

 addition, subtraction, multiplication, division := arithmetic(10, 20)

 fmt.Println("Sum of 10 and 20 is : ", addition)
 fmt.Println("Subtraction of 10 and 20 is : ", subtraction)
 fmt.Println("Multiplication of 10 and 20 is : ", multiplication)
 fmt.Println("Division of 10 and 20 is : ", division)

}

func arithmetic(a int, b int) (int, int, int, int){
 return a + b, a - b, a *b, a / b
}

Output
Sum of 10 and 20 is :  30
Subtraction of 10 and 20 is :  -10
Multiplication of 10 and 20 is :  200
Division of 10 and 20 is :  0

Named parameters
Just add the names to the return types in function signature.

Example
func arithmetic(a int, b int) (sum int, sub int, mul int, div int){
         sum =  a + b
         sub = a - b
         mul = a *b
         div = a / b

         return
}


HelloWorld.go
package main

import "fmt"

func main() {

 addition, subtraction, multiplication, division := arithmetic(10, 20)

 fmt.Println("Sum of 10 and 20 is : ", addition)
 fmt.Println("Subtraction of 10 and 20 is : ", subtraction)
 fmt.Println("Multiplication of 10 and 20 is : ", multiplication)
 fmt.Println("Division of 10 and 20 is : ", division)

}

func arithmetic(a int, b int) (sum int, sub int, mul int, div int){
 sum =  a + b
 sub = a - b
 mul = a *b
 div = a / b

 return
}

Output
Sum of 10 and 20 is :  30
Subtraction of 10 and 20 is :  -10
Multiplication of 10 and 20 is :  200
Division of 10 and 20 is :  0


A return statement without arguments returns the named return values. This is known as a "naked" return.


Previous                                                 Next                                                 Home