- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我们使用第三方 Tcl 解析库来验证 Tcl 脚本的语法和语义检查。驱动程序是用 C 语言编写的,并定义了一组实用函数。然后它调用 Tcl_CreateObjCommand
因此脚本可以调用这些 C 函数。现在我们正在将主程序移植到 go 中,但我找不到执行此操作的方法。有人知道从 Tcl 脚本调用 golang 函数的方法吗?
static int
create_utility_tcl_cmds(Tcl_Interp* interp)
{
if (Tcl_CreateObjCommand(interp, "ip_v4_address",
ip_address, (ClientData)AF_INET, NULL) == NULL) {
TCL_CHECKER_TCL_CMD_EVENT(0, "ip_v4_address");
return -1;
}
.....
return 0;
}
最佳答案
假设您已将相关函数设置为已导出并构建项目的 Go 部分
[…]
The important things to note are:
- The package needs to be called
main
- You need to have a
main
function, although it can be empty.- You need to import the package
C
- You need special
//export
comments to mark the functions you want callable from C.I can compile it as a C callable static library with the following command:
go build -buildmode=c-archive foo.go
那么剩下要做的核心工作就是将 Tcl 的 API 中的 C 粘合函数写入您的 Go 代码。这将涉及类似以下的功能:
static int ip_address_glue(
ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const *objv) {
// Need an explicit cast; ClientData is really void*
GoInt address_family = (GoInt) clientData;
// Check for the right number of arguments
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "address");
return TCL_ERROR;
}
// Convert the argument to a Go string
GoString address;
int len;
address.p = Tcl_GetStringFromObj(objv[1], &len);
address.n = len; // This bit is hiding a type mismatch
// Do the call; I assume your Go function is called ip_address
ip_address(address_family, address);
// Assume the Go code doesn't fail, so no need to map the failure back to Tcl
return TCL_OK;
}
(感谢 https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf 为我提供了足够的信息来计算一些类型绑定(bind)。)
这就是您向 Tcl 注册为回调的函数。
Tcl_CreateObjCommand(interp, "ip_v4_address", ip_address_glue, (ClientData)AF_INET, NULL);
理论上,命令注册可能会失败。实际上,只有在删除 Tcl 解释器(或其中的一些关键 namespace )时才会发生这种情况。
如果在 Go 级别将故障编码为枚举,则将故障映射到 Tcl 将是最简单的。可能最容易将成功表示为零。有了它,您就可以:
GoInt failure_code = ip_address(address_family, address);
switch (failure_code) {
case 0: // Success
return TCL_OK;
case 1: // First type of failure
Tcl_SetResult(interp, "failure of type #1", TCL_STATIC);
return TCL_ERROR;
// ... etc for each expected case ...
default: // Should be unreachable, yes?
Tcl_SetObjResult(interp, Tcl_ObjPrintf("unexpected failure: %d", failure_code));
return TCL_ERROR;
}
用值元组传回更复杂的返回类型(尤其是成功指示器和“真实”结果值的组合)也应该是可能的,但我没有 Go 开发环境来探究它们是如何实现的'映射到 C 级。
关于go - 从 Tcl 脚本调用 golang 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56385020/
我是 TCL 新手,正在努力访问其他模块。 SOURCE 命令帮助我包含了我在 TCL 程序中编写的其他代码。但是,我认为我在访问代码库时遇到了问题。 例如,当我引用一个数学函数时,找不到它。我认为在
我有两个 tcl 脚本。我想在第一个脚本完成后运行第二个脚本。我该怎么做? 最佳答案 取决于你的真正意思。 一种方法是编写第三个(“主”)脚本 source /the/path/to/the/firs
相比之下,使用 TCL C API 读取文件和填充 TCL 数组会快得多吗?对标准 TCL 做同样的事情。我有一个大约 100+MB 的大文件,我需要读取它并设置一些哈希条目。使用 TCL C API
相比之下,使用 TCL C API 读取文件和填充 TCL 数组会快得多吗?对标准 TCL 做同样的事情。我有一个大约 100+MB 的大文件,我需要读取它并设置一些哈希条目。使用 TCL C API
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我有以下基本代码: proc test {} { set my_var2 3 foreach iter {1 2 3} { set my_var1 4
例如,在 Perl 中,要获取从 1 到 10 的连续数字数组,您可以简单地执行以下操作: @myArray = (1 .. 10); 这两个句点用作此操作的简写,而不是制作 for 循环或手动写出整
我有一个列表,我正在尝试修改它并根据我想要实现的目标制作一个新列表。 原始列表 $> set a {123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno}
在TCL中是否可以将参数的默认值作为函数调用的返回值? proc GetParameterValue { } { # calculation for value... return v
有些东西在说谎...... 请记住,当我手动运行脚本时,这会按预期工作,但当它通过任务调度程序运行时则不会。 我有一个 TCL 脚本,用于检查网络驱动器上是否存在文件,如果存在则将其删除。我通过以下方
如果不是,它是什么? 我读到的关于 TCL 的所有内容都指出,所有内容都只是其中的一个字符串。解释器内部可以有一些其他类型和结构(为了性能),但在 TCL 语言级别,一切都必须表现得像一个字符串。还是
我已经开发了一些代码,但我在 Linux 机器上遇到了 Tcl 解释器的错误标记问题。 #!/usr/bin/tclsh if {1} { puts "abc1" } elseif {} {
我需要一些帮助来定义数组以及在 TCL 中显示和循环它们。 这是我将如何在 php 中执行它们。 $date =array(); $size=0; $date[$size] =$pre_event_d
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
谁能解释一下init.tcl有什么用? TCL 解释器实际上何时加载它? 该文件的描述如下 startup file for TCL . 但据我所知.tclshrc是启动文件。 谁能解释一下吗? 最佳
我试图通过在函数外部声明它来使用全局变量 (gpio_out_set_3)(因为该变量将来也可能在其他函数中使用)。在函数内部,我已将相同的变量声明为“global”并尝试通过“$gpio_out_s
我在 TCL 中有代码: set a 1 set b 0 set c "Start" if { $a == 1 && ($b == 1 || $c == "Start") } { puts W
我正在实习,实习要求我学习和练习 TCL - OO,所以我一直在寻找有关 TCL - OO 的教程、示例和书籍,但我找不到任何东西,所以如果有人可以的话,我将非常感激给我一些关于 TCL - OO 的
我试图通过在函数外部声明它来使用全局变量 (gpio_out_set_3)(因为该变量将来也可能在其他函数中使用)。在函数内部,我已将相同的变量声明为“global”并尝试通过“$gpio_out_s
如何通过键盘向 Tcl 脚本提供输入? C 中有类似 scanf() 的东西吗? 最佳答案 gets命令可能就是您想要的。 set data [gets stdin] # or set numchar
我是一名优秀的程序员,十分优秀!