initial commit
This commit is contained in:
commit
493ce92e02
62 changed files with 1213 additions and 0 deletions
BIN
closures/closures
Executable file
BIN
closures/closures
Executable file
Binary file not shown.
30
closures/closures.go
Normal file
30
closures/closures.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// This function returns another function,
|
||||
// which we define anonymously in the body of the intSeq.
|
||||
// The returned function closes over the variable i to form a closure
|
||||
func intSeq() func() int {
|
||||
i := 0
|
||||
return func() int {
|
||||
i++
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
func main(){
|
||||
//we call intSeq assigning the result(a function) to nextInt.
|
||||
// This function value captures its own i value, which will be updated
|
||||
// each time we call nextInt
|
||||
nextInt := intSeq()
|
||||
|
||||
// see the effect of the closure by calling nextInt a few times.
|
||||
fmt.Println(nextInt())
|
||||
fmt.Println(nextInt())
|
||||
fmt.Println(nextInt())
|
||||
|
||||
// to confirm that the state is unique to the instance create a new instance
|
||||
newInts := intSeq()
|
||||
fmt.Println(newInts())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue