I'm not sure your `move` function actually does anything in this case. AIUI the compiler and runtime are already clever enough to recognize that c is no longer used after the `go` statement (hence the necessity of runtime.KeepAlive) and that's all your `move` is trying to do, no? And in the general case, you have no guarantee that a pointer passed to `move` is the *only* pointer to the relevant object. So ISTM the cases where the Go implementation *can't* tell that c is no longer used, adding the `move` doesn't really help.
Do you have concrete evidence that this helps in some cases? Because otherwise it seems like a premature optimization that mostly makes the code harder to read. On Wed, 7 Jan 2026 at 04:25, Qingwei Li <[email protected]> wrote: > Take the following program as an example. > > ```go > c, err := getFtpConnection() > if err != nil { > return nil, err > } > go func(c2 *ftp.ServerConn) { > c2.List() > }(c) > // c will not be used later > ``` > > Let's add the `move` function. > > ```go > // pointer is > func move[PT *any](pp *PT) (res PT) { > res = *pp > *pp = nil > return > } > ``` > > ```go > // Goroutine G1 > c, err := getFtpConnection() > if err != nil { > return nil, err > } > go func(c2 *ftp.ServerConn) { // Goroutine G2 > c2.List() > }(move(&c)) > // c will not be used later > ``` > > Would this `move` without runtime support make GC unable to reach the > object pointed by `c` scanning from the stack of goroutine G1 so that the > ownership of object is entirely moved to goroutine G2? With this ownership > transfering, freegc for `c2` in the end of goroutine G2 is feasible in > cross-goroutine reference case. > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion visit > https://groups.google.com/d/msgid/golang-nuts/5c0b8aaf-8470-4de2-91f6-5d74e884dff3n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/5c0b8aaf-8470-4de2-91f6-5d74e884dff3n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGNpWnhjzKkq7MYvSS6v%3DVSpvVz%2BXJp4jM5s43rK%2BshZw%40mail.gmail.com.
