Does Go provide a REPL?
Asked Answered
M

14

276

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

Merline answered 15/12, 2011 at 0:21 Comment(0)
L
212

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.

Levee answered 15/12, 2011 at 8:53 Comment(4)
TL;DR igo Others: Gore isn't very convenient as it requires pasting and Ctrl-D. Something like Reinteract for Go would be awesomesauce.Cherycherye
There seem to be two REPL projects called "gore". This one is more actively maintained than the one in the answer: github.com/motemen/gorePiracy
Ha, I was just about to post the same thing. I've just been playing with motemen/gore there - it's pretty good.Esteresterase
github.com/motemen/gore gore is pretty good and simple to use.Tasimeter
P
66

Try motemen/gore

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

https://github.com/motemen/gore

enter image description here

Piracy answered 22/2, 2015 at 22:53 Comment(2)
What is the program you are using to display your inputs? I love thatBethannbethanne
@Bethannbethanne it's in the repo's readme. github.com/cho45/KeyCast MacOS onlyRedwine
P
11

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).

Piggyback answered 26/3, 2013 at 12:29 Comment(0)
M
8

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.

enter image description here

Mongolism answered 9/1, 2015 at 15:15 Comment(2)
how can make the GoRun output in the current buffer。my vim, display that in the shellPentha
vim-go all the way! However, to take this a step further I would remove the boiler plate code, put my 'package_test.go' in the split buffer & keep hammering :GoTest & :GoCoverage. My 2 cents...Roundshouldered
M
6

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.

Miquelon answered 15/12, 2011 at 1:48 Comment(0)
A
5

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

Argilliferous answered 23/8, 2013 at 16:58 Comment(0)
H
4

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.

Helices answered 16/3, 2013 at 14:51 Comment(0)
P
4

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

https://github.com/mkouhei/gosh

Peralta answered 31/8, 2015 at 2:41 Comment(0)
W
3

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. Debugging go file in VSCode to introspect through debugger

Wellinformed answered 23/10, 2019 at 1:20 Comment(0)
D
1

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

Divers answered 20/6, 2018 at 15:48 Comment(0)
V
1

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

Victualage answered 10/8, 2018 at 8:36 Comment(1)
Dead link dead link.Undersized
I
0

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 running in Visual Studio Code

Instantaneous answered 12/1, 2020 at 1:40 Comment(0)
T
0

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.

Trey answered 14/4, 2023 at 20:52 Comment(0)
M
0

Try https://github.com/cosmos72/gomacro. Can be used as an alternative to bash scripts.

There's also https://github.com/d4l3k/go-pry, slightly different workflow.

I'll update this once I have a comparison with https://github.com/x-motemen/gore.

Miyamoto answered 3/12, 2023 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.