my net house

WAHEGURU….!

Dive into Closures in Go Programming.

First thing you will find out when you think About Closures, Either in Python or in Golang,
Point is Why would someone will ever use
the Closures insted of Regular Function and What impact will it to on my Code and Running o code,
Well Tat would be a Difiult Question to answer, One thing always matter is your taste of writing code and
how well you want to understand your logic and How well you can UNderstand the Flow-Control of your code
Why you should use Closure Functions?

When the inner function has to remember the state of the Outer functino then uou can use Closure functions, As many times
You will make Sure how good your inner functin is and how much you want to make access of your outer function only then you can
find out how much it is useful to make sure that all the functinos that your are using or gonig to be reuse can be closures as well.

package main

import (
“fmt”
)

func NormalFunc() { // this function has no Return Type and takes no input, It will just print String(“Hello world”)

fmt.Println(“Hello World!!!”)

}

func main() {

NormalFunc()

func(msg string) { // This function has also no Retrn type but will take string as input.
fmt.Println(msg)
}(“hello World”)
}


Now let’s Dive More into Closures!!

package main

import (
“fmt”
)

func NormalFunc() { // this function has no Return Type and takes no input, It will just print String(“Hello world”)

fmt.Println(“Hello World!!!”)

}

func NotNormalFunc() func(string) {
//msg := “Hello World”
return func(msg string) {
fmt.Println(msg)
}
}

func int_count() func() int {
i := 0
return func() int {
i++
//fmt.Println(i)
return i
}
}
func main() {

NormalFunc()
func(msg string) { // This function has also no Retrn type but will take string as input.
fmt.Println(msg)
}(“hello World”)
fmt.Println(“here we are Calling NOt-Normal Function”)
not_normal := NotNormalFunc()
not_normal(“hello-world”) // This part is so Sexy!!!
fmt.Println(“Now we are calling thing which is More Close to Closure.”)
output := int_count()
fmt.Println(output()) // Notice this Function call very Carefully!!
fmt.Println(output())
output2 := int_count()
fmt.Println(output2()) // Notice this Function call very Carefully!!
fmt.Println(output2())

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: