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)) | |
| } | |