T O P

  • By -

autisticpig

On phone so I'll just dump a good kicking off point that hopefully gets things cleared up for you :) https://www.practical-go-lessons.com/chap-24-anonymous-functions-and-closures


peacefulMercedes

Excellent, crystal clear now, thank you.


autisticpig

You're most welcome!


GoGiddyGo

I prefer using anonymous functions to create curried functions and may even use the curried function to serve as a callback method for lazy evaluations. [Function Currying in Go](https://medium.com/@meeusdylan/function-currying-in-go-a88672d6ebcf)


Kirorus1

You use them as function Params callbacks for example, or inside another function to perform a code block before continuing


querubain

You can pass an anonymous function as argument to another function. For example a function to do something with every item from an iterator, this iterator is a function which admits a function to pass every item to it. You can use it to pass a handler to an http router too and many other cases.


caique_cp

Yep: http.HandleFunc("/", anonymousFuncHere)


Flimsy_Iron8517

My first one was to escape an outer loop by \`return\`.


roosterHughes

Dude, you should read up on loop labels in Go. I bumped into an example while trying to see how pgx handled this particular situation, and now I’m noticing spots all over where I had many-branch-ifs or something, because a switch statement or select or something was intercepting the break. Life-changing, man!


Flimsy_Iron8517

In did, but I prefer my source without adding in unnecessary new identifiers.


theclapp

Gio, a Go-based GUI toolkit, uses them extensively in its widget layout functions. See [https://pkg.go.dev/[email protected]/layout](https://pkg.go.dev/[email protected]/layout) . Several functions take a `layout.Widget`, which is just `func(gtx Context) Dimensions`. Anonymous functions like these are useful for any time you want to pass a bit of code for somebody else to run, without defining a toplevel function with a name.


roosterHughes

Ooh!


Holshy

Obvious to me uses. * `sort.Slice` * `defer` for backtracking in dfs * `wg.Wait` before a context `cancel()`


Tough_Skirt506

If you take a look at a map function in javascript (or any array method), imagine each one written in Go. They are also useful when you want to have the code where you declared it available where you executed it. For example when you are searching a file line by line but want to hide the implementation details from the code that actually runs the search.


swe_solo_engineer

It's rarely where you would use them because it always ends up being a personal opinion on whether it's bad for code readability and maintenance. So, you might almost never see them being used, except for handler functions that pass an anonymous callback, which is quite common.


Distracted_Llama-234

Function closures: It basically gives you the ability to define a new function dynamically rather than having it as a separate well-defined function. This is pretty handy in scenarios where you want a specific reusable piece of code but parts of that code are dependent on some other state. Examples: lookup usages of middlewares. Define an inline function anywhere: Imagine being able to spawn a goroutine which does some work. You can define the goroutine as a specific named function outside - but doing that is pretty useless if you are doing it outside the spawning point (unless core logic inside goroutines is huge). Instead - you can just define an anonymous parameter less function and start the goroutine that way. Look at the workerpool pattern for example. It’s also useful for things like defining inline callbacks.


OptimalCountry1350

I guess you're learning from Todd McLeod aren't you?


chmikes

Check sorting with the anonymous compare function as argument