从Go调用Python函数并获取函数返回值

布莱斯·托马斯

我正在编写Go程序。从这个Go程序中,我想调用另一个文件中定义的Python函数并接收该函数的返回值,以便可以在Go程序的后续处理中使用它。我在Go程序中无法获取任何返回的数据。以下是我认为可行的最低示例,但显然无效:

gofile.go

package main

import "os/exec"
import "fmt"

func main() {
     fmt.Println("here we go...")
     program := "python"
     arg0 := "-c"
     arg1 := fmt.Sprintf("'import pythonfile; print pythonfile.cat_strings(\"%s\", \"%s\")'", "foo", "bar")
     cmd := exec.Command(program, arg0, arg1)
     fmt.Println("command args:", cmd.Args)
     out, err := cmd.CombinedOutput()
     if err != nil {
         fmt.Println("Concatenation failed with error:", err.Error())
     return
     }
     fmt.Println("concatentation length: ", len(out))
     fmt.Println("concatenation: ", string(out))
     fmt.Println("...done")
}

pythonfile.py

def cat_strings(a, b):
    return a + b

如果我打电话,go run gofile我得到以下输出:

here we go...
command args: [python -c 'import pythonfile; print pythonfile.cat_strings("foo", "bar")']
concatentation length:  0
concatenation:  
...done

一些注意事项:

  • -c在Python调用中使用了该标志,因此可以cat_strings直接调用该函数假定cat_strings是Python文件的一部分,该文件包含其他Python程序使用的实用程序功能,因此为什么我没有任何if __name__ == __main__生意。
  • 我不想将Python文件修改为print a + b(而不是return a + b);请参见关于该函数属于一组实用程序函数的一部分的先前知识,该实用函数应该由其他Python代码调用。
  • cat_strings功能是虚构的,用于演示目的;真正的功能是我不想简单地在Go中重新实现的功能。我真的对如何从Go调用Python函数并获取返回值感兴趣。

通过删除命令本身周围的引号,我设法获得了一些有效的代码:

package main

import "fmt"
import "os/exec"

func main() {
    cmd := exec.Command("python",  "-c", "import pythonfile; print pythonfile.cat_strings('foo', 'bar')")
    fmt.Println(cmd.Args)
    out, err := cmd.CombinedOutput()
    if err != nil { fmt.Println(err); }
    fmt.Println(string(out))
}

可以肯定的是,在源代码中,您具有此功能(至少对于Windows,我不知道该功能是否适用于其他操作系统):

// EscapeArg rewrites command line argument s as prescribed
// in http://msdn.microsoft.com/en-us/library/ms880421.
// This function returns "" (2 double quotes) if s is empty.
// Alternatively, these transformations are done:
// - every back slash (\) is doubled, but only if immediately
//   followed by double quote (");
// - every double quote (") is escaped by back slash (\);
// - finally, s is wrapped with double quotes (arg -> "arg"),
//   but only if there is space or tab inside s.
func EscapeArg(s string) string { ...

因此,您的代码最终将通过以下命令行调用:

$ python -c "'import pythonfile; print pythonfile.cat_strings(\\"foo\\", \\"bar\\")'"

如果经过测试,则结果为字符串,不返回任何内容,因此输出长度为0。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

调用javascript函数并使用匿名函数获取返回值

来自分类Dev

获取函数返回值

来自分类Dev

从Go中的函数获取第n个返回值

来自分类Dev

Java调试模式:如何获取函数调用的返回值

来自分类Dev

如何获取递归python函数以返回值?

来自分类Dev

如何获取python函数的所有可能的返回值?

来自分类Dev

从html调用的setInterval()函数返回值

来自分类Dev

从运行中获取函数的返回值

来自分类Dev

AngularJs-获取函数的返回值

来自分类Dev

jQuery:获取函数的返回值

来自分类Dev

Javascript获取内部函数的返回值

来自分类Dev

如何从连接函数获取返回值

来自分类Dev

如何从存根函数获取返回值?

来自分类Dev

获取异步函数参数的返回值

来自分类Dev

从函数返回值获取指针的地址

来自分类Dev

获取reactmongo findAndUpdate函数的返回值

来自分类Dev

如何获取函数javascript的返回值

来自分类Dev

如何调用javascript函数并从javascript函数获取返回值

来自分类Dev

函数的返回值

来自分类Dev

函数返回值

来自分类Dev

使用switch语句从函数返回值以调用函数

来自分类Dev

从具有多个返回值的函数调用中仅获取特定值?

来自分类Dev

如何获得在另一个函数中调用的函数以返回值?PYTHON

来自分类Dev

当函数本身在PYTHON内部调用时从函数返回值

来自分类Dev

无法从Lua函数获取C函数返回值

来自分类Dev

Python-从函数返回值

来自分类Dev

在python中打印函数的返回值?

来自分类Dev

从python中的修饰函数返回值

来自分类Dev

Python-返回值的函数