I'm trying to use c strings in a c library in a go program, but error occurs:
a.h file:
extern const char *s[];
a.c file:
const char *s[] = {"a", "b", "c"};
I compile the two files to liba.a
main.go file:
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]))
}
then I run the code with command
go run main.go
I got the error:
panic: runtime error: index out of range [0] with length 0
how to fix it? thanks
go version: go version go1.15.7 linux/amd64