利用cgo,实现在go语言中调用c语言函数,取MAC和硬盘序列号。cgo中需要注意的:*/和import "C"两行之间不能有其它内容!
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/hdreg.h>
char * mac_serial(char *iface){
int fd;
struct ifreq ifr;
unsigned char *mac = NULL;
char *rtn=malloc(21);
errno=0;
memset(&ifr, 0, sizeof(ifr));
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) {
mac = (unsigned char *)ifr.ifr