initial commit

This commit is contained in:
specCon18 2023-12-18 03:40:05 -05:00
commit 493ce92e02
62 changed files with 1213 additions and 0 deletions

30
closures/closures.go Normal file
View 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())
}