Convert from bool to integer (cgo)

5,461 views
Skip to first unread message

r.w.johnstone

unread,
Mar 17, 2011, 12:53:08 PM3/17/11
to golang-nuts
Hello Everyone,

Perhaps I'm missing the obvious, but I'm wrapping some C api's using
CGO, and I frequently run into the problem of converting Go's bool to
C.bool or C.int. Unfortunately, you can't perform these casts. I've
had to write a little dummy function to perform the conversion, which
seems unnecessarily verbose for such a simple operation. Perhaps I've
missed something obvious.

What is the preferred method of converting a bool into an integer?

Thank-you,

Robert

Gustavo Niemeyer

unread,
Mar 17, 2011, 1:26:49 PM3/17/11
to r.w.johnstone, golang-nuts
> had to write a little dummy function to perform the conversion, which
> seems unnecessarily verbose for such a simple operation.

cbool(b) feels just as good as bool(b) in those terms.

That said, it may be a good idea to have a C.CBool(b bool) int helper available.

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter

r.w.johnstone

unread,
Mar 17, 2011, 4:16:48 PM3/17/11
to golang-nuts
Hello,

You are right, the function call is not more verbose than the cast. I
misspoke. I really object to the requirement of a function call. A
conversion that would be essentially free on a C compiler, now
requires a function call plus a branching statement. I'm probably
micro-optimizing, but this just seems unnecessary.

In any case, I suspect that this is a common task for CGO users. I
figured that there would be a standard approach.

Robert

Gustavo Niemeyer

unread,
Mar 17, 2011, 4:50:34 PM3/17/11
to r.w.johnstone, golang-nuts
> You are right, the function call is not more verbose than the cast.  I
> misspoke.  I really object to the requirement of a function call.  A
> conversion that would be essentially free on a C compiler, now
> requires a function call plus a branching statement.  I'm probably
> micro-optimizing, but this just seems unnecessary.

Ah, ok, that's another topic indeed. Go doesn't allow implicit
cross-type casting, so while in Go land, cgo logic will behave as Go
code should.

We can make a C.CBool(b) operation be a NOOP, but you're right, you're
early optimizing if you're worried about this before you have a speed
problem at all.

Steven

unread,
Mar 17, 2011, 5:13:07 PM3/17/11
to r.w.johnstone, golang-nuts
On Thu, Mar 17, 2011 at 4:16 PM, r.w.johnstone <r.w.jo...@gmail.com> wrote:
Hello,

You are right, the function call is not more verbose than the cast.  I
misspoke.  I really object to the requirement of a function call.  A
conversion that would be essentially free on a C compiler, now
requires a function call plus a branching statement.  I'm probably
micro-optimizing, but this just seems unnecessary.

In any case, I suspect that this is a common task for CGO users.  I
figured that there would be a standard approach.

Robert

If you're using cgo, the least of your worries is a function call and a branch statement. The overhead of creating a C friendly environment to execute code in dwarfs an individual function call, and anything you are doing in C via cgo should in turn dwarf the overhead of calling into C.

jianzh...@gmail.com

unread,
Jul 31, 2017, 10:23:22 PM7/31/17
to golang-nuts


Hi, 

Any update about the C.CBool function supporting in CGO?

BRs,
Jian

Ian Lance Taylor

unread,
Jul 31, 2017, 10:42:26 PM7/31/17
to jianzh...@gmail.com, golang-nuts
On Mon, Jul 31, 2017 at 7:21 PM, <jianzh...@gmail.com> wrote:
>
> Any update about the C.CBool function supporting in CGO?

You are asking for an update, but as far as I know nobody has requested this.

Is it needed? How would C.CBool(i) differ from `i != 0`? The latter
seems clearer and shorter.

Ian
Reply all
Reply to author
Forward
0 new messages