gpt4 book ai didi

go - 如何从标准输出返回值?

转载 作者:行者123 更新时间:2023-12-01 22:34:51 24 4
gpt4 key购买 nike

我正在用其他语言编写REPL / shell。

我有以下代码,我希望打印2,但什么也没打印。

grepCmd := exec.Command("python")
grepIn, _ := grepCmd.StdinPipe()
grepOut, _ := grepCmd.StdoutPipe()
grepCmd.Start()
grepIn.Write([]byte("1+1")) <- assume this is fixed and we cannot use print().
//grepIn.Write([]byte("print(1+1)")) This one returns 2
grepIn.Close()
grepBytes, _ := ioutil.ReadAll(grepOut)
grepCmd.Wait()
fmt.Println(string(grepBytes)

我不是在问如何使用 cmd.stdout = os.stdout

最佳答案

当stdin连接到管道时,默认情况下python解释器将以非交互模式运行。因此,如果在shell中运行echo "1+1" | python,您将不会从stdout获得任何帮助。

python -i一样,执行echo "1+1" | python -i以显式启用交互模式。

关于go - 如何从标准输出返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com