Anonymous Functions

Reading material

Go by example: Closures

A Tour of Go: Closures


First exercise

// anonymous functions1
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func main() {

	func(name string) {
		fmt.Printf("Hello %s", name)
	}()

}

Second exercise

// anonymous functions2
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func main() {
	var sayBye func(name string)

	sayBye = func() {
		fmt.Printf("Bye %s", n)
	}
}

Third exercise

// anonymous functions3
// Make me compile!

// I AM NOT DONE
package main

import "fmt"

func updateStatus() func() string {
	var index int
	orderStatus := map[int]string{
		1: "TO DO",
		2: "DOING",
		3: "DONE",
	}

	return func() string {
		index++
		return "What should I return?"
	}
}

func main() {
	anonymous_func := updateStatus()
	var status string

	status = anonymous_func()
	status = anonymous_func()

	if status == "DONE" {
		fmt.Println("Good Job!")
	} else {
		panic("To complete the challenge the status must be DONE")
	}
}


If something is wrong in this page, edit this page on GitHub