Skip to content

Instantly share code, notes, and snippets.

@physacco
Last active December 15, 2015 04:38
Show Gist options
  • Save physacco/5202626 to your computer and use it in GitHub Desktop.
Save physacco/5202626 to your computer and use it in GitHub Desktop.
This is a very trivial demo that shows the basic usage of cgo.

This is a very trivial demo that shows the basic usage of cgo.

Build and Run:

$ go build main.go
$ ./main
hello, cgo
3
#include <stdio.h>
#include "add.h"
void Hello() {
printf("hello, cgo\n");
}
int Add(int a, int b) {
return a + b;
}
package add
// #include "add.h"
import "C"
func Hello() {
C.Hello()
}
func Add(a, b int) (c int) {
c = int(C.Add(C.int(a), C.int(b)))
return
}
extern void Hello();
extern int Add(int, int);
// +build ignore
package main
import (
"."
"fmt"
)
func main() {
add.Hello();
fmt.Println(add.Add(1, 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment