Go语言练习:go语言与C语言的交互——cgo
2023-09-11 14:18:54 时间
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 !
相关文章
- go 优雅的检查channel关闭
- 【Go命令教程】13. go tool cgo
- 【Go命令教程】10. go fix 与 go tool fix
- 如何安装 第三方 Go 离线包? (GOPATH、 go install)
- Go 语言体系下的微服务框架选型:Dubbo-go
- 【Go语言】【6】GO语言的数组
- 【Go语言】【3】GO语言常量
- 【Go语言】【18】GO语言的select
- 【Go语言】【11】GO语言的包和函数
- go template模板的常用基本语法
- go异常处理:自定义异常代码示例
- golang beego快速入门示例(单文件hello.go)
- [Go] Viper 加载项目配置,go build 打包配置文件进二进制
- Atitit golang开发环境搭建 目录 1. 编辑helo.go1 1.1. 调试编译1 2. Ide选择liteide2 3. 问题解决2 4. 附录2 4.1. Go语言标准库常
- Go语言自学系列 | go常用命令
- Go语言自学系列 | golang函数的返回值
- Go语言自学系列 | go语言访问数组元素
- Go语言自学系列 | go语言数据类型
- GO语言教程(一)Linux( Centos)下Go的安装, 以及HelloWorld