Go调用Rust方法及外部函数接口前置


Posted in Golang onJune 14, 2022

前言

近期 Rust 社区/团队有些变动,所以再一次将 Rust 拉到大多数人眼前。

我最近看到很多小伙伴说的话:

  • Rust 还值得学吗?社区是不是不稳定呀
  • Rust 和 Go 哪个好?
  • Rust 还值得学吗?

这些问题如果有人来问我,那我的回答是:

小孩子才做选择,我都要!

当然,关于 Rust 和 Go 的问题也不算新,比如之前的一条推文:

Go调用Rust方法及外部函数接口前置

我在本篇中就来介绍下如何用 Go 调用 Rust。

当然,这篇中我基本上不会去比较 Go 和 Rust 的功能,或者这种方式的性能之类的,Just for Fun

FFI 和 Binding

FFI (Foreign Function Interface) 翻译过来叫做外部函数接口(为了比较简单,下文中都将使用 FFI 指代)。最早来自于 Common Lisp 的规范,这是在 wiki 上写的,我并没有去考证。 不过我所使用过的绝大多数语言中都有 FFI 的概念/术语存在,比如:Python、Ruby, Haskell、Go、Rust、LuaJIT 等。

FFI 的作用简单来说就是允许一种语言去调用另一种语言,有时候我们也会用 Binding 来表示类似的能力。

在不同的语言中会有不同的实现,比如在 Go 中的 cgo , Python 中的 ctypes , Haskell 中的 CAPI (之前还有一个 ccall)等。 我个人感觉 Haskell 中用 FFI 相比其他语言要更简单&方便的多,不过这不是本篇的重点就不展开了。

在本文中,对于 Go 和 Rust 而言,它们的 FFI 需要与 C 语言对象进行通信,而这部分其实是由操作系统根据 API 中的调用约定来完成的。

我们来进入正题。

准备 Rust 示例程序

Rust 的安装和 Cargo 工具的基本使用,这里就不介绍了。大家可以去 Rust 的官网进行了解。

用 Cargo 创建项目

我们先准备一个目录用来放本次示例的代码。(我创建的目录叫做 go-rust )

然后使用 Rust 的 Cargo 工具创建一个名叫 rustdemo 的项目,这里由于我增加了 --lib 的选项,使用其内置的 library 模板。

➜  go-rust git:(master) ✗ mkdir lib && cd lib
➜  go-rust git:(master) ✗ cargo new --lib rustdemo
     Created library `rustdemo` package
➜  go-rust git:(master) ✗ tree rustdemo 
rustdemo
├── Cargo.toml
└── src
    └── lib.rs
1 directory, 2 files

准备 Rust 代码

extern crate libc;
use std::ffi::{CStr, CString};
#[no_mangle] 
pub extern "C" fn rustdemo(name: *const libc::c_char) -> *const libc::c_char {
    let cstr_name = unsafe { CStr::from_ptr(name) };
    let mut str_name = cstr_name.to_str().unwrap().to_string();
    println!("Rust get Input:  \"{}\"", str_name);
    let r_string: &str = " Rust say: Hello Go ";
    str_name.push_str(r_string);
    CString::new(str_name).unwrap().into_raw()
}

代码比较简单,Rust 暴露出来的函数名叫做 rustdemo ,接收一个外部的参数,并将其打印出来。之后从 Rust 这边再设置一个字符串。

CString::new(str_name).unwrap().into_raw() 被转换为原始指针,以便之后由 C 语言处理。

编译 Rust 代码

我们需要修改下 Cargo.toml 文件以便进行编译。注意,这里我们增加了 crate-type = ["cdylib"] 和 libc 。

[package]
name = "rustdemo"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
libc = "0.2"

然后进行编译

➜  rustdemo git:(master) ✗ cargo build --release
   Compiling rustdemo v0.1.0 (/home/tao/go/src/github.com/tao12345666333/go-rust/lib/rustdemo)
    Finished release [optimized] target(s) in 0.22s

查看生成的文件,这是一个 .so 文件(这是因为我在 Linux 环境下,你如果在其他系统环境下会不同)

➜  rustdemo git:(master) ✗ ls target/release/librustdemo.so 
target/release/librustdemo.so

准备 Go 代码

Go 环境的安装之类的这里也不再赘述了,继续在我们的 go-rust 目录操作即可。

编写 main.go

package main
/*
#cgo LDFLAGS: -L./lib -lrustdemo
#include <stdlib.h>
#include "./lib/rustdemo.h"
*/
import "C"
import (
	"fmt"
	"unsafe"
)
func main() {
	s := "Go say: Hello Rust"
	input := C.CString(s)
	defer C.free(unsafe.Pointer(input))
	o := C.rustdemo(input)
	output := C.GoString(o)
	fmt.Printf("%s\n", output)
}

在这里我们使用了 cgo ,在 import "C" 之前的注释内容是一种特殊的语法,这里是正常的 C 代码,其中需要声明使用到的头文件之类的。

下面的代码很简单,定义了一个字符串,传递给 rustdemo 函数,然后打印 C 处理后的字符串。

同时,为了能够让 Go 程序能正常调用 Rust 函数,这里我们还需要声明其头文件,在 lib/rustdemo.h 中写入如下内容:

char* rustdemo(char *name);

编译代码

在 Go 编译的时候,我们需要开启 CGO (默认都是开启的),同时需要链接到 Rust 构建出来的 rustdemo.so 文件,所以我们将该文件和它的头文件放到 lib 目录下。

➜  go-rust git:(master) ✗ cp lib/rustdemo/target/release/librustdemo.so lib

所以完整的目录结构就是:

➜  go-rust git:(master) ✗ tree -L 2 .
.
├── go.mod
├── lib
│   ├── librustdemo.so
│   ├── rustdemo
│   └── rustdemo.h
└── main.go
2 directories, 5 files

编译:

➜  go-rust git:(master) ✗ go build -o go-rust  -ldflags="-r ./lib" main.go
➜  go-rust git:(master) ✗ ./go-rust 
Rust get Input:  "Go say: Hello Rust"
Go say: Hello Rust Rust say: Hello Go

可以看到,第一行的输出是由 Go 传入了 Rust , 第二行中则是从 Rust 再传回 Go 的了。符合我们的预期。

总结

本篇介绍了如何使用 Go 与 Rust 进行结合,介绍了其前置关于 FFI 相关的知识,后续通过一个小的实践演示了其完整过程。 感兴趣的小伙伴可以自行实践下。

以上就是Go调用Rust方法及外部函数接口前置的详细内容,更多关于Go调用Rust外部函数接口前置的资料请关注三水点靠木其它相关文章!

Golang 相关文章推荐
Golang 正则匹配效率详解
Apr 25 Golang
go语言中切片与内存复制 memcpy 的实现操作
Apr 27 Golang
golang 比较浮点数的大小方式
May 02 Golang
Goland使用Go Modules创建/管理项目的操作
May 06 Golang
基于Golang 高并发问题的解决方案
May 08 Golang
Go中的条件语句Switch示例详解
Aug 23 Golang
golang操作rocketmq的示例代码
Apr 06 Golang
Golang原生rpc(rpc服务端源码解读)
Apr 07 Golang
Golang日志包的使用
Apr 20 Golang
Golang 入门 之url 包
May 04 Golang
Go本地测试解耦任务拆解及沟通详解Go本地测试的思路沟通的重要性总结
Jun 21 Golang
Go语言编译原理之源码调试
Aug 05 Golang
详解Go语言中配置文件使用与日志配置
Jun 01 #Golang
详解Go语言中Get/Post请求测试
Golang实现可重入锁的示例代码
May 25 #Golang
Go web入门Go pongo2模板引擎
May 20 #Golang
Go语言入门exec的基本使用
May 20 #Golang
Golang并发工具Singleflight
May 06 #Golang
深入理解 Golang 的字符串
May 04 #Golang
You might like
火车头discuz6.1 完美采集的php接口文件
2009/09/13 PHP
ThinkPHP独立分组使用的注意事项
2014/11/25 PHP
php实现根据IP地址获取其所在省市的方法
2015/04/30 PHP
thinkphp5.1框架模板布局与模板继承用法分析
2019/07/19 PHP
RR vs IO BO3 第一场2.13
2021/03/10 DOTA
ExtJS 2.0实用简明教程 之Ext类库简介
2009/04/29 Javascript
JavaScript 创建对象和构造类实现代码
2009/07/30 Javascript
jquery tools系列 expose 学习
2009/09/06 Javascript
instanceof和typeof运算符的区别详解
2014/01/06 Javascript
js实现的点击数量加一可操作数据库
2014/05/09 Javascript
jquery中change()用法实例分析
2015/02/06 Javascript
js实现同一页面多个不同运动效果的方法
2015/04/10 Javascript
JS动态给对象添加事件的简单方法
2016/07/19 Javascript
jQuery内存泄露解决办法
2016/12/13 Javascript
基于jQuery封装的分页组件
2017/06/26 jQuery
js+html5实现半透明遮罩层弹框效果
2020/08/24 Javascript
JS写谷歌浏览器chrome的外挂实例
2018/01/11 Javascript
Vue.js 动态为img的src赋值方法
2018/03/14 Javascript
angularjs 的数据绑定实现原理
2018/07/02 Javascript
[03:17]史诗级大片应援2018DOTA2国际邀请赛 致敬每一位坚守遗迹的勇士
2018/07/20 DOTA
Python中工作日类库Busines Holiday的介绍与使用
2017/07/06 Python
python数据封装json格式数据
2018/03/04 Python
python使用百度文字识别功能方法详解
2019/07/23 Python
python为Django项目上的每个应用程序创建不同的自定义404页面(最佳答案)
2020/03/09 Python
Django ForeignKey与数据库的FOREIGN KEY约束详解
2020/05/20 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
2021/01/15 Python
CSS3中Color的一些特性介绍
2012/05/27 HTML / CSS
css和css3弹性盒模型实现元素宽度(高度)自适应
2019/05/15 HTML / CSS
伊莱克斯(Electrolux)俄罗斯网上商店:瑞典家用电器品牌
2021/01/23 全球购物
学生安全教育材料
2014/02/14 职场文书
住宿生擅自离校检讨书
2014/09/22 职场文书
群众路线四风自我剖析材料
2014/10/08 职场文书
四风查摆问题自查报告
2014/10/10 职场文书
学习保证书100字
2015/02/26 职场文书
单位同意报考证明
2015/06/17 职场文书
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2021/08/30 Java/Android