Go提供REPL吗?

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

Does Go provide REPL?

问题

交互式环境对程序员非常有帮助。然而,似乎Go语言并没有提供这个功能。我的理解正确吗?

英文:

The interactive environment is VERY helpful for a programmer. However, it seems Go does not provide it. Is my understanding correct?

答案1

得分: 208

不,Go语言没有提供REPL(读取-求值-输出循环)。

然而,正如已经提到的,Go Playground非常方便。Go的作者们也在考虑为其添加一个功能丰富的编辑器。

如果你想要本地的解决方案,可以考虑安装hsandbox。只需在终端中运行hsandbox go,它将使用screen将你的终端屏幕分割成两部分,你可以在顶部编写代码,并在每次保存时在底部看到执行结果。

在标准的Go命令中曾经有一个gotry,它用于评估表达式(可选的包名),可以像这样从shell中运行gotry 1+2gotry fmt 'Println("hello")'。由于实际使用它的人不多,它已经不再可用。

我也看到过一些第三方项目用于构建Go的REPL,但现在我只能找到两个链接:igogo-repl。它们的工作效果如何我不清楚。

我的建议是:Go的编译速度使得编写REPL成为可能,正如它也帮助构建了这里提到的工具,但是同样的速度使得REPL变得不那么必要。每当我想要在Go中测试一些无法在Playground中运行的东西时,我会打开一个简单的.go文件并开始编码,然后直接运行代码。当Go 1中的go命令使得一键构建过程变得可能且更加简单时,这将变得更加容易。

更新: Go的最新每周版本添加了go命令,可以用于非常容易地构建一个文件:编写你的prog.go文件,然后运行go build prog.go && ./prog

更新2: 使用Go 1,你可以直接运行go程序,命令为go run filename.go

更新3: gore是一个看起来很有趣的新项目。

英文:

No, Go does not provide a REPL(read–eval–print loop).

However, as already mentioned, Go Playground is very handy. The Go Authors are also thinking about adding a feature-rich editor to it.

If you want something local, consider installing hsandbox. Running it simply with hsandbox go will split your terminal screen (with screen) where you can write code at the top and see its execution output at the bottom on every save.

There was a gotry among standard Go commands, which used to evaluate expressions (with an optional package name), and could be run like gotry 1+2 and gotry fmt 'Println("hello")' from shell. It is no longer available because not many people actually used it.

I have also seen third party projects for building a REPL for Go, but now I can only find links to two of them: igo and go-repl. How well do they work I don't know.

My two cents: Speed of compilation makes writing a REPL possible for Go, as it has also helped building the tools mentioned here, but the same speed makes REPL less necessary. Every time I want to test something in Go that I can't run in Playground I open a simple .go file and start coding and simply run the code. This will be even easier when the go command in Go 1 makes one-command build process possible and way easier.

UPDATE: Latest weekly release of Go added go command which can be used to very easily build a file: write your prog.go file and run go build prog.go && ./prog

UPDATE 2: With Go 1 you can directly run go programs with go run filename.go

UPDATE 3: gore is a new project which seems interesting.

答案2

得分: 66

尝试 motemen/gore

> 又一个很好用的 Go REPL。具备行编辑、代码补全等功能。

https://github.com/motemen/gore

Go提供REPL吗?

英文:

Try motemen/gore

> Yet another Go REPL that works nicely. Featured with line editing,
> code completion, and more.

https://github.com/motemen/gore

Go提供REPL吗?

答案3

得分: 11

你还有一个最近的项目(2013年3月)叫做**gore**,由Sriram Srinivasan开发,可能会很有用:

> gore是一个用于评估golang代码的命令行工具 - 一个没有循环的REPL。
它是go playground的替代品,同时使得交互式尝试代码片段变得更加容易:gore会自动提供样板代码,如导入和包声明以及一个main函数的包装器
此外,由于它在你自己的计算机上运行,不会因为安全原因拒绝任何代码(不像go playground的安全沙盒模式)。

英文:

You also have a recent (March 2013) project called gore from Sriram Srinivasan, which can be useful:

> gore is a command-line evaluator for golang code -- a REPL without a loop, if you will.
It is a replacement for the go playground, while making it much easier to interactively try out bits of code: gore automatically supplies boiler-plate code such as import and package declarations and a main function wrapper.
Also, since it runs on your own computer, no code is rejected on security grounds (unlike go playground's safe sandbox mode).

答案4

得分: 8

如果你是Vim用户,vim-go插件(https://github.com/fatih/vim-go)提供了一个命令(GoRun)来运行并打印当前缓冲区的输出。你仍然需要包含一个主Go文件的所有样板代码,但它仍然提供了一种方便的方式来快速测试本地环境中的代码片段。

英文:

If you're a Vim user, the vim-go plugin (https://github.com/fatih/vim-go) provides a command (GoRun) to run and print the output of the current buffer. You still have to include all the boilerplate code of a main Go file, but it still provides a convenient way to quickly test code snippets in your local environment.

Go提供REPL吗?

答案5

得分: 6

你尝试过Go Playground吗?

> 关于Go Playground
>
> Go Playground是一个运行在golang.org服务器上的网络服务。
> 该服务接收一个Go程序,编译、链接并在一个沙盒中运行该程序,然后返回输出结果。

英文:

Have you tried the Go Playground?

> About the Go Playground
>
> The Go Playground is a web service that runs on golang.org's servers.
> The service receives a Go program, compiles, links, and runs the
> program inside a sandbox, then returns the output.

答案6

得分: 5

GoSpeccy 项目包含了一个内置的 REPL,用于限制性的 Go 语言子集。实现使用了 goeval

英文:

The GoSpeccy project includes a builtin REPL of a restricted subset of the Go language. The implementation is using goeval.

答案7

得分: 4

不过,你可以利用编译速度(如其他答案中提到的)。

可以看看rango,它使用生成-编译-运行循环来模拟REPL。你也可以通过导入和语句来开始一个交互式会话。

英文:

No, but you can exploit the speed of compilation (as mentioned in other answers).

Have a look at rango that uses a generate-compile-run loop to mimic a REPL. You can also start it with imports and statements to begin an interactive session.

答案8

得分: 4

Gosh 是一个交互式的 Golang shell。目标是提供一个易于使用的交互式执行环境。

https://github.com/mkouhei/gosh

英文:

Gosh is the interactive Golang shell. The goal is to provide an easy-to-use interactive execution environment.

https://github.com/mkouhei/gosh

答案9

得分: 3

我在VSCode调试器上有一些运气,但它在某种程度上相当有限,因为您无法从调试控制台调用函数调用Debug: Function Calls not supported #2225

基本上,您需要在正确配置launch.json文件后设置断点。然后,您可以在变量侧边栏中向下钻取,并在调试控制台中输入变量表达式。
Go提供REPL吗?

英文:

I've had some luck with the VSCode debugger, but it's fairly limited in so far as you cannot invoke function calls from the debug console Debug: Function Calls not supported #2225.

Basically you set a breakpoint after properly configuring your launch.json file. Then you can drill down on the left in the variables side bar and enter variable expressions an the debug console.
Go提供REPL吗?

答案10

得分: 1

你可能也想尝试一下https://github.com/haya14busa/goplay
这可以让你直接从终端运行go代码文件到Go Playground。

英文:

You may also like to try https://github.com/haya14busa/goplay
This enables you to run go code files from your terminal directly to the Go Playground

答案11

得分: 1

请同时查看www.gorepl.com以获取go REPL和其他REPLs。

英文:

Please also check www.gorepl.com for go REPL and other REPLs

答案12

得分: 0

使用Go扩展和Code Runner扩展,可以在Visual Studio Code中以类似REPL的方式运行Go代码。点击下面截图中由鼠标光标标记的运行三角形▶,即可运行代码并在Visual Studio Code底部的输出窗格中显示结果。

在使用Go进行编程时,Visual Studio Code会建议安装其他Go扩展来扩展其功能。

Go提供REPL吗?

英文:

Go code can be run in a REPL-like way in Visual Studio Code with the Go extension and Code Runner extension. Click the Run triangle ▶ which is marked by the mouse cursor in the below screenshot to run the code and show the results in the Output pane at the bottom of Visual Studio Code.

When programming with Go Visual Studio Code will suggest additional Go extensions that can be installed to extend Visual Studio Code's functionality.

Go提供REPL吗?

答案13

得分: 0

这是一个用Bash编写的16行Go可脚本化REPL,适用于Linux:

这不会打开一个REPL,但你可以像使用pythonpython3nodedenobash等一样使用它作为可脚本化的REPL /运行时。

将以下内容保存为/usr/bin/gorun并使用以下命令赋予可执行权限:

sudo chmod ugo+x /usr/bin/gorun

你可以扩展gorun以在shebang行中使用参数。如果你传递了参数,它们将在$GORUN_ARGS中。

如果你使用#! /usr/bin/env gorun作为shebang行,它会工作,但是在这个shebang行中传递的任何参数都会传递给env而不是gorun,这可能不是你想要的,除非你根本不关心将参数传递给gorun

你的Go脚本,带有shebang行:

#! /usr/bin/gorun

package main

import "fmt"
import "os"
import "strings"

func main() {
	fmt.Println(`Hello World`)
    fmt.Println(strings.Join(os.Args[1:], ","))
}

将其保存为index.go,例如。此脚本的内容仅用于演示通过CLI解析传递给它的参数。

通过CLI运行你的Go脚本(选项1):

./index.go arg1 arg2 arg3

通过CLI运行你的Go脚本(选项2):

gorun index.go arg1 arg2 arg3

在构建之前,你需要删除shebang行。也许可以编写另一个名为gobuild的CLI,它与gorun执行相同的操作,只是将go run替换为go build

老实说,我不知道为什么Go没有内置的REPL,或者为什么默认情况下不像其他运行时一样支持shebang行。

我猜他们真的不希望你编写Go脚本,因为它被设计为一种编译语言。好吧,这改变了这一点,哈哈。

顺便说一句,你可以对其他编译语言做类似的事情,只是Go是一个很好的选择,因为go run默认情况下相当快,这是由于对语言格式施加的限制。

如果你不知道,你可以使用其他运行时(如nodepython等)以相同的方式编写脚本,并按照上述两种方式执行它们,只要你相应地更改shebang行。这些运行时默认支持shebang行。例如,你可以在NodeJS脚本中使用#! /usr/bin/env node,并使用./index.jsnode index.js运行它。它能完美运行。

英文:

16-line Go Scriptable REPL written in Bash, for Linux:

This will NOT open a REPL, but you can use it as a Scriptable REPL / Runtime, like you can with python, python3, node, deno, bash, etc.

#! /usr/bin/env bash

if [[ $(cat $1 2> /dev/null) == "" ]]; then
  SCRIPT_P_FNAME=$2
  GORUN_ARGS=$1
  shift 2
else
  SCRIPT_P_FNAME=$1
  shift 
fi

cat ${SCRIPT_P_FNAME} | sed '/^#!/d' > wubbalubbadubdub.go

go run wubbalubbadubdub.go $@

rm wubbalubbadubdub.go

Save this as /usr/bin/gorun and give it executable permissions using:

sudo chmod ugo+x /usr/bin/gorun

You can extend gorun to take arguments in the shebang-line with the above configuration. It's arguments will be in $GORUN_ARGS if you passed them.

If you use a shebang-line of #! /usr/bin/env gorun it will work, but any arugments passed in this shebag-line will go to env instead of gorun, which is probably not what you want, unless you don't care to pass arguments to gorun at all.

Your Go Script, with a Shebang-line:

#! /usr/bin/gorun

package main

import "fmt"
import "os"
import "strings"

func main() {
	fmt.Println(`Hello World`)
    fmt.Println(strings.Join(os.Args[1:], ","))
}

Save this as index.go, for example. The content of this script is just demonstrative of being able to parse arguments passed to it via the CLI.

Run your Go Script via the CLI (Option 1):

./index.go arg1 arg2 arg3

Run your Go Script via the CLI (Option 2):

gorun index.go arg1 arg2 arg3

Before you build it, you'll have to remove the shebang-line. Maybe write another CLI like this called gobuild that does the same thing as gorun, except replacing go run with go build.

Honestly, I don't know why Go doesn't have a built-in REPL, or doesn't respect the shebang-line by default like all other runtimes.

I guess they really don't want you to write Go scripts, as it is intended to be a compiled language. Well, this changes that, lol.

Btw, you can do something similar for other compiled languges, it's just that Go is a good contender for this since go run is pretty quick by default due to the constraints placed on the language format.

If you didn't know, you can write scripts in the same manner as shown above with other runtimes such as node, python etc. and execute them in the same 2 ways as shown above as long as you change up the shebang-line accordingly. These runtimes respect the shebang-line by default. For example, you can use #! /usr/bin/env node in a NodeJS script and run it with either ./index.js or node index.js. It works like a charm.

huangapple
  • 本文由 发表于 2011年12月15日 08:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/8513609.html
匿名

发表评论

匿名网友

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

确定