<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="default.xsl"?>
<fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="true"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>398</fr:anchor><fr:addr
type="user">go-00sq</fr:addr><fr:route>go-00sq.xml</fr:route><fr:title
text="Golang › Closures › Why use closures?"><fr:link
type="local"
href="go-00sp.xml"
addr="go-00sp"
title="Golang › Closures"><fr:link
type="local"
href="go-00rs.xml"
addr="go-00rs"
title="Golang">Golang</fr:link> › Closures</fr:link> › Why use closures?</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:ul><fr:li>Limit function scope</fr:li>
<fr:p>If a function is going to be called from only one other function, but it's called multiple times, you can use an inner function to "hide" the called function. This reduces the number of declarations at a package level, making it easier to find an unused name.</fr:p>
<fr:li>Removing logic repetetion</fr:li>
<fr:p>Given a piece of logic that is repeated multiple times within a function, a closure can be used to remove that repetition.</fr:p>
<fr:li>Passing to funcs/returning from funcs</fr:li>
<fr:p>These become really interesting, allowing you to take variables within your function and use those variables <fr:em>outside</fr:em> of the function</fr:p></fr:ul></fr:mainmatter><fr:backmatter><fr:tree
toc="false"
numbered="false"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:title
text="Context">Context</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:tree
toc="true"
numbered="false"
show-heading="true"
show-metadata="true"
expanded="false"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>399</fr:anchor><fr:addr
type="user">go-00sp</fr:addr><fr:route>go-00sp.xml</fr:route><fr:title
text="Golang › Closures"><fr:link
type="local"
href="go-00rs.xml"
addr="go-00rs"
title="Golang">Golang</fr:link> › Closures</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:p>These are functions defined _within_ a function, and are able to access and modify variables declared in the outer function. Closures allow you to pass fome function state to another function. These can also be returned from a function.</fr:p><fr:pre><![CDATA[package main

import "fmt"

func main() {
    a := 20
    f := func() {
        fmt.Println(a)
        a = 30
    }
    f()
    fmt.Println(a)
}]]></fr:pre><fr:p>Anonymous function `f` can read and write `a` even though `a` is not passed in to the function. Using `:=` instead of `=` inside the closure creates a new `a` that ceases to exist when the closure exits.</fr:p><fr:tree
toc="true"
numbered="true"
show-heading="true"
show-metadata="false"
expanded="true"
root="false"
xmlns:fr="http://www.jonmsterling.com/jms-005P.xml"><fr:frontmatter><fr:anchor>288</fr:anchor><fr:addr
type="user">go-00sq</fr:addr><fr:route>go-00sq.xml</fr:route><fr:title
text="Why use closures?">Why use closures?</fr:title><fr:authors /></fr:frontmatter><fr:mainmatter><fr:ul><fr:li>Limit function scope</fr:li>
<fr:p>If a function is going to be called from only one other function, but it's called multiple times, you can use an inner function to "hide" the called function. This reduces the number of declarations at a package level, making it easier to find an unused name.</fr:p>
<fr:li>Removing logic repetetion</fr:li>
<fr:p>Given a piece of logic that is repeated multiple times within a function, a closure can be used to remove that repetition.</fr:p>
<fr:li>Passing to funcs/returning from funcs</fr:li>
<fr:p>These become really interesting, allowing you to take variables within your function and use those variables <fr:em>outside</fr:em> of the function</fr:p></fr:ul></fr:mainmatter><fr:backmatter /></fr:tree></fr:mainmatter><fr:backmatter /></fr:tree></fr:mainmatter><fr:backmatter /></fr:tree></fr:backmatter></fr:tree>