mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 2: Functions as Parameters #4

Open mchirico opened 3 years ago

mchirico commented 3 years ago

Functions as Parameters

In Go, you can pass functions as parameters to other functions. Here's an example...

Video Day 2 Repil.it

package main

import (
    "fmt"
)

type someFunction func(int) (string, error)

func Thingy(s someFunction) (string, error) {
    return s(4)
}

func main() {
    result, err := Thingy(func(x int) (string, error) { return fmt.Sprintf("Number: %d", x), nil })
    fmt.Println(result, err)
}
tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!