C에서 매개 변수없이 Go 함수를 호출 할 수 있습니다 (per below). 이 go build
및 인쇄C에서 문자열 매개 변수로 Go 함수를 호출 하시겠습니까?
Hello from Golang main function! CFunction says: Hello World from CFunction! Hello from 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!")
}
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;
}
지금, 나는 문자열/문자 배열을 전달하려는 통해 컴파일 C에서 GoFunction으로
이 가능하다 cgo documentation에서 '이동 C 참조 "에 따르면, 그래서 GoFunction에 문자열 매개 변수를 추가하고 GoFunction에 문자 배열 message
합격 :
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(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(message);
return 0;
}
./file1.c:7:14: error: passing 'char [28]' to parameter of incompatible type 'GoString' ./main.go:50:33: note: passing argument to parameter 'p0' here
기타 참조 : https://blog.golang.org/c-go-cgo
위의 블로그 게시물의 "문자열과 사물"섹션에 따르면 을 (3 개 링크를 게시하는 것만으로는 충분하지 평판) : "변환 사이 go build
시나는이 오류가 나타납니다 이동 및 C 문자열은 C.CString, C.GoString 및 C.GoStringN 함수를 사용하여 수행됩니다. " 하지만 이것들은 Go에서 사용하기위한 것이며 문자열 데이터를 Go에 전달하려는 경우에는 도움이되지 않습니다.
그 아래의 문서를 읽는다면 사용할 수있는 GoString 형식의'_cgo_export.h '가 생성됩니다. 그것은 다음과 같이 보입니다 :'typedef struct {const char * p; GoInt n; } GoString' – JimB