The method of embedding the C language in the Go language

  • 2020-05-26 09:17:04
  • OfStack

This article demonstrates an example of how to embed the C language in the Go language. Share with you for your reference. The specific analysis is as follows:

The Go language official has a tool called cgo, which makes it easy to embed the C code in the Go language code or to integrate the C and Go codes. Here is a simple piece of C experiment code embedded in Go:

package main
#include <stdio.h>
#include <stdlib.h>
void say_hello() {
        printf("Hello World!\n");
import "C"
func main() {

Save the above code to ctest.go and run go run ctest.go to see the effect immediately.

It is convenient to call a function written by C in Go code without having to implement a special interface or declare anything yourself.

I hope this article has been helpful to your programming of Go language.

Related articles: