CGO, pass string parameter into C function.

3,198 views
Skip to first unread message

Viacheslav Chumushuk

unread,
May 20, 2011, 10:10:17 AM5/20/11
to golang-nuts
Hi there.

I'm trying to make go wrapper for C library. But can't even compile simple code.
Image that we have string variable:

s := "some go string"

And we have C-function that looks next (C-code I mean):
void foo(char const *);

And I can't understand (or guess) how to write Go-code.
C.foo(s) won't work and cause me next error message:
> cannot use s (type string) as type *_Ctype_char in function argument
What is the right way to pass string arguments?

Thanks for help.

--
Best regards, Viacheslav Chumushuk
email/jabber: vo...@root.ua
Ukraine, Khmelnitsky

Justin Lilly

unread,
May 20, 2011, 10:34:09 AM5/20/11
to golang-nuts
To convert from go string to C string, you're looking for
C.CString(my_go_string) to convert back, you're looking for
C.GoString(my_c_string)

You can find some examples of the back and forth in
https://github.com/str1ngs/go-git

-justin

Viacheslav Chumushuk

unread,
May 20, 2011, 10:55:36 AM5/20/11
to golang-nuts
Thank you for help. It is exactly what I was looking for.

On Fri, May 20, 2011 at 07:34:09AM -0700, Justin Lilly <jus...@justinlilly.com> wrote:
> To convert from go string to C string, you're looking for
> C.CString(my_go_string) to convert back, you're looking for
> C.GoString(my_c_string)
>
> You can find some examples of the back and forth in
> https://github.com/str1ngs/go-git
>
> -justin

--

Jeremy Cowgar

unread,
May 20, 2011, 11:06:47 AM5/20/11
to golang-nuts
Don't forget to free if it necessary, just my method of doing things,
prefix the created C var with c.

cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

C.MyFunc(cName)

Jeremy
http://jeremy.cowgar.com

Viacheslav Chumushuk

unread,
May 21, 2011, 3:58:35 AM5/21/11
to golang-nuts
Aha. Thanks a lot. I didn't know about that. I've found only this manual
http://golang.org/cmd/cgo/ It is very poor. Is there some other reading
about this topic?

On Fri, May 20, 2011 at 11:06:47AM -0400, Jeremy Cowgar <jco...@gmail.com> wrote:
> Don't forget to free if it necessary, just my method of doing things,
> prefix the created C var with c.
>
> cName := C.CString(name)
> defer C.free(unsafe.Pointer(cName))
>
> C.MyFunc(cName)
>
> Jeremy
> http://jeremy.cowgar.com

--

bflm

unread,
May 21, 2011, 6:42:57 AM5/21/11
to golang-nuts
On May 21, 9:58 am, Viacheslav Chumushuk <vo...@root.ua> wrote:
> Aha. Thanks a lot. I didn't know about that. I've found only this manualhttp://golang.org/cmd/cgo/It is very poor. Is there some other reading
> about this topic?

From your link:
"See $GOROOT/misc/cgo/stdio and $GOROOT/misc/cgo/gmp for examples."

and then e.g. from http://golang.org/misc/cgo/stdio/file.go :

33 func (f *File) WriteString(s string) {
34 p := C.CString(s)
35 C.fputs(p, (*C.FILE)(f))
36 C.free(unsafe.Pointer(p))
37 f.Flush()
38 }
39
40 func (f *File) Flush() {
41 C.fflush((*C.FILE)(f))
42 }
43
44 var Greeting = C.GoString(C.greeting)

and the e.g.: http://golang.org/src/cmd/cgo/out.go#L682 ...
Reply all
Reply to author
Forward
0 new messages