我正在尝试在 go 程序的 c 库中使用 c 字符串,但发生错误:
a.h文件:
extern const char *s[];
a.c文件:
const char *s[] = {"a", "b", "c"};
我将这两个文件编译为liba.a
main.go文件:
package main
// #cgo LDFLAGS: -L${SRCDIR} -la
// #include"a.h"
import "C"
import "fmt"
func main() {
x := 1
fmt.Println(x)
fmt.Println(len(C.s))
fmt.Println(C.GoString(C.s[x]))
}
然后我用命令运行代码
go run main.go
我得到错误:
panic: runtime error: index out of range [0] with length 0
如何解决?谢谢
转到版本:go version go1.15.7 linux/amd64
江户川乱折腾
相关分类