1、代码
1 package main 2 3 import "fmt" 4 /* 5 #include <stdlib.h> 6 #include <stdio.h> 7 void hello() 8 { 9 printf("Hello World !\n"); 10 } 11 */ 12 import "C" 13 14 func Hello() { 15 C.hello(); 16 } 17 18 19 func Random() int { 20 return int(C.random()) 21 } 22 23 func Seed(i int) { 24 C.srandom(C.uint(i)) 25 } 26 27 func main () { 28 Seed(100) 29 fmt.Println("Random:", Random()) 30 Hello() 31 }
2、运行结果
$ go run cgo.go Random: 677741240 Hello World !
有疑问加站长微信联系(非本文作者)