为什么CGO不能直接调用C中的函数指针?

Why CGO can't directly call function pointer in C?

在 C 中定义

typedef err_t(* netif_input_fn) (struct pbuf *p, struct netif *inp);

运行 在 GO

// netif.input is function pointer defined in C
netif.input(buf, netif)
// got error: cannot call non-function netif.input (type _Ctype_netif_input_fn)

Go 中的 CGO 函数是一个 C 函数,需要特殊处理以处理其堆栈。

Go 中的一个函数(又名 goroutine)以较小的堆栈大小 (2 kB) 开始,这个堆栈将自动扩展或收缩。虽然 CGO 函数无法从该功能中受益,但其初始堆栈大小在大多数操作系统上为 2MB,并将在不同的堆栈(g0 堆栈)上执行。所以Go运行时不能把一个CGO函数当作一个普通的Go函数(goroutine)来直接调用。