I just finished a little tool called "gosnip" that allows you to run little
snippets of Go code from the command line:

https://github.com/benhoyt/gosnip

To use it, just type something like:

    $ gosnip 'fmt.Println("Hello world")'
    Hello world

gosnip automatically adds (standard library) imports, rolls into into a
complete program, and uses "go run" to run it.

To quote the "Why?" section in the README: I made gosnip because when
coding in Go I often want to try little snippets of code to see what they
do, for example, "how does format string %6.3f work again?" I could use the
Go playground, but it's nice to be able to use a one-line command. Also, I
often develop while offline on my bus commute, so don't have access to the
online Go playground (yes, I know it's possible to run the Go playground
locally).

It was very handy to have go/parser available in the standard library, and
even nicer that it automatically provides the list of unresolved names --
which I use to know what to import.

"go run" isn't particularly fast for this use case, as it spawns the go
compiler, linker, and then the program itself. Seems to take about 250ms on
my macOS machine (and it's probably slower on Windows, as os/exec is
somewhat slower on Windows).

If anyone knows a better way to run Go source, let me know. As much as I
like writing interpreters, it'd be a big job to write a Go compiler just
for this. In the meantime, 250ms will have to do.

Feedback welcome!

-Ben

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to