0
package main
/*
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <utmpx.h>
#include <fcntl.h>
#include <unistd.h>
char *path_utmpx = _PATH_UTMPX;
typedef struct utmpx utmpx;
*/
import "C"
import (
"fmt"
"io/ioutil"
)
type Record C.utmpx
func main() {
path := C.GoString(C.path_utmpx)
content, err := ioutil.ReadFile(path)
handleError(err)
var records []Record
// now we have the bytes(content), the struct(Record/C.utmpx)
// how can I cast bytes to struct ?
}
func handleError(err error) {
if err != nil {
panic("bad")
}
}
我正在尝试将content
转换为Record
我已经提出了一些相关问题。如何投入字节结构(C结构)在去?
Cannot access c variables in cgo
我看过一些文章和帖子,但仍然无法想出一个办法做到这一点。
我知道这个回购,我已经读过它。我只想尝试一下。感谢您的回答。我得到了'undefined:utmp.ReadUtmp','undefined:utmp.UtmpxFile'。 –
我想等着看有没有其他答案。 –
GZ薛,你运行了'go get github.com/ericlagergren/go-gnulib/utmp'来安装utmp库吗? – Mark