猿问

cgo **char 切片字符串

我已经使用 cgo 开发了一个 pam 模块。不能转换为 []string a ** char



func pam_sm_authenticate(pamh *C.pam_handle_t, flags C.int, argc C.int, argv **C.char) int {

        fmt.Println(C.GoString(*argv[0]))

        return 0

}


错误是



invalid operation: argv[0] (type **C.char does not support indexing)


如果你知道,请告诉我。


繁星淼淼
浏览 220回答 1
1回答

阿晨1998

从 cgo wiki 拼凑而成:https : //github.com/golang/go/wiki/cgo#Turning_C_arrays_into_Go_slices。import "C"import "unsafe"func GoStrings(argc C.int, argv **C.char) []string {&nbsp; &nbsp; length := int(argc)&nbsp; &nbsp; tmpslice := (*[1 << 30]*C.char)(unsafe.Pointer(argv))[:length:length]&nbsp; &nbsp; gostrings := make([]string, length)&nbsp; &nbsp; for i, s := range tmpslice {&nbsp; &nbsp; &nbsp; &nbsp; gostrings[i] = C.GoString(s)&nbsp; &nbsp; }&nbsp; &nbsp; return gostrings}
随时随地看视频慕课网APP

相关分类

Go
我要回答