Search code examples
gocgo

What's the difference between int and C.int in go?


import "C"

func f() {
  var vGo int
  var vC  C.int
  // fails to compile with error
  // cannot use &vGo (type *int) as type *C.int in argument to...
  C.c_function(&vGo)  
  // compiles just fine:
  C.c_function(&vC)
}

I compile with CGO_ENABLED=1 GOARCH=arm...

What's the different in int and C.int types in this case?
Where do I find additional information on C types in GO?


Solution

  • What's the difference between the types? It depends. If you're on 64bit, the Go int will be 64 bits while the C int will be 32. If you're on 32bit, there is no real difference.

    Where do I find additional information on C types in Go? Look at documentation for C. As mentioned in the comments, implicit numeric type conversions aren't allowed in Go so a conversion is required.