In Detail Closure & Rune In Golang 2019

Check This Previous blog post written by Sagar Jaybhay for better understanding.

Closure

What is Closure in golang?

Closure helps to limit the visibility or scope of the variables. Outerscope enclosed inner scope and what that means?

The package is our outer scope and function we write inside that package is inner scope so when we declared a variable in package level scope it is accessible to all functions which are declared inside that scope(package scope).

But if you declared a variable inside a function and try to access this variable outside of that function or package level scope then it will throw an error undefined variable.

package main

import "fmt"

var y = "sagar jaybhay"

func main() {
    x := 10
    fmt.Println("Program working correctly ", x)

    fmt.Println("i am accessing outer variable inside function := ", y)
}

fmt.Println(x)

see the above example the program will not run and throw an error if we remove last statement print(x) then the program will run.

Closure with scope in golang
Closure with scope in golang

See below code, where we write a function expression or we can say anonymous function which function doesn’t have a name and generally It is a small function or one line, inline function.

package main
package main

var y = "sagar jaybhay"

func main() {

    //x := 10
    //fmt.Println("Program working correctly ", x)

    ///fmt.Println("i am accessing outer variable inside function := ", y)

    variable := func() int {
        return (10 + 20)
    }
    println(variable())

}
// println(variable())  /// not able to call this function outside of scope
//fmt.Println(x)

)

In golang, we are not able to write a function inside a function like other languages C#, Java but you will able to write function expression and evaluate that.

The variable is function type which returns an integer and we call this function expression using the variable name which we assign and print this result.

This function expression is not accessible outside of that inner function main.

Rune

What is Rune in Golang?

It is a superset of ASCII and if you want to know it will contain all the characters in a words writing system. It includes accents and another diacritical mark. In golang it will assign one number for every character and this standard number is called a Unicode Code Point and in golang, it is called a rune. A finally meaning of this is to convert whatever you see a symbol, character into an integer.

If you see ASCII defines 128 characters which are identified by code points 0-127.

Unicode is a superset of ASCII which defines a codespace of 1,114,112 code points.

  1. For rune in go is a single character in single quotes.
  2. It an integer value identifying a Unicode code point.
  3. It also an alias of int32

Print Rune

The following Printf formats work with integer:

  • %c → character as is.
  • %q → rune syntax. e.g. ‘a’.
  • %U → Unicode notation. e.g. U+03B1.
  • %b → base 2
  • %o → base 8
  • %d → base 10
  • %x → base 16, with lower-case letters for a-f

Program for this is

package main

func main() {

    for i:= 50; i < 150; i++ {
        println(i, " --- ", string(i), " --- ", []byte(string(i)))
    }

    // foo := 'a'
    // println(foo)
    // fmt.Printf("%T", foo)
    // println()
    // fmt.Printf("%T", rune(foo))

}

rune in golang
rune in golang

rune in golang -2 .jpg
rune in golang -2 .jpg

rune program
rune program

GitHub Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...