A command-line runner for Go expressions

grun

grun is a command-line tool for evaluating Go expressions. It was inspired by runner which does the same for Rust.

It constructs a little Go program printing out the expression and runs it using go run. This is normally fast but the first invocation may take a little while since it runs go list all first to make a package map.

$ grun -e 'strings.Split("hello world"," ")'
[hello world]

You can choose to display the results in JSON, which is good for presenting data structures like slices and maps neatly, although not so useful for types that don't have public fields.

$ grun -j -e 'strings.Split("hello world"," ")'
[
  "hello",
  "world"
]

Go can be a bit verbose so there are some shortcuts

  • S.: strings.
  • M.: math.
  • C.: strconv.
  • R(rx): regexp.MustCompile(rx)
$ grun -e 'R(`[a-z]+`).MatchString("hello")'
true
$ grun -e 'M.Sin(1.2)'
0.9320390859672264

In addition, these type aliases are defined:

type M = map[string]interface{}
type S = []interface{}

So we can say:

grun$ grun -e 'M{"one":1,"two":"zwei","three":S{1,2,3}}'
map[one:1 three:[1 2 3] two:zwei]

Expressions may of course return multiple results, commonly for error returns:

$ grun -e 'os.Open("nada.txt")'
<nil>
open nada.txt: no such file or directory
$ grun -j -e 'os.Open("nada.txt")'
null
 {
  "Op": "open",
  "Path": "nada.txt",
  "Err": 2
}

There is another option, which is to print the result(s) using the '%#v' format, as Go expressions:

$ grun -G -e 'os.Open("nada.txt")'
(*os.File)(nil)
&fs.PathError{Op:"open", Path:"nada.txt", Err:0x2}

We resolve nested packages like io/ioutil using the package map:

$ cat tmp.txt
hello dolly
$ grun -e 'ioutil.ReadFile("tmp.txt")'
[104 101 108 108 111 32 100 111 108 108 121 10]
 <nil>

(These are not always unique but it's good enough for now)

There are limits to what a single Go expresion can do, so the expression may be preceded by one or more statements:

$ grun -e 's,_ := ioutil.ReadFile("tmp.txt"); string(s)'
hello dolly

Regular error handling is a little clumsy for one-liners, so there is an implicit shortcut. If there's a second var assigned, and it starts with 'e', then we put in a log.Fatal check:

$ grun -e 's,e := ioutil.ReadFile("nada.txt"); string(s)'
2021/04/21 16:03:07 open nada.txt: no such file or directory
exit status 1
2021/04/21 16:03:07 exit status 1

No self-respecting programming language designer would actually implement such a feature, but grun is about using Go in a very informal way.

It may feel more natural to actually edit these lines in a file:

$ cat read.go
// read.go
s,e := ioutil.ReadFile("nada.txt")
fmt.Println(string(s))

$ grun -f read.go
2021/04/21 16:17:24 open nada.txt: no such file or directory
exit status 1
2021/04/21 16:17:24 exit status 1

These are not really .go files, of course - the imports and boilerplate are implicit.

You can include a proper Go file using -i:

$ cat answer.go
package main

func answer() int {
	return 42
}
$ grun -e 'answer()' -i answer.go
42

Finally, after importing new packages that you would like to make available to grun, you will need to run grun -r to refresh the package list.

For example,

$ go get github.com/iancoleman/strcase
$ go doc strcase | tail
func ConfigureAcronym(key, val string)
func ToCamel(s string) string
func ToDelimited(s string, delimiter uint8) string
func ToKebab(s string) string
func ToLowerCamel(s string) string
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string
func ToScreamingKebab(s string) string
func ToScreamingSnake(s string) string
func ToSnake(s string) string
func ToSnakeWithIgnore(s string, ignore uint8) string
$ grun -r
$ grun -e 'strcase.ToScreamingSnake("HelloWorld")'
HELLO_WORLD

This is really useful when exploring a new API - and remember any setup boilerplate can be put into a Go file linked in with -i.

Owner
Steve J Donovan
Always been fascinated by programming languages, and know a fair number. Have a serious open source hacking habit. Currently focusing on Rust
Steve J Donovan
Similar Resources

Watcher - A simple command line app to watch files in a directory for changes and run a command when files change!

Watcher - Develop your programs easily Watcher watches all the files present in the directory it is run from of the directory that is specified while

Mar 27, 2022

LINE account link: Sample code for LINE account link

LINE account link: Sample code for LINE account link

LINE account link: Sample code for LINE account link This is sample code to demostration LINE chatbot account link, refer to document https://develope

Dec 11, 2021

argv - Go library to split command line string as arguments array using the bash syntax.

Argv Argv is a library for Go to split command line string into arguments array. Documentation Documentation can be found at Godoc Example func TestAr

Nov 19, 2022

CLI - A package for building command line app with go

CLI - A package for building command line app with go

Command line interface Screenshot Key features Lightweight and easy to use. Defines flag by tag, e.g. flag name(short or/and long), description, defau

Dec 23, 2022

Simple and complete API for building command line applications in Go

Simple and complete API for building command line applications in Go Module cli provides a simple, fast and complete API for building command line app

Nov 23, 2022

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

cmdr cmdr is a POSIX-compliant, command-line UI (CLI) library in Golang. It is a getopt-like parser of command-line options, be compatible with the ge

Oct 28, 2022

Automatically sets up command line flags based on struct fields and tags.

Automatically sets up command line flags based on struct fields and tags.

Commandeer Commandeer sets up command line flags based on struct fields and tags. Do you... like to develop Go apps as libraries with tiny main packag

Dec 1, 2022

A simple command line notebook for programmers

A simple command line notebook for programmers

Dnote is a simple command line notebook for programmers. It keeps you focused by providing a way of effortlessly capturing and retrieving information

Jan 2, 2023

Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand

Flag Flag is a simple but powerful commandline flag parsing library for Go. Documentation Documentation can be found at Godoc Supported features bool

Sep 26, 2022
Related tags
A commandline tool to resolve URI Templates expressions as specified in RFC 6570.

URI Are you tired to build, concat, replace URL(s) (via shell scripts sed/awk/tr) from your awesome commandline pipeline? Well! here is the missing pi

Jun 9, 2021
An open-source GitLab command line tool bringing GitLab's cool features to your command line
An open-source GitLab command line tool bringing GitLab's cool features to your command line

GLab is an open source GitLab CLI tool bringing GitLab to your terminal next to where you are already working with git and your code without switching

Dec 30, 2022
A command line tool that builds and (re)starts your web application everytime you save a Go or template fileA command line tool that builds and (re)starts your web application everytime you save a Go or template file

# Fresh Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file. If the web framework yo

Nov 22, 2021
A command line tool to prompt for a value to be included in another command line.

readval is a command line tool which is designed for one specific purpose—to prompt for a value to be included in another command line. readval prints

Dec 22, 2021
The runner project is to create an interface for users to run their code remotely without having to have any compiler on their machine
The runner project is to create an interface for users to run their code remotely without having to have any compiler on their machine

The runner project is to create an interface for users to run their code remotely without having to have any compiler on their machine. This is a work in progress project for TCSS 401X :)

May 29, 2022
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.

asciigraph Go package to make lightweight ASCII line graphs ╭┈╯. Installation go get github.com/guptarohit/asciigraph Usage Basic graph package main

Jan 8, 2023
git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.
git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.

Table of contents Introduction Reference Contributing Introduction Overview git-xargs is a command-line tool (CLI) for making updates across multiple

Dec 31, 2022
git-xargs is a command-line tool (CLI) for making updates across multiple GitHub repositories with a single command
git-xargs is a command-line tool (CLI) for making updates across multiple GitHub repositories with a single command

git-xargs is a command-line tool (CLI) for making updates across multiple GitHub repositories with a single command. You give git-xargs:

Feb 5, 2022
Package command provide simple API to create modern command-line interface

Package command Package command provide simple API to create modern command-line interface, mainly for lightweight usage, inspired by cobra Usage pack

Jan 16, 2022
A command line tool for simplified docker volume command built with go

dockervol A command line tool for simplified docker volume command built with go. Features: Remove anonymous volume (beta) Remove volume by matching n

Dec 18, 2021