cgo编译器目前无法处理c函数中的变量参数和c头文件中的宏,所以你不能做一个简单的
// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
import "C"
func GetWinSz() {
var ts C.ttysize;
C.ioctl(0,C.TIOCGWINSZ,&ts)
}
为了绕过宏使用常量,所以
// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
import "C"
const TIOCGWINSZ C.ulong = 0x5413; // Value from Jed Smith's answer
func GetWinSz() {
var ts C.ttysize;
C.ioctl(0,TIOCGWINSZ,&ts)
}
然而,cgo 仍然会在 ioctl 原型中的 ... 上呕吐。你最好的选择是用带有特定数量参数的 c 函数包装 ioctl 并将其链接到其中。作为一种 hack,你可以在上面的注释中执行此操作 import "C"
// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
// void myioctl(int i, unsigned long l, ttysize * t){ioctl(i,l,t);}
import "C"
const TIOCGWINSZ C.ulong = 0x5413; // Value from Jed Smith's answer
func GetWinSz() {
var ts C.ttysize;
C.myioctl(0,TIOCGWINSZ,&ts)
}
我还没有测试过这个,但类似的东西应该可以工作。