# Good places to use anonymous functions that aren't just for concurrency

**URL:** <https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318>\
**Category:** Technical Discussion\
**Created:** [October 28, 2022, 2:54pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318 "2022-10-28T14:54:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashinnv](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/ashinnv/32/10887_2.png) [@ashinnv](https://forum.golangbridge.org/u/ashinnv)\
**Post date:** [October 28, 2022, 2:54pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/1 "2022-10-28T14:54:46Z")

</div>

It just occurred to me that I pretty much exclusively use anonymous functions as an easy way to write concurrent and parallel code, with the very, very occasional use of returning a function from another function. Where else is it appropriate to use an anonymous function in go?

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [October 28, 2022, 4:02pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/2 "2022-10-28T16:02:05Z")

</div>

Hi @ashinnv,

An anonymous function lives in the context of the function that it is defined in. (This is why anonymous functions are also called “closure”.)

A closure is bound to its outer scope. That is, a closure can access variables defined in the function that the closure is defined in. This way, you can create functions that carry some sort of state with them, for example, a counter, as shown in [Go by Example: Closures](https://gobyexample.com/closures).

And Jon Calhoun describes [5 Useful Ways to Use Closures in Go](https://www.calhoun.io/5-useful-ways-to-use-closures-in-go/).

---

<div class="post-metadata">

**Author:** ![mje](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/mje/32/10284_2.png) [@mje](https://forum.golangbridge.org/u/mje)\
**Post date:** [October 28, 2022, 4:54pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/3 "2022-10-28T16:54:20Z")

</div>

I most frequently use anonymous functions for `less` in `sort.Slice`. [sort package - sort - Go Packages](https://pkg.go.dev/sort#example-Slice)

---

<div class="post-metadata">

**Author:** ![ashinnv](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/ashinnv/32/10887_2.png) [@ashinnv](https://forum.golangbridge.org/u/ashinnv)\
**Post date:** [October 29, 2022, 9:07pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/4 "2022-10-29T21:07:02Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [October 30, 2022, 3:01pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/5 "2022-10-30T15:01:25Z")

</div>

I also use them as equivalents to [C#'s Local Functions](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions). One example I can think of is if I need to build up some metadata for types, such as:

```go
type typeData struct {
    // some data that I want for each type, maybe a mapping of
    // the type's fields to functions that get or set the values, etc.
}

var typeDataByReflectType = sync.Map{}

func getTypeData(rt reflect.Type) *typeData {
    // newTypeData is "local" to getTypeData because you shouldn't be able
    // to call it outside of getTypeData, even from other functions in
    // the same package.
    newTypeData := func(rt reflect.Type) (td *typeData) {
        // do whatever is needed to create one of these *typeData structs
        return
    }
    v, loaded := typeDataByReflectType.Load(rt)
    if loaded {
        return v.(*typeData)
    }
    td := newTypeData(rt)
    if v, loaded = typeDataByReflectType.LoadOrStore(rt, td); loaded {
        return v.(*typeData)
    }
    return td
}

```

---

<div class="post-metadata">

**Author:** ![Przemo](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/przemo/32/11718_2.png) [@Przemo](https://forum.golangbridge.org/u/Przemo)\
**Post date:** [November 1, 2022, 6:12pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/6 "2022-11-01T18:12:25Z")

</div>

Maybe as a callback:

```auto
package main

import (
	"fmt"
	"time"
)

type callback func()

func main() {
	doSth(func() {
		fmt.Print("Jobs done")
	})
}

func doSth(c callback) {
	time.Sleep(time.Second * 2) // to simulate some tasks
	c()
}

```

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [January 30, 2023, 6:12pm UTC](https://forum.golangbridge.org/t/good-places-to-use-anonymous-functions-that-arent-just-for-concurrency/29318/7 "2023-01-30T18:12:51Z")

</div>

Closed on January 30, 2023, 6:12pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
