1.Go language access C language functions:

import "C"/ *#include <stdio.h>

void say() {printf("hello world\n");
}
*/
import "C"

func main() {/* 1.C code needs to be commented with single-line registration or multi-line comments 2. Write import next to the C code"C"3. You can access C functions in go code by using c. function names. Note: 1"C"2.C code can be commented with a multi-line comment, or can be commented with a single-line comment */ c.say ()}Copy the code

2.Go language access C data:

/ *#include <stdio.h>
char ch = 'b'; int num = 123; A double value = 3.14; */ import"C"
import "fmt"

func main// fmt.println (C) // fmt.println (C) // fmt.println (C) // fmt.println (C) // fmt.printf (C) // fmt.println (C) // fmt.println (C) // fmt.printf (C) // fmt.println (C) // fmt.println (C) // fmt.println (C) // fmt.printf (C)"%T\n", C.ch)
   //fmt.Printf("%T\n", C.num)
   //fmt.Printf("%T\n", C.value)

   var ch byte
   ch = byte(C.ch)
   fmt.Println(ch)

   var num int
   num = int(C.num)
   fmt.Println(num)

   var value float64
   value = float64(C.value)
   fmt.Println(value)
}Copy the code

3.Go language access string:

/ *#include <stdio.h>
#include <stdlib.h>

char *str1 = "www.it666.com";
char str2[20] = "www.it666.com";
 */
import "C"
import (
   "fmt"
   "unsafe"
)

func mainPrintln(c.tr1) var str1 str1 = c.gostring (c.tr1) FMT.Println(c.tr1) var str1 str1 = c.gostring (c.tr1) FMT.Println() {/* // fmt.println (c.tr1) var str1 str1 = c.gostring (c.tr1) fmt.println () Var str2 string //str2 = c.Gostring (c.tr2) str2 = C.Gostring (&c.tr2 [0]) Println(str2) */ // if you convert a Go string to a C string, the string is a garbage collector that is not controlled by the Go GC. Var STR string ="www.itzb.com"Str2 := c.unsafe.pointer (str2)} c.unsafe.pointer (str2)}Copy the code

4.Go language access pointer:

/ *#include <stdio.h>
int num = 123;
int *p1 = &num;
void *p2 = &num;
 */
import "C"
import (
   "fmt"
   "unsafe"
)

func main() {
   /*
   var p1 *C.int = C.p1
   //fmt.Printf("%T\n", (p1) / / FMT. Println (* (p1) p1 / / / / var num = * FMT. Println (num) * / / / attention: All other types can be represented in the format above, but void *, //var p2 * c.oid = c.p2 var p2 unsafe.Pointer = c.p2 fmt.println (p2)}Copy the code

5. Access enumeration in C:

/ *#include <stdio.h>
enum Gender {
   GenderMale,
   GenderFemale,
   GenderYao
};
*/
import "C"
import "fmt"

func main() {// Go accesses enumerations in C just like normal variables, through C. Var gender c. ennum_gender = C. genderyao fmt.println (gender)}Copy the code

6. Access the structure in C:

/ *#include <stdio.h>
struct Point {
    float x;
    float y;
};
*/
import "C"
import "fmt"

func mainVar PI c.truct_point = c.truct_point {1.1, 2.2} fmt.println (pi.x) fmt.println (pi.y)} var PI c.truct_point = c.truct_point {1.1, 2.2} fmt.println (pi.x) fmt.println (pi.y)}Copy the code

7. Access arrays in C:

/ *#include <stdio.h>
int cArray[5] = {1, 2, 3, 4, 5};
*/
import "C"
import "fmt"

func main() {
   var arr [5]C.int = C.cArray
   fmt.Println(arr)
   fmt.Println(arr[0])
   fmt.Println(arr[1])
}Copy the code

Example:

/ *#include <stdio.h>char lowerCase(char ch){ // 1. Checks whether the current character is lowercaseif(ch >= 'a' && ch <= 'z') {returnch; } // Note: cannot be written directlyelseBecause the implementation toelseIt doesn't have to be a capital letterelse if(ch >= 'A' && ch <= 'Z') {return ch + ('a' - 'A');
    }
    return ' ';
}
char getCh(){// 1. scanf("%c", &ch); setbuf(stdin, NULL); Ch = lowerCase(ch); // 3. Return converted charactersreturn ch;
}
 */
import "C"
import "fmt"

func main() {
   for {
      fmt.Println("Please enter one character")
      ch := C.getCh()
      fmt.Printf("%c\n", ch)
   }
}Copy the code


Code: https://github.com/codeXiaoQiang/Go-language