我正在嘗試建立從 go 呼叫 rust 函數的可能性,然後所述 rust 函數將函數回調到 go。我使用 cgo 作為 go 和 rust 之間的 ffi 介面。 以下是我的 go 程式碼(src/main.go):
package main import ( "c" "fmt" "unsafe" ) /* #cgo cflags: -i./../lib #cgo ldflags: -l./../bin -lgo_move -wl,-rpath=./bin #include "move.h" */ //export cosmoscallbackwrapper func cosmoscallbackwrapper(data *c.uchar, datalen c.int) { // convert data to go slice godata := c.gobytes(unsafe.pointer(data), datalen) // call the actual callback function cosmoscallback(godata) } // setcosmoscallback sets the callback function to be called by the move vm. func setcosmoscallback(callback func([]byte)) { cosmoscallback = callback c.set_cosmos_callback((c.cosmos_callback)(unsafe.pointer(c.cosmoscallbackwrapper))) } var cosmoscallback func([]byte) func main() { // create a new move interpreter // set the callback function setcosmoscallback(func(data []byte) { fmt.println("received data from move vm:", data) // handle data and call cosmos sdk functions as needed }) }
這是我的 rust 程式碼 (src/lib.rs)
use std::os::raw::{c_char, c_int}; use std::ffi::cstring; use std::sync::mutex; #[macro_use] extern crate lazy_static; pub fn main() { } pub type cosmoscallback = extern "c" fn(*const c_char, c_int); lazy_static! { static ref callback: mutex<option<cosmoscallback>> = mutex::new(none); } #[no_mangle] pub extern "c" fn set_cosmos_callback(callback: cosmoscallback) { let mut cb = callback.lock().unwrap(); *cb = some(callback); } #[no_mangle] pub extern "c" fn cosmoscallbackwrapper(data: *const c_char, data_len: c_int) { let cb = callback.lock().unwrap(); if let some(callback) = &*cb { callback(data, data_len); } }
這是我的 cargo.toml 檔案:
[package] name = "go-move" version = "0.1.0" edition = "2021" [lib] crate-type = ["cdylib"] [dependencies] libc = "0.2" lazy_static = "1.4.0" [workspace] members = [ ".",]
這是 lib/move.h(共享庫的頭檔):
#ifndef move_vm_lib_h #define move_vm_lib_h #include <stdint.h> //typedef void (*cosmos_callback)(const unsigned char *data, int data_len); //void set_cosmos_callback(cosmos_callback callback); typedef void (*cosmos_callback)(const uint8_t* data, int32_t data_len); void set_cosmos_callback(cosmos_callback callback); #endif
這是我的 makefile:
shell := /bin/bash .phony: build os = $(shell uname) clean: rm -rf bin rm -rf target build: @echo the os is $(os) mkdir bin cargo build --release ifeq ($(os),linux) cp target/release/libgo_move.so bin/ else cp target/release/libgo_move.dylib bin/ endif cp -a lib/. bin/ go build --ldflags="-l./bin -lgo_move" -o bin/main src/main.go run: export ld_library_path=./bin && ./main
檔案結構如下:
src main.go main.rs bin libgo_move.so (after cargo build) lib move.h cargo.toml makefile
運行 make clean build
後,我得到以下輸出:
cp target/release/libgo_move.so bin/ cp -a lib/. bin/ go build -o bin/main src/main.go # command-line-arguments src/main.go:27:59: could not determine kind of name for C.cosmosCallbackWrapper src/main.go:27:25: could not determine kind of name for C.cosmos_callback src/main.go:27:2: could not determine kind of name for C.set_cosmos_callback
由於某種原因,它找不到 ffi 函數。
正確答案
這是一個愚蠢的錯誤:
/* #cgo CFLAGS: -I./../lib #cgo LDFLAGS: -L./../bin -lgo_move -Wl,-rpath=./bin #include "move.h" */
必須在導入之前進行。僅此而已。
以上是從 Rust 到 Go 的回呼函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tousethe"encoding/binary"packageinGoforencodinganddecodingbinarydata,followthesesteps:1)Importthepackageandcreateabuffer.2)Usebinary.Writetoencodedataintothebuffer,specifyingtheendianness.3)Usebinary.Readtodecodedatafromthebuffer,againspeci

encoding/binary包提供了統一的方式來處理二進制數據。 1)使用binary.Write和binary.Read函數可以編碼和解碼整數、浮點數等多種數據類型。 2)可以通過實現binary.ByteOrder接口來處理自定義類型。 3)需要注意字節序選擇、數據對齊和錯誤處理,以確保數據的正確性和高效性。

Go的strings包不適用於所有用例。它適用於大多數常見的字符串操作,但對於復雜的NLP任務、正則表達式匹配和特定格式解析,可能需要第三方庫。

Go語言中的strings包在處理大量字符串操作時存在性能和內存使用上的限制。 1)性能問題:如strings.Replace和strings.ReplaceAll在處理大規模字符串替換時效率較低。 2)內存使用:由於字符串不可變,每次操作會生成新對象,導致內存消耗增加。 3)Unicode處理:在處理複雜Unicode規則時不夠靈活,可能需要藉助其他包或庫。

掌握Go語言中的strings包可以提高文本處理能力和開發效率。 1)使用Contains函數檢查子字符串,2)用Index函數查找子字符串位置,3)Join函數高效拼接字符串切片,4)Replace函數替換子字符串。注意避免常見錯誤,如未檢查空字符串和大字符串操作性能問題。

你應該關心Go語言中的strings包,因為它能簡化字符串操作,使代碼更清晰高效。 1)使用strings.Join高效拼接字符串;2)用strings.Fields按空白符分割字符串;3)通過strings.Index和strings.LastIndex查找子串位置;4)用strings.ReplaceAll進行字符串替換;5)利用strings.Builder進行高效字符串拼接;6)始終驗證輸入以避免意外結果。

thestringspackageingoisesential forefficientstringManipulation.1)itoffersSimpleyetpoperfulfunctionsFortaskSlikeCheckingSslingSubstringsStringStringsStringsandStringsN.2)ithandhishiCodeDewell,withFunctionsLikestrings.fieldsfieldsfieldsfordsforeflikester.fieldsfordsforwhitespace-fieldsforwhitespace-separatedvalues.3)3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)