aaaa.go 719 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package C
  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. }
  39. func main() {
  40. GetFoo("111")
  41. }