Does Go provide REPL?
GoRead Eval-Print-LoopGo Problem Overview
The interactive environment is VERY helpful for a programmer. However, it seems Go does not provide it. Is my understanding correct?
Go Solutions
Solution 1 - Go
No, Go does not provide a REPL(read–eval–print loop).
However, as already mentioned, Go Playground (this is the new URL) 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.
Solution 2 - Go
Try motemen/gore
> Yet another Go REPL that works nicely. Featured with line editing, > code completion, and more.
Solution 3 - Go
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).
Solution 4 - 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.
Solution 5 - 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.
Solution 6 - Go
The GoSpeccy project includes a builtin REPL of a restricted subset of the Go language. The implementation is using goeval.
Solution 7 - Go
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.
Solution 8 - Go
Gosh is the interactive Golang shell. The goal is to provide an easy-to-use interactive execution environment.
Solution 9 - Go
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.
Solution 10 - Go
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
Solution 11 - Go
Please also check www.gorepl.com for go REPL and other REPLs
Solution 12 - Go
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.