为何结合两者
c 语言的强大之处在于使用很多底层库,性能高,语言简洁,设计上和架构上来说更为直接,而go语言像httpserver,webrtc等已经做得很好了,协程方面也封装了细节,程序员不用关心如何调整cpu和线程, 如果说两者结合,更能直接达到我们想要的效果。
go语言比如嵌入ffmpeg来进行编解码 // #include “videostreamer.h” // #include // #cgo LDFLAGS: -lavformat -lavdevice -lavcodec -lavutil // #cgo CFLAGS: -std=c11 // #cgo pkg-config: libavcodec
以上是嵌入了ffmpeg的库,我们先用简单的例子来嵌入简单的c,以作示例 /* #include #include int num = 0; */ import “C”
func main(){ a := int(C.num) fmt.Println(a) }
mingw若在windows上,可能出现 exec: “gcc”: executable file not found in %PATH% 我们必须安装mingw,地址 mingw安装地址 mingw64 官方网站 安装完成,在进行运行就行了,因为go语言并不自带c编译器,所以调用的是外部gcc编译。
c加入c后的go语言更为突出亮点,其他语言并无此直接调用的好处。
package main
/*
#include
#include
void hello_world() {
printf("I am the monkey!\n");
}
*/
import "C"
func main() {
C.hello_world()
}