Function in Go Language

  • date 5th September, 2022 |
  • by Prwatech |
  • 0 Comments
  1. Call a Function
    package main
    import (
    "fmt"
    
    )
    func myMessage() {
      fmt.Println("This is Prwatech Institute of Data Scientist  
    }
    
    func main() {
      myMessage() // call the function
    }
    

    Output :

    PS C:\GO_Language\Function> go run func1.go
     
    This is Prwatech Institute of Data Scientist
    
    
  2. Function with Parameter
    package main
    
    import (
        "fmt"
    )
    
    func familyName(fname string) {
        fmt.Println("Hello", fname, "Prwatech")
    }
    
    func main() {
        familyName("Guru")
        familyName("Sam")
        familyName("Anuj")
    }
    

    Output :

    PS C:\GO_Language\Function> go run func2.go
    Hello Guru Prwatech
    Hello Sam Prwatech
    Hello Anuj Prwatech
    

     

  3. print the Fibonacci sequence using FUNCTION
    package main
    
    import (
        "fmt"
    )
    
    func fib1(i int) int {
    
        if i == 0 {
            return 1
        }
        if i == 1 {
            return 1
        }
        return fib1(i-1) + fib1(i-2)
    }
    func main() {
        var i int
        for i = 0; i < 10; i++ {
            fmt.Printf("%d \n", fib1(i))
    
        }
    }
    

    Output :

    PS C:\GO_Language\Function> go run func3.go
    1 
    1 
    2 
    3 
    5 
    8 
    13 
    21 
    34 
    55
    

     

    • Golang Passing Address to a Function

    It is passing the address of a variable to the function and modifying it using dereferencing inside the body of the function is the value of the variable.

    package main
    
    import "fmt"
    
    func update(a *int, t *string) {
        *a = *a + 5      		   // pointer address
        *t = *t + " sharma" 		   // pointer address
        return
    }
    
    func main() {
        var age = 20
        var text = "sandeep"
        fmt.Println("Before:", text, age)
    
        update(&age, &text)
    
        fmt.Println("After :", text, age)
    }
    

    Output :

    PS C:\GO_Language\Function> go run func4.go
    Before: Sandeep 20
    After: Sandeep Sharma 25
    

     

    • Anonymous Functions in Golang

     A function that was declared to be referenced without a named identifier. Anonymous functions can accept input and return output, just as standard functions do.

    package main
    
    import "fmt"
    
    var (
        area = func(l int, b int) int {
            return l * b
        }
    )
    
    func main() {
        fmt.Println(area(20, 30))
    }
    

    Output :

    PS C:\GO_Language\Function> go run func6.go
    600
    

     

    • Closures Functions in Golang

    There are anonymous functions that access variables defined outside the body of the function.

    package main
    
    import "fmt"
    
    func main() {
        l := 50
        b := 70
    
        func() {
            var area int
            area = l * b
            fmt.Println(area)
        }()
    }
    

    Output :

    PS C:\GO_Language\Function> go run func5.go
    3500
    

     

    • Higher Order Functions in Golang

     A Higher-Order function is a function that receives a function as an argument or returns the function as output.

    Higher order functions are functions that operate on other functions, either by taking them as arguments or by returning them.

    package main
    
    import "fmt"
    
    func sum(x, y int) int {
        return x + y
    }
    func Sum(x int) func(int) int {
        return func(y int) int {
            return sum(x, y)
        }
    }
    func main() {
        partial := Sum(30)
        fmt.Println(partial(50))
    }
    

    Output :

    PS C:\GO_Language\Function> go run func7.go
    80
    

     

Quick Support

image image