Skip to content

Commit b37b27b

Browse files
codekitchengopherbot
authored andcommitted
cmd/cgo: document errno value semantics
Added a section about errno values vs normal go error value semantics, and checking the return value of C functions for error before looking at errno. Fixes #63485 Change-Id: Id0132a9f11e4127f4adc14e010b7e17f57a0f7d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/616264 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e470a00 commit b37b27b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/cmd/cgo/doc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ function returns void). For example:
209209
_, err := C.voidFunc()
210210
var n, err = C.sqrt(1)
211211
212+
Note that the C errno value may be non-zero, and thus the err result may be
213+
non-nil, even if the function call is successful. Unlike normal Go conventions,
214+
you should first check whether the call succeeded before checking the error
215+
result. For example:
216+
217+
n, err := C.setenv(key, value, 1)
218+
if n != 0 {
219+
// we know the call failed, so it is now valid to use err
220+
return err
221+
}
222+
212223
Calling C function pointers is currently not supported, however you can
213224
declare Go variables which hold C function pointers and pass them
214225
back and forth between Go and C. C code may call function pointers

0 commit comments

Comments
 (0)