Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C中的字符串参数调用Go函数?_C_Go_Cgo - Fatal编程技术网

用C中的字符串参数调用Go函数?

用C中的字符串参数调用Go函数?,c,go,cgo,C,Go,Cgo,我可以调用一个Go函数,不需要C中的参数。它通过go build编译并打印 你好,来自Golang主功能! CFunction说:你好,来自CFunction的世界! 你好,来自GoFunction main.go package main //extern int CFunction(); import "C" import "fmt" func main() { fmt.Println("Hello from Golang main function!") //Calling a

我可以调用一个Go函数,不需要C中的参数。它通过
go build
编译并打印

你好,来自Golang主功能!
CFunction说:你好,来自CFunction的世界!
你好,来自GoFunction

main.go

package main

//extern int CFunction();
import "C"
import "fmt"

func main() {
  fmt.Println("Hello from Golang main function!")
  //Calling a CFunction in order to have C call the GoFunction
  C.CFunction();
}

//export GoFunction
func GoFunction() {
  fmt.Println("Hello from GoFunction!")
}
package main

//extern int CFunction();
import "C"
import "fmt"

func main() {
  fmt.Println("Hello from Golang main function!")
  //Calling a CFunction in order to have C call the GoFunction
  C.CFunction();
}

//export GoFunction
func GoFunction(str string) {
  fmt.Println("Hello from GoFunction!")
}
file1.c

#include <stdio.h>
#include "_cgo_export.h"

int CFunction() {
  char message[] = "Hello World from CFunction!";
  printf("CFunction says: %s\n", message);
  GoFunction();
  return 0;
}
#include <stdio.h>
#include "_cgo_export.h"

int CFunction() {
  char message[] = "Hello World from CFunction!";
  printf("CFunction says: %s\n", message);
  GoFunction(message);
  return 0;
}
file1.c

#include <stdio.h>
#include "_cgo_export.h"

int CFunction() {
  char message[] = "Hello World from CFunction!";
  printf("CFunction says: %s\n", message);
  GoFunction();
  return 0;
}
#include <stdio.h>
#include "_cgo_export.h"

int CFunction() {
  char message[] = "Hello World from CFunction!";
  printf("CFunction says: %s\n", message);
  GoFunction(message);
  return 0;
}
#包括
#包括“\u cgo\u export.h”
int CFunction(){
char message[]=“来自CFunction的Hello World!”;
printf(“CFunction显示:%s\n”,消息);
功能(信息);
返回0;
}
go build
时,我收到此错误:

/file1.c:7:14:错误:将“char[28]”传递给不兼容类型“GoString”的参数
./main.go:50:33:注意:在此处将参数传递给参数“p0”

其他参考资料:
https://blog.golang.org/c-go-cgo
(声誉不足,无法发布3个链接)
根据上面博文的“字符串和事物”部分:“Go和C字符串之间的转换是通过C.CString、C.GoString和C.GoString函数完成的。”但这些函数用于Go,如果我想将字符串数据传递到Go中,则没有帮助。

C中的字符串是
*C.char
,而不是Go
字符串。
让导出的函数接受正确的C类型,并根据需要在Go中进行转换:

//export GoFunction
func GoFunction(str *C.char) {
    fmt.Println("Hello from GoFunction!")
    fmt.Println(C.GoString(str))
}

如果要将C字符串传递给只接受Go字符串的函数,可以在C端使用
GoString
type:

char message[] = "Hello World from CFunction!";
printf("CFunction says: %s\n", message);
GoString go_str = {p: message, n: sizeof(message)}; // N.B. sizeof(message) will
                                                    // only work for arrays, not
                                                    // pointers.
GoFunction(go_str);
return 0;

我想OP是在问,如何调用只接受Go字符串的Go函数。但我想创建包装器函数是一种方法。@Ainar-G:好的,我应该等评论回复;)这管用!当我在排除故障时尝试此方法时,我错误地使用了
str*C.Char
,导致错误
无法确定C.Char
@joeprvay如果您确实需要
str
成为
string
,请查看我的答案。如果您阅读了下面的文档,将生成一个
\u cgo\u export.h
,类型为您可以使用的
GoString
。它看起来像:
typedef struct{const char*p;GoInt n;}GoString
非常感谢!我投了更高的票,但由于我的代表性不高,所以它不会出现在柜台上。