Skip to content

Instantly share code, notes, and snippets.

@zchee
Last active Oct 19, 2021
Embed
What would you like to do?
cgo convert list

See also, http://libraryofalexandria.io/cgo/

Using Go cgo

cgo has a lot of trap.
but Not "C" pkg also directory in $GOROOT/src. IDE's(vim) Goto command not works.

So, Here collect materials.

cgo compiling file extension

C

.c, .s, .S

C++

.cc, .cpp, .cxx

Header

Any .h, .hh, .hpp, .hxx files will not be compiled separately.
but, if these header files are changed, the C and C++ files will be recompiled.

cgo environment variable

Building Go

CC, CXX, CC_FOR_TARGET, CXX_FOR_TARGET

Compiling .go

CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_LDFLAGS

cgo convert C-Go string type

string

// Go string to C string; result must be freed with C.free
func C.CString(string) *C.char

// C string to Go string
func C.GoString(*C.char) string

// C string, length to Go string
func C.GoStringN(*C.char, C.int) string

// C pointer, length to Go []byte
func C.GoBytes(unsafe.Pointer, C.int) []byte

C Types in Go

char

type C.char
type C.schar (signed char)
type C.uchar (unsigned char)

short

type C.short
type C.ushort (unsigned short)

int

type C.int
type C.uint (unsigned int)

long

type C.long
type C.ulong (unsigned long)

longlong

type C.longlong (long long)
type C.ulonglong (unsigned long long)

float

type C.float  

double

type C.double

Directly access struct type

As in C.struct_stat

struct

type C.struct_***

union

type C.union_***

enum

type C.enum_***

void*

See also https://golang.org/pkg/unsafe/

func unsafe.Pointer() *ArbitraryType
@muratsplat

This comment has been minimized.

Copy link

@muratsplat muratsplat commented Mar 1, 2019

good stuf

@aleitner

This comment has been minimized.

Copy link

@aleitner aleitner commented Apr 29, 2019

thank you!

@HongkaiWen

This comment has been minimized.

Copy link

@HongkaiWen HongkaiWen commented Sep 13, 2019

thank you!!!

@zchee

This comment has been minimized.

Copy link
Owner Author

@zchee zchee commented Sep 13, 2019

:)

@cuiweixie

This comment has been minimized.

Copy link

@cuiweixie cuiweixie commented Jun 11, 2020

thank you!

@stevesobol

This comment has been minimized.

Copy link

@stevesobol stevesobol commented Jun 16, 2020

THANK YOU THANK YOU THANK YOU

@Techemist

This comment has been minimized.

Copy link

@Techemist Techemist commented Jul 20, 2020

Hi,
Thanks for this. I faced with a problem lately. C.char in some platform is signed by default whereas in some others it is unsigned. so I faced with this error when trying to use C.GoString function:
cannot use _Cfunc_myfunc() (type *_Ctype_uchar) as type _Ctype_char in argument to _Cfunc_GoString
It turned out that myfunc() which return char
translated to uchar instead of char. do you know how to force the cgo to deal with it?

@lochv

This comment has been minimized.

Copy link

@lochv lochv commented Oct 2, 2020

@zchee it possible to convert *net.Conn to *_Ctype_int: go net.Conn to c SOCKET?

@zchee

This comment has been minimized.

Copy link
Owner Author

@zchee zchee commented Oct 3, 2020

@lochv I don't know yet. This gist created about 3 years ago(?)
Might be helpful you dive to runtime/cgo

@mhh12121

This comment has been minimized.

Copy link

@mhh12121 mhh12121 commented Jan 21, 2021

Hi,
Thanks for this. I faced with a problem lately. C.char in some platform is signed by default whereas in some others it is unsigned. so I faced with this error when trying to use C.GoString function:
cannot use _Cfunc_myfunc() (type *_Ctype_uchar) as type __Ctype_char in argument to Cfunc_GoString It turned out that myfunc() which return char translated to uchar instead of char. do you know how to force the cgo to deal with it?

damn, I met the same problem....

@matthew-williamson-sd

This comment has been minimized.

Copy link

@matthew-williamson-sd matthew-williamson-sd commented Feb 23, 2021

Hi,
Thanks for this. I faced with a problem lately. C.char in some platform is signed by default whereas in some others it is unsigned. so I faced with this error when trying to use C.GoString function:
cannot use _Cfunc_myfunc() (type *_Ctype_uchar) as type __Ctype_char in argument to Cfunc_GoString It turned out that myfunc() which return char translated to uchar instead of char. do you know how to force the cgo to deal with it?

damn, I met the same problem....

Not sure if this works for everyone but I've been using C.GoBytes to return the byte array and then convert that in go.

foo := C.GoBytes(unsafe.Pointer(buffer), C.int(size))
bar := string(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment