Skip to content

Instantly share code, notes, and snippets.

@alfg
Created January 17, 2017 06:20
Show Gist options
  • Save alfg/6eb0a18f2f9d155f11f3cc20be7a5393 to your computer and use it in GitHub Desktop.
Save alfg/6eb0a18f2f9d155f11f3cc20be7a5393 to your computer and use it in GitHub Desktop.
Golang CGO Example
package main
/*
#include <stdlib.h>
#include <time.h>
*/
import "C"
import (
"fmt"
)
func main() {
// Seed rand generator.
seed(int(C.time(nil)))
// Output 10 random numbers from 0-100.
for i := 1; i <= 10; i++ {
rand := random()
fmt.Println(rand)
}
}
func random() int {
return int(C.rand()) % 100
}
func seed(i int) {
C.srand(C.uint(i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment