Using CString with cgo and different C types

Let’s say that I have two different C methods

// My C methods:
MyFirstMethod(const char *str)
MySecondMethod(const char * const str)

I know that I can use this code to pass my variable to the first method

var strPtr *C.char
strPtr = C.CString("My String Here")
defer C.free(unsafe.Pointer(strPtr))
res := C.MyFirstMethod(strPtr)

However, I am not sure for the second one, is there any difference? Should I use the same code? I know that there are not the same type and I am not sure if I should use something else to pass my argument to the second method.

Here is a topic that explains the difference between const char * and const char * const

There’s no difference here. const char * is a changeable pointer to zero or more unchangeable chars. const char * const is an unchangeable pointer to zero or more unchangeable chars. The changeability of the underlying buffer allocated by C.CString doesn’t matter to your C code. You could even pass the data as char * or char * const.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.