-
Add dependency and build-dependency to
Cargo.toml
:[dependencies] monoio-rust2go = { version = "0.3.0" } [build-dependencies] monoio-rust2go = { version = "0.3.0", features = ["build"] }
And also install commandline tool:
cargo install --force monoio-rust2go-cli
-
Create an empty
user.rs
and add it tomain.rs
:mod user;
. -
Define request and response structs, and calling conventions in
user.rs
. You should also add#[derive(monoio_rust2go::R2G)]
to your structs.#[derive(monoio_rust2go::R2G)] pub struct DemoRequest { pub name: String, pub age: u8, } #[derive(monoio_rust2go::R2G)] pub struct DemoResponse { pub pass: bool, } pub trait DemoCall { fn demo_check(req: DemoRequest) -> DemoResponse; fn demo_check_async(req: DemoRequest) -> impl std::future::Future<Output = DemoResponse>; }
-
Create an empty go project and initialize it:
mkdir go && cd go && go mod init r2gexample
; or you can use any existed project. -
Generate golang code with
monoio-rust2go-cli --src src/user.rs --dst go/gen.go
. -
Write a
build.rs
with the following content:fn main() { monoio_rust2go::Builder::new().with_go_src("./go").build(); }
-
Add an include to the top of
user.rs
to make sure the generated code is used:pub mod binding { monoio_rust2go::r2g_include_binding!(); }
Also add macro
#[monoio_rust2go::r2g]
to your trait:#[monoio_rust2go::r2g] pub trait DemoCall { fn demo_oneway(req: &DemoUser); fn demo_check(req: &DemoComplicatedRequest) -> DemoResponse; fn demo_check_async( req: &DemoComplicatedRequest, ) -> impl std::future::Future<Output = DemoResponse>; }
-
Call the golang with
user::{$trait}Impl
.fn main() { let req = DemoRequest { name: "ihciah".to_string(), age: 28, }; println!("User pass: {}", DemoCallImpl::demo_check(req).pass); }
You can also run a async call with
DemoCallImpl::demo_check_async(req).await
. -
Run it and it will show
User pass: false
! Then you can edit the golang code ingo/gen.go
and customize golang side logic.
example
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||