登录
首页 >  Golang >  Go问答

如何在控制台输出中检索值?

来源:stackoverflow

时间:2024-03-08 11:51:24 312浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《如何在控制台输出中检索值?》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

我正在为其他语言构建 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


解决方案


当标准输入连接到管道时,Python解释器默认以非交互模式运行。因此,如果您运行 echo "1+1" | ,您将不会从标准输出获得任何信息。 shell 中的 python

执行 python -i 显式启用交互模式,如 echo "1+1" | python -i

今天关于《如何在控制台输出中检索值?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>