What's the best way to detect if CGO is enabled?

354 views
Skip to first unread message

twp...@gmail.com

unread,
Jan 14, 2019, 6:26:03 PM1/14/19
to golang-nuts
Whether CGO is enabled or not affects how reliable certain standard library functions are. For example, os/user.LookupGroupId is untrustworthy if CGO is not enabled (example 1, example 2).

I can use build flags and two separate source files to determine whether CGO is enabled or not, for example in this commit.

This feels like it should be part of runtime, like runtime.GOOS and runtime.GOARCH.

Is there a better way to determine whether CGO is enabled?

Cheers,
Tom

Ian Lance Taylor

unread,
Jan 14, 2019, 6:52:25 PM1/14/19
to Tom Payne, golang-nuts
Build tags are the best way.

Although it's true that the use of cgo affects of a few standard
library functions, it's not remotely as fundamental as GOOS and
GOARCH. There is a way to check--via build tags--so I'm not sure we
need another mechanism.

Ian
Reply all
Reply to author
Forward
0 new messages