有一个 Golang 终端 shell 吗?编译语言是否可以实现这个功能?

huangapple go评论83阅读模式
英文:

Is there a Golang terminal shell? Is it possible for a compiled language?

问题

最近我对Golang很感兴趣。

当我学习Python时,我会启动一个终端窗口,然后尝试使用不同的数据和文件,进行一些琐碎的操作。这是一种很好的与新语言互动的方式,对于编写新程序非常有帮助。

我想知道,我猜因为Golang是一种像Java一样的编译语言,所以不可能有这样的终端窗口。是这样吗?如果是这样的话,真正的技术原因是什么导致它无法工作?

英文:

Recently I'm interested in Golang.

When I was learning Python I kicked off a terminal shell and just practised throwing it different data, files, making many trivial silly operations, it's such a nice way to interact with a new language and it's super helpful for writing new programs.

I wonder- I guess because Golang is a compiled language like Java it's not possible to have such a terminal shell. Is that right? If so- what's the real technical reason why it can't work?

答案1

得分: 4

请看一下gomacro

go get -u github.com/cosmos72/gomacro

~ $ gomacro
// GOMACRO,一个带有泛型和宏的交互式Go解释器
// 版权所有 (C) 2018-2019 Massimiliano Ghilardi <https://github.com/cosmos72/gomacro>
// 许可证 MPL v2.0+: Mozilla Public License version 2.0 or later <http://mozilla.org/MPL/2.0/>
// 这是一款没有任何保证的免费软件。
//
// 输入 :help 获取帮助
gomacro> import "fmt"
gomacro> a := 10
gomacro> fmt.Println(a)
10
3	// int
<nil>	// error
gomacro>
英文:

Take a look at gomacro

go get -u github.com/cosmos72/gomacro

~ $ gomacro
// GOMACRO, an interactive Go interpreter with generics and macros
// Copyright (C) 2018-2019 Massimiliano Ghilardi <https://github.com/cosmos72/gomacro>
// License MPL v2.0+: Mozilla Public License version 2.0 or later <http://mozilla.org/MPL/2.0/>
// This is free software with ABSOLUTELY NO WARRANTY.
//
// Type :help for help
gomacro> import "fmt"
gomacro> a := 10
gomacro> fmt.Println(a)
10
3	// int
<nil>	// error
gomacro>

答案2

得分: 1

当我刚开始使用Go语言时,我有同样的感觉,因为我之前使用Python已经有好几年了。但后来我决定,对于除了一些简单的编码(可以在Go Playground上完成)之外的任何事情,我最终都会编写一个脚本,而且添加一些样板代码也并不困难。

我认为关键在于Go的编译速度足够快,以至于大多数人并没有真正注意到重新编译和运行整个程序来查看更改的问题。

不过你说得对,Go是编译型语言。这与Java或Python有根本的不同,Java和Python都使用虚拟机来生成并执行代码。没有JVM或Python解释器,你无法运行Java程序或Python程序。而Go程序一旦编译完成,就可以直接分发,不需要任何依赖,这也是许多人喜欢用它进行部署的原因之一。

英文:

I felt the same way when I first started using golang having used Python for years. I have since decided that for anything beyond trivial coding (which can be done on the go playground) I end up writing a script for anyways and it's really not much harder to add in the boiler plate stuff.

The key I believe is that Go compiles fast enough that I think most people haven't really noticed an issue with just recompiling and running the entire program to see their changes.

You are right though, Go is compiled. This is fundamentally different than say Java or Python which both use Virtual Machines to generate code which it then executes. You can't run a Java program or Python program without the JVM or Python interpreter respectively. A go program on the other hand once compiled can be distributed directly with no dependancies, one reason that many people love it for deployment.

答案3

得分: 1

不是直接的解决方案,但可以尝试使用一个带有Go内核的Jupyter笔记本,例如https://github.com/gopherdata/gophernotes。

虽然你将使用Jupyter浏览器界面而不是终端,但你仍然可以执行类似终端的操作,比如执行shell命令和使用Tab键自动补全。当我想要导入一个我不熟悉的库并快速尝试它的函数时,我发现这很有用,例如

英文:

Not a direct solution, but one thing to try is a Jupyter notebook with a Go kernel like https://github.com/gopherdata/gophernotes

While you'll be using the Jupyter browser interface instead of the terminal, you can still do terminal-like things like execute shell commands and tab-complete. I've found this useful when wanting to import a library I'm not familiar with and quickly trying its functions, for example.

huangapple
  • 本文由 发表于 2017年6月1日 19:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/44306015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定