This is a creation in Article, where the information may have evolved or changed.
1. Basic type conversion is simple, directly with coercion type conversion can be, as follows:
Go convert to C:var listen I listen to INTCI listen: = Listen to C.int (i) c convert to Go:var listen I listen to c.intgoi listen: = Listen int (i)
2. String type conversion is also not very difficult, one thing to note is that you need to make a small change in converting the char array in C to go string, as follows:
Go convert to C:var listen str Listen stringcstr listen: = Listen c.cstring (str) c convert to go:/* #include聽 <stdlib.h> #include聽 <stdio.h>char Listen foo[ ] Listen = Listen to "Hellofoo"; char listen to *bar listen = listen to "HelloBar"; */import listen to "C" Import Listen to "FMT" Func Listen to Main () listen {Listen and listen to the FMT. Printf ("%s\n", Listen to C.gostring (&c.foo[0)) listen and listen to the FMT. Printf ("%s\n", Listen to C.gostring (C.bar))}
3. Array type conversions
The following is an example of an array of int and float, as follows:
/* #include聽 <stdio.h>int listen to ciarray[] Listen = Listen {1, listen to 2, listen to 3, listen to 4, listen to 5, listen to 6, listen to 7};float listen to cfarray[] Listen = Listen {1.011, listen to 2.022, listen to 3.022, listen to 4.023, Listen to the 5.02, listen to 6.03, listen to 7.045};*/import listen to "C" Import Listen to "FMT" Func Listen to Main () listen to hear {Listen to listen to Goiarray listen: = Listen to c.ciarray[:] Listen to listen to the FMT. PRINTLN (Goiarray) listen to Gofarray listen to listen to: Listen to c.cfarray[:] Listen to listen to the FMT. Println (Gofarray)}
This article is from the "Linux related" blog, so be sure to keep this source http://diadia.blog.51cto.com/5440926/1737451