Can hand-written move function without runtime support take the ownership of an object entirely?

58 views
Skip to first unread message

Qingwei Li

unread,
Jan 6, 2026, 10:29:21 PM (2 days ago) Jan 6
to golang-nuts
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 `move` function.

```go
func move[T any, PT *T](pp *PT) (res PT) {
res = *pp
*pp = nil
return
}

// 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 G? If it does, the ownership of object is entirely moved to goroutine G2. With the ownership transfering, freegc for `c2` in the end of goroutine G2 is feasible in cross-goroutine reference case.

Reply all
Reply to author
Forward
0 new messages