Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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
Go:导入和C库之间存在冲突的类型_C_Go_Libgit2 - Fatal编程技术网

Go:导入和C库之间存在冲突的类型

Go:导入和C库之间存在冲突的类型,c,go,libgit2,C,Go,Libgit2,为了熟悉Go/C互操作,我想使用git2go/libgit2通过一个 因此我提出了这段代码(去掉了错误处理等),它输出了一个我无法放置的编译错误: /git.go:30:无法在git.NewOdbBackendFromC的参数中将odbBackendC(type*C.struct\u git\u odb\u backend)用作type*git.C.struct\u git\u odb\u backend 显然,编译器认为git.C.struct\u git\u odb\u后端和C.struc

为了熟悉Go/C互操作,我想使用git2go/libgit2通过一个

因此我提出了这段代码(去掉了错误处理等),它输出了一个我无法放置的编译错误:

/git.go:30:无法在git.NewOdbBackendFromC的参数中将odbBackendC(type*C.struct\u git\u odb\u backend)用作type*git.C.struct\u git\u odb\u backend

显然,编译器认为
git.C.struct\u git\u odb\u后端
C.struct\u git\u odb\u后端
是不同的类型,尽管它们是相同的-毕竟系统上只有一个libgit2。我能做些什么来解决这个问题

以下是完整的列表:

package main

/*
#cgo LDFLAGS: -L ./libgit2-backends/redis -lgit2 -lhiredis -lgit2-redis

#include <git2.h>

extern int git_odb_backend_hiredis(git_odb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password);
extern int git_refdb_backend_hiredis(git_refdb_backend **backend_out, const char* prefix, const char* path, const char *host, int port, char* password);

*/
import "C"

import (
  git "gopkg.in/libgit2/git2go.v23"
)

func ImportRepo(url string) {
  odb, err := git.NewOdb();

  var odbBackendC *C.git_odb_backend = nil
  C.git_odb_backend_hiredis(&odbBackendC, C.CString("prefix_"), C.CString("path"), C.CString("localhost"), 6379, C.CString(""))
  backend := git.NewOdbBackendFromC(odbBackendC)
  odb.AddBackend(backend)
}
主程序包
/*
#cgo LDFLAGS:-L./libgit2后端/redis-lgit2-lhiredis-lgit2redis
#包括
extern int git_odb_backend_hiredis(git_odb_backend**backend_out,常量字符*前缀,常量字符*路径,常量字符*主机,int端口,字符*密码);
extern int git_refdb_backend_hiredis(git_refdb_backend**backend_out,常量字符*前缀,常量字符*路径,常量字符*主机,int端口,字符*密码);
*/
输入“C”
进口(
git“gopkg.in/libgit2/git2go.v23”
)
func ImportRepo(url字符串){
odb,err:=git.NewOdb();
var odbBackendC*C.git_odb_backend=nil
C.git_odb_backend_hiredis(&odbBackendC,C.CString(“前缀”),C.CString(“路径”),C.CString(“本地主机”),6379,C.CString(“”)
后端:=git.NewOdbBackendFromC(odbBackendC)
AddBackend(后端)
}

我在尝试将git2go拆分为子包时遇到了同样的问题。据我所知,这是一个Go编译器试图使用Go作用域规则来编写C代码。我认为有两种方法可以解决这个问题:

  • 在git2go中实现
    git_odb
    接口,可以运行任意Go代码;或
  • 用C编写添加后端的代码,并从Go代码调用该代码

  • 谢谢!我将尝试实现第一种方法。这主要是一个学习项目,我可能会从中学到最多:)我想知道是否有人使用当前的ODB代码?我尝试了一些方法,但无法编译。libgit2使用的是
    git_odb
    本身,它都是相同的接口。您可能需要在C中编写一些这样的代码,因为Go本身不会做一些在C中有用的事情。