12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package C
- /*
- // C 标志io头文件,你也可以使用里面提供的函数
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LEN 1024
- void pri(){
- printf("Welcome to the C World!\n");
- }
- int add(int a,int b){
- return a+b;
- }
- char* Foo( char *input ) {
- char* res = malloc( LEN * sizeof( char ) );
- sprintf( res, "%s %s", input, "World!" );
- return res;
- }
- */
- import "C" // 切勿换行再写这个
- import (
- "fmt"
- "unsafe"
- )
- func Getpri() {
- C.pri()
- }
- func GetAdd() {
- fmt.Println(C.add(2, 1))
- }
- func GetFoo(a string) {
- cs := C.CString(a)
- res := C.Foo(cs)
- str := C.GoString(res)
- C.free(unsafe.Pointer(cs))
- C.free(unsafe.Pointer(res))
- fmt.Println(str)
- }
- func main() {
- GetFoo("111")
- }
|