CGO usage for struct transfer

There is a C struct like this:
、、、
struct dcmi_chip_info {
unsigned char chip_type[MAX_CHIP_NAME_LEN];
unsigned char chip_name[MAX_CHIP_NAME_LEN];
unsigned char chip_ver[MAX_CHIP_NAME_LEN];
unsigned int aicore_cnt;
};
、、、

and a C func like this:
DCMIDLLEXPORT int dcmi_get_device_chip_info(int card_id, int device_id, struct dcmi_chip_info *chip_info);

Then I want to use CGO to get the result:
、、、
ret = C.dcmi_get_device_id_in_card(card_id_list[card_id_index],
(*C.int)(unsafe.Pointer(&device_id_max)),
(*C.int)(unsafe.Pointer(&mcu_id)),
(*C.int)(unsafe.Pointer(&cpu_id)))
/chip_info := C.struct_dcmi_chip_info{
chip_type: [C.MAX_CHIP_NAME_LEN]C.uchar{},
chip_name: [C.MAX_CHIP_NAME_LEN]C.uchar{},
chip_ver: [C.MAX_CHIP_NAME_LEN]C.uchar{},
}
/
chip_info := C.struct_dcmi_chip_info{}
ret = C.dcmi_get_device_chip_info(card_id_list[card_id_index], (C.int)(device_id_max), &chip_info)
fmt.Printf(“%T\n”, chip_info.chip_name)
fmt.Println(“chip_type”, chip_info.chip_name)
、、、

But what I get is

How can I get correct transfer? Thanks a lot

That looks like a null terminated string, of “MCU”. So you probably want to replace

fmt.Println(“chip_type”, chip_info.chip_name)

with

fmt.Println(“chip_type”, C.GoString(&chip_info.chip_name[0]))

Thanks, but the error is :
cannot use &chip_info.chip_name[0] (value of type *_Ctype_uchar) as *_Ctype_char value in argument to (_Cfunc_GoString)

Sorry about that. The *C.uchar will need to be converted to *C.char, as that is what C.GoString needs. The Go compiler will not allow the conversion to be performed directly like this (*C.char)(&chip_info.chip_name[0]).

However any pointer type can be converted to unsafe.Pointer, and unsafe.Pointer can be converted to any pointer type.

fmt.Println(C.GoString((*C.char)(unsafe.Pointer(&chip_info.chip_name[0]))))

That’s a lot of parentheses. Breaking it down, it could be written like this.

chipNameUCharPointer := &chip_info.chip_name[0]
chipNameUnsafePointer := unsafe.Pointer(chipNameUCharPointer)
chipNameCharPointer := (*C.char)(chipNameUnsafePointer)
chipNameGoString := C.GoString(chipNameCharPointer)
fmt.Println(chipNameGoString)

It really works for me. Thanks a lot