Created
July 2, 2018 09:50
-
-
Save Nydan/c5a9e258abd94cd3113f0fd726e4d398 to your computer and use it in GitHub Desktop.
syntax for calling func that receive func as parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func trace(requestName func() string) func(string) string { | |
return func(text string) string { | |
request := requestName() | |
return fmt.Sprintf("operation: %s, request: %s", request, text) | |
} | |
} | |
func main() { | |
// first way to call the func trace() | |
// it call func trace() to get s as a func() and then add parameter to s() in order to get the result | |
s := trace(func() string { | |
return "first way" | |
}) | |
result := s("some param here") | |
fmt.Println(result) | |
// second way, it fullfil func trace() paramter followed by parameter needed in the return value (func) from func | |
// trace. This might be usefull for some middleware such as http.Handler or else for trace or log something | |
q := trace(func() string { | |
return "second way" | |
})("here is the paramter goes") | |
fmt.Println(q) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment