先是类型对照
C语言类型 | CGO类型 | Go语言类型 |
---|---|---|
char | C.char | byte |
singed char | C.schar | int8 |
unsigned char | C.uchar | uint8 |
short | C.short | int16 |
unsigned short | C.ushort | uint16 |
int | C.int | int32 |
unsigned int | C.uint | uint32 |
long | C.long | int32 |
unsigned long | C.ulong | uint32 |
long long int | C.longlong | int64 |
unsigned long long int | C.ulonglong | uint64 |
float | C.float | float32 |
double | C.double | float64 |
size_t | C.size_t | uint |
bool | 懒得试 | int32 |
bool :0为false,1为true.
结构体的小例子
type DDOSINFO struct {
time int32 // 攻击时间 /分钟
flag int32 // 攻击类型
target [256]byte // 目标
port int32 // 端口
nthread int32 // 线程
count1 int32 //第一轮回参数
count2 int32 //第二轮回参数
bAttack int32
dwToken uint32 //命令ID
}
typedef struct _MSGHEAD
{
int time; // 攻击时间 /分钟
int flag; // 攻击类型
char target[256]; // 目标
int port; // 端口
int nthread; // 线程
int count1;//第一轮回参数
int count2;//第二轮回参数
BOOL bAttack;
DWORD dwToken; //命令ID
}MSGHEAD,*PMSGHEAD;
Go中结构体转[]byte与[]byte转结构体
package main
import (
"fmt"
"unsafe"
)
type x struct {
a string
b []byte
c int
}
type SliceMock struct {
addr uintptr
len int
cap int
}
func main() {
a := new(x)
a.a = "121626"
a.b = []byte("小学生到此一游")
a.c = 8848
Len := unsafe.Sizeof(*a) //sysinfo为结构体
testBytes := &SliceMock{
addr: uintptr(unsafe.Pointer(a)),
cap: int(Len),
len: int(Len),
}
data := *(*[]byte)(unsafe.Pointer(testBytes)) //data为[]byte
ddosinfo := *(**x)(unsafe.Pointer(&data)) //data2为[]byte
fmt.Println(ddosinfo)
}