struct和byte類型轉換
import (
"fmt"
"unsafe"
)
type TestStructTobytes struct {
data int64
}
type SliceMock struct {
addr uintptr
len int
cap int
}
func main() {
var testStruct = &TestStructTobytes{100}
Len := unsafe.Sizeof(*testStruct)
testBytes := &SliceMock{
addr: uintptr(unsafe.Pointer(testStruct)),
cap: int(Len),
len: int(Len),
}
data := *(*[]byte)(unsafe.Pointer(testBytes))
fmt.Println("[]byte is : ", data)
}
運行結果:
[]byte is : [100 0 0 0 0 0 0 0]
因為[]byte底層的數據結構為:
struct {
addr uintptr
len int
cap int
}
其中addr為數值的地址,len為當地數值的長度,cap為數值的容量。
轉換的時候,需要定義一個和[]byte底層結構一致的struct(如例子中的SliceMock),然后把結構體的地址賦給addr,結構體的大小賦給len和cap。最后將其轉換為[]byte類型。
2、將[]byte轉換為struct,轉換方法如下:
import (
"fmt"
"unsafe"
)
type TestStructTobytes struct {
data int64
}
type SliceMock struct {
addr uintptr
len int
cap int
}
func main() {
var testStruct = &TestStructTobytes{100}
Len := unsafe.Sizeof(*testStruct)
testBytes := &SliceMock{
addr: uintptr(unsafe.Pointer(testStruct)),
cap: int(Len),
len: int(Len),
}
data := *(*[]byte)(unsafe.Pointer(testBytes))
fmt.Println("[]byte is : ", data)
var ptestStruct *TestStructTobytes = *(**TestStructTobytes)(unsafe.Pointer(&data))
fmt.Println("ptestStruct.data is : ", ptestStruct.data)
}
運行結果:
[]byte is : [100 0 0 0 0 0 0 0]
ptestStruct.data is : 100
從上面的例子中將[]byte轉換為struct的代碼片段為:
var ptestStruct *TestStructTobytes = *(**TestStructTobytes)(unsafe.Pointer(&data))
分析:
由於在golang中編譯器不將[]byte視為指針,所以要使用其地址進行轉換,由於[]byte底層存放的是指向數據的地址。用[]byte的地址就需要使用雙層指針轉換,然后再指向其內容,得出來的就是轉換對應struct的指針了。