The issue here is you have two different types. You have an int
which is defined by your Go compiler and C.int
which is defined by your C compiler. Depending on the compilers and architecture these may be the same size. However, it is very possible that int
will be 64 bits in size and C.int
would be 32 bits.
In order for the compiler to enforce the type safety, it requires you to do an explicit conversion or define the type at initialization.
In your example, you do: x := 0
. This is the same as x := int(0)
. The int
is implied. Instead, you can do x := C.int(0)
.
However, I think the "cleaner" method would be to do what jnml recommended: var x C.int
. This does exactly the same thing but just looks nicer.
Later, you will most likely want to convert that C.int
back to an int
. This is easy to do with a conversion.
y := int(x)
This will convert x
to an int
and then copy that value into the newly initialized variable y
.
Bonus Anwser: int* len
is indeed the same as int *len