mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
fix(go func factory): fix func factory example
previous code did not run because outside main() code was done
This commit is contained in:
parent
f90cdc44bb
commit
74c0a49e6d
@ -192,16 +192,26 @@ func learnFlowControl() {
|
|||||||
goto love
|
goto love
|
||||||
love:
|
love:
|
||||||
|
|
||||||
|
learnFunctionFactory() // func returning func is fun(3)(3)
|
||||||
learnDefer() // A quick detour to an important keyword.
|
learnDefer() // A quick detour to an important keyword.
|
||||||
learnInterfaces() // Good stuff coming up!
|
learnInterfaces() // Good stuff coming up!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func learnFunctionFactory() {
|
||||||
|
// Next two are equivalent, with second being more practical
|
||||||
|
fmt.Println(sentenceFactory("summer")("A beautiful", "day!"))
|
||||||
|
|
||||||
|
d := sentenceFactory("summer")
|
||||||
|
fmt.Println(d("A beautiful", "day!"))
|
||||||
|
fmt.Println(d("A lazy", "afternoon!"))
|
||||||
|
}
|
||||||
|
|
||||||
// Decorators are common in other languages. Same can be done in Go
|
// Decorators are common in other languages. Same can be done in Go
|
||||||
// with function literals that accept arguments.
|
// with function literals that accept arguments.
|
||||||
func learnFunctionFactory(mystring string) func(before, after string) string {
|
func sentenceFactory(mystring string) func(before, after string) string {
|
||||||
return func(before, after string) string {
|
return func(before, after string) string {
|
||||||
return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
|
return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next two are equivalent, with second being more practical
|
// Next two are equivalent, with second being more practical
|
||||||
|
Loading…
Reference in New Issue
Block a user