Access C array of type const char * from Go

感情迁移 提交于 2019-12-02 03:33:45

While you could do the pointer arithmetic yourself, it's safer and more convenient to convert the C array into a Go slice first.

arraySize := 3
cStrings := (*[1 << 30]*C.char)(unsafe.Pointer(&C.myStringArray))[:arraySize:arraySize]

for _, cString := range cStrings {
    fmt.Println(C.GoString(cString))
}

// prints:
// NAME_OF_FIRST_THING
// NAME_OF_SECOND_THING
// NAME_OF_THIRD_THING

Relevant cgo wiki entry: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!