aaaa.go 690 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package Cgo
  2. /*
  3. // C 标志io头文件,你也可以使用里面提供的函数
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define LEN 1024
  8. void pri(){
  9. printf("Welcome to the C World!\n");
  10. }
  11. int add(int a,int b){
  12. return a+b;
  13. }
  14. char* Foo( char *input ) {
  15. char* res = malloc( LEN * sizeof( char ) );
  16. sprintf( res, "%s %s", input, "World!" );
  17. return res;
  18. }
  19. */
  20. import "C" // 切勿换行再写这个
  21. import (
  22. "fmt"
  23. "unsafe"
  24. )
  25. func Getpri() {
  26. C.pri()
  27. }
  28. func GetAdd() {
  29. fmt.Println(C.add(2, 1))
  30. }
  31. func GetFoo(a string) {
  32. cs := C.CString(a)
  33. res := C.Foo(cs)
  34. str := C.GoString(res)
  35. C.free(unsafe.Pointer(cs))
  36. C.free(unsafe.Pointer(res))
  37. fmt.Println(str)
  38. }