cgo **char 切片字符串

cgo **char to slice string

我使用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)

知道的请告诉我

从 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 {

    length := int(argc)
    tmpslice := (*[1 << 30]*C.char)(unsafe.Pointer(argv))[:length:length]
    gostrings := make([]string, length)
    for i, s := range tmpslice {
        gostrings[i] = C.GoString(s)
    }
    return gostrings
}