怎么把Go struct转换成C struct?

I want to use cgo. On the C side I have struct containing 32 bytes.

struct hash {
    uint8_t bytes[32];
};

On the Go size I have very similar type.

type Hash [32]bytes

I would like to call a C function void f(struct hash) with an instance of Hash type. If possible I would also like to avoid a copy of the value.

Simple assignment Chash C.struct_hash := hash does not work:

cannot use hash (type Hash) as type C.struct_hash in assignment

You can use the unsafe package to cast the data. The Go compiler can no longer check the types here, so it's up to you to ensure that the size and type of everything is identical between C.struct_hash and Hash.

Chash := *(*C.struct_hash)(unsafe.Pointer(&hash))

consider the align in c you can not simply do the cast. the best way should be assign value 1 by 1 from c to go or go to c. But this process is painful is the struct is large and struct contain struct