Giter Club home page Giter Club logo

gore's Introduction

#gore

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

This package is forked from https://github.com/sriram-srinivasan/gore, with light edits.

#Usage

(Note: In the examples below, $ is the shell prompt.)

Code snippet in command line: gore evaluates its first argument

$ gore 'println(200*300, math.Log10(1000))'
60000 +3.000000e+000

Note the absence of boiler-plate code like package main, import "math" and func main() {}

Default to stdin without arguments

$ gore
Enter one or more lines and hit ctrl-D
func test() string {return "hello"}
println(test())
^D
hello

Alias for convenient printing

The example above can be written more compactly:

$ gore 'p 200*300, math.Log10(100)'
60000
2

p arg1, arg2 pretty-prints each argument by formatting it with fmt.Printf("%v\n"). t arg1, arg2` prints the type of each argument.

Command-line arg can be over multiple lines

$ gore '
 p "Making a point"
 type Point struct {
    x,y int
 }
 v := Point{10, 100}
 p v
' 
Making a point
{10 100}

Import statements are inferred

Standard go packages are automatically imported. Where there is a clash of names, the more "likely" one is preferred: math/rand to crypto/rand, net/http/pprof to runtime/pprof and text/template to html/template. Of course, you can add import statements of your own (which overrides the default preferences as well)

$ gore '
  r := regexp.MustCompile(`(\w+) says (\w+)`)
  match := r.FindStringSubmatch("World says Hello")
  p "0:" + match[0], "1:"+ match[1], "2:" + match[2]
  '
0:World says Hello
1:World
2:Hello

Install

go get github.com/theclapp/gore
go test github.com/theclapp/gore/eval

The gore/eval package

gore is a thin command-line wrapper over the gore/eval package. Use this for your own REPL.

How it works

The eval.Eval function expands aliases, and scans the snippet for references to packages from the standard Go library. All such references a corresponding import statement. The source is then partitioned into global and non-global code, where global refers to type, import and func declarations. The rest is bundled into a func main() {} wrapper. This reorganized code is compiled using go run and the output (stdout and stderr) collected. If there are compiler errors pointing to incorrectly inferred packages, the corresponding import statements are removed and the code is run once again.

To examine the generated code, set the environment variables TMPDIR or TEMPDIR, and look for $TMPDIR/gore_eval.go

License

gore is available under a liberal MIT style license. See the LICENSE file.

gore's People

Contributors

dmitshur avatar snipsnipsnip avatar sriram-srinivasan avatar theclapp avatar tmc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.