强制定义Go结构以将unsafe.Pointer()转换为C结构

gss*_*der 3 c pointers language-interoperability go

与C代码互操作,我无法直接转换结构,我被迫在Go中定义一个等效的结构.来自的C函数libproc.h

int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize)
Run Code Online (Sandbox Code Playgroud)

为C结构flavor==PROC_PIDTASKINFOproc_taskinfo如在定义sys/proc_info.h(由包括libproc.h):

struct proc_taskinfo {
    uint64_t        pti_virtual_size;   /* virtual memory size (bytes) */
    uint64_t        pti_resident_size;  /* resident memory size (bytes) */
    uint64_t        pti_total_user;     /* total time */
    uint64_t        pti_total_system;
    uint64_t        pti_threads_user;   /* existing threads only */
    uint64_t        pti_threads_system;
    int32_t         pti_policy;     /* default policy for new threads */
    int32_t         pti_faults;     /* number of page faults */
    int32_t         pti_pageins;        /* number of actual pageins */
    int32_t         pti_cow_faults;     /* number of copy-on-write faults */
    int32_t         pti_messages_sent;  /* number of messages sent */
    int32_t         pti_messages_received;  /* number of messages received */
    int32_t         pti_syscalls_mach;  /* number of mach system calls */
    int32_t         pti_syscalls_unix;  /* number of unix system calls */
    int32_t         pti_csw;            /* number of context switches */
    int32_t         pti_threadnum;      /* number of threads in the task */
    int32_t         pti_numrunning;     /* number of running threads */
    int32_t         pti_priority;       /* task priority*/
};
Run Code Online (Sandbox Code Playgroud)

即使Go代码确实有效,我也无法C.proc_taskinfo直接使用.旅途中的功能是propertiesOf():完整的源代码在这里.

如果我引用C结构,我在关于这个主题的最新问题中报告了类似的错误:could not determine kind of name for C.proc_taskinfo但是这次我确定定义是导入的#include.

Ain*_*r-G 7

根据文件

要直接访问struct,union或enum类型,请在其前面加上struct_,union_或enum_,如C.struct_stat中所示.

使用C.struct_proc_taskinfo.