github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/1-functional-fundamentals/ch01-pure-fp/04_greeting/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  func greeting(name string) {
     9  	// variable in outer function
    10  	msg := name + fmt.Sprintf(" (at %v)", time.Now().String())
    11  
    12  	// foo is a inner function and has access to text variable, is a closure
    13  	// closures have access to variables even after exiting this block
    14  	foo := func() {
    15  		fmt.Printf("Hey %s!\n", msg)
    16  	}
    17  
    18  	// calling the closure
    19  	foo()
    20  }
    21  
    22  func main() {
    23  	greeting("alice")
    24  }