golang里如何将一个struct指针转换成slice
废话不说了,直接上代码[plain]package mainimport "unsafe"import "fmt"func abc() {type T struct {a intb intc int}type SliceHeader struct {addr uintptrlen intcap int}t := &T{a: 1, b: 2, c: 3}p := unsafe.Sizeof(*t)println(int(p))sl := &SliceHeader{addr: uintptr(unsafe.Pointer(t)),len: int(p),cap: int(p),}b := *(*[]byte)(unsafe.Pointer(sl))println(len(b))fmt.Println(b)b[0] = 7b[4] = 5b[8] = 8fmt.Println(t)}func main() {abc()}
补充:综合编程 , 其他综合 ,