Call c----type conversion in Go language

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.