runtime/cgo: clarify pinning requirement in Handle example

Add a comment to the void* example in the Handle documentation
highlighting that if the C code keeps the pointer after the call
returns, runtime.Pinner should be used to pin it.

The existing documentation already mentions this requirement in prose
(lines 69-73), but adding a comment directly in the example makes it
more visible and less likely to be missed when copying the example.

Fixes #68044

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Change-Id: I20a5b1ad10de1b441980dc7ed3185720e77912bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/741443
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
kovan
2026-02-03 15:34:08 +01:00
committed by Sean Liao
parent 6b7763407c
commit 26d5e16075

View File

@@ -97,6 +97,9 @@ import (
// func main() {
// val := "hello Go"
// h := cgo.NewHandle(val)
// // In this example, unsafe.Pointer(&h) is valid because myprint
// // does not keep a copy of the pointer. If the C code keeps the
// // pointer after the call returns, use runtime.Pinner to pin it.
// C.myprint(unsafe.Pointer(&h))
// // Output: hello Go
// }